class FakerBot::Renderer

Attributes

crayon[R]
hash[R]
options[R]
output[R]
pager[R]

Public Class Methods

call(*args) click to toggle source
# File lib/fakerbot/renderer.rb, line 12
def self.call(*args)
  new(*args).call
end
new(hash, options, output) click to toggle source
# File lib/fakerbot/renderer.rb, line 16
def initialize(hash, options, output)
  @hash = hash
  @options = options
  @output = output
  @crayon = Pastel.new(enabled: output.tty?)
  @pager = TTY::Pager.new(command: 'less -R')
end

Public Instance Methods

call() click to toggle source
# File lib/fakerbot/renderer.rb, line 24
def call
  if paginable?
    pager.page(render)
  else
    output.puts(render)
  end
end
gt_screen_height?() click to toggle source
# File lib/fakerbot/renderer.rb, line 44
def gt_screen_height?
  tree.nodes.size > TTY::Screen.height
end
paginable?() click to toggle source
# File lib/fakerbot/renderer.rb, line 40
def paginable?
  gt_screen_height? && output.tty?
end
render() click to toggle source
# File lib/fakerbot/renderer.rb, line 32
def render
  tree.render
end
tree() click to toggle source
# File lib/fakerbot/renderer.rb, line 36
def tree
  @tree ||= TTY::Tree.new(build_tree)
end

Private Instance Methods

build_tree() click to toggle source
# File lib/fakerbot/renderer.rb, line 50
def build_tree
  result = hash.reduce({}) do |h, (faker, methods)|
    h.merge! node(faker, methods&.sort)
  end

  result.sort_by(&:to_s).to_h
end
ensure_method_is_supported(method, const) click to toggle source
# File lib/fakerbot/renderer.rb, line 87
def ensure_method_is_supported(method, const)
  const.respond_to?(:"_deprecated_#{method}") ? ' ( WILL BE DEPRECATED )' : ''
end
faker_method(method, const) click to toggle source
# File lib/fakerbot/renderer.rb, line 81
def faker_method(method, const)
  [const.public_send(method), ensure_method_is_supported(method, const)]
rescue ArgumentError => _exception
  ['N/A', '']
end
leaf(const, methods) click to toggle source
# File lib/fakerbot/renderer.rb, line 64
def leaf(const, methods)
  (methods || []).map { |m| crayon.cyan(*leaf_args(m, const)) }
end
leaf_args(method, const) click to toggle source
# File lib/fakerbot/renderer.rb, line 68
def leaf_args(method, const)
  [method.to_s].tap { |arr| verbose_output(method, const, arr) if verbose? }
end
node(const, methods) click to toggle source
# File lib/fakerbot/renderer.rb, line 58
def node(const, methods)
  {
    crayon.green(const.to_s) => leaf(const, methods)
  }
end
verbose?() click to toggle source
# File lib/fakerbot/renderer.rb, line 72
def verbose?
  options[:verbose]
end
verbose_output(method, const, arr) click to toggle source
# File lib/fakerbot/renderer.rb, line 76
def verbose_output(method, const, arr)
  fake, message = faker_method(method, const)
  arr << crayon.dim.white("=> #{fake}") << crayon.dim.magenta.bold(message.to_s)
end