class Faker::Bot::Renderer
A class responsible for printing output to an [IO] interface
@api private
Constants
- DEPRECATION_WARNING
- EMPTY
- NOT_AVAILABLE
Attributes
Public Class Methods
# File lib/faker/bot/renderer.rb, line 21 def self.call(*args) new(*args).call end
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
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
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
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 the structured data tree
@return [String]
@api private
# File lib/faker/bot/renderer.rb, line 63 def render tree.render end
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 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
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
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
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
# 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
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
Boolean verbose option flag
@return [Boolean]
@api private
# File lib/faker/bot/renderer.rb, line 147 def verbose? options[:verbose] end
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