class Faker::Bot::Renderer

A class responsible for printing output to an [IO] interface

@api private

Constants

DEPRECATION_WARNING
EMPTY
NOT_AVAILABLE

Attributes

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

Public Class Methods

call(*args) click to toggle source
# File lib/faker/bot/renderer.rb, line 21
def self.call(*args)
  new(*args).call
end
new(hash, options, output) click to toggle source

Initialize a Render

@param hash [Hash<Class => <Array<Symbol>>] @param options [Hash] @param output [IO]

@api public

# File lib/faker/bot/renderer.rb, line 33
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

Print paginated output if the terminal interface supports pagination. Otherwise, just print the full output

@return [IO]

@api private

# File lib/faker/bot/renderer.rb, line 49
def call
  if paginable?
    pager.page(render)
  else
    output.puts(render)
  end
end
gt_screen_height?() click to toggle source

Check whether the tree size is greater than current screen height

@return [Boolean]

@api private

# File lib/faker/bot/renderer.rb, line 93
def gt_screen_height?
  tree.nodes.size > TTY::Screen.height
end
paginable?() click to toggle source

Check whether the terminal interface supports pagination

@return [Boolean]

@api private

# File lib/faker/bot/renderer.rb, line 83
def paginable?
  gt_screen_height? && output.tty?
end
render() click to toggle source

Render the structured data tree

@return [String]

@api private

# File lib/faker/bot/renderer.rb, line 63
def render
  tree.render
end
tree() click to toggle source

Warm up the structured data tree render object

@return [TTY<Tree>]

@api private

# File lib/faker/bot/renderer.rb, line 73
def tree
  @tree ||= TTY::Tree.new(build_tree)
end

Private Instance Methods

build_tree() click to toggle source

Build the structured data tree sorted alphabetically

@return [Hash{Class => <Array<Symbol>}]

@api private

# File lib/faker/bot/renderer.rb, line 105
def build_tree
  results = hash.reduce({}) do |h, (const, methods)|
    h.merge! node(const, methods&.sort)
  end

  results.sort_by(&:to_s).to_h
end
ensure_method_is_supported(method, const) click to toggle source

Mark deprecated methods

@return [String]

@api private

# File lib/faker/bot/renderer.rb, line 185
def ensure_method_is_supported(method, const)
  if const.respond_to?(:"_deprecated_#{method}")
    DEPRECATION_WARNING
  else
    EMPTY
  end
end
faker_method(method, const) click to toggle source

Send message to Faker object; receive sample fake data

@return [Array<String>]

@api private

# File lib/faker/bot/renderer.rb, line 170
def faker_method(method, const)
  [
    const.public_send(method),
    ensure_method_is_supported(method, const)
  ]
rescue ArgumentError => _e
  [NOT_AVAILABLE, EMPTY]
end
leaf(const, methods) click to toggle source

Tree leaf builder with color

@return [Array<Symbol>]

@api private

# File lib/faker/bot/renderer.rb, line 131
def leaf(const, methods)
  (methods || []).map { |m| crayon.cyan(*leaf_args(m, const)) }
end
leaf_args(method, const) click to toggle source
# File lib/faker/bot/renderer.rb, line 135
def leaf_args(method, const)
  [method.to_s].tap do |arr|
    verbose_output(method, const, arr) if verbose?
  end
end
node(const, methods) click to toggle source

Tree node builder with color

@return [Hash{Class => <Array<Symbol>}]

@api private

# File lib/faker/bot/renderer.rb, line 119
def node(const, methods)
  {
    crayon.green(const.to_s) => leaf(const, methods)
  }
end
verbose?() click to toggle source

Boolean verbose option flag

@return [Boolean]

@api private

# File lib/faker/bot/renderer.rb, line 147
def verbose?
  options[:verbose]
end
verbose_output(method, const, arr) click to toggle source

Generate verbose output via sample fake data

@return [Array<String>]

@api private

# File lib/faker/bot/renderer.rb, line 157
def verbose_output(method, const, arr)
  fake, message = faker_method(method, const)

  arr.push(crayon.dim.white("=> #{fake}"))
     .push(crayon.dim.magenta.bold(message.to_s))
end