-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from akabiru/feature/--show-examples
Include Faker sample output
- Loading branch information
Showing
12 changed files
with
122 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'forwardable' | ||
require 'pastel' | ||
require 'tty/pager' | ||
require 'tty/tree' | ||
require_relative 'reflector' | ||
require_relative 'renderer' | ||
|
||
module FakerBot | ||
class Command | ||
extend Forwardable | ||
|
||
def_delegators :command, :run | ||
attr_reader :options | ||
|
||
def pager | ||
TTY::Pager.new(command: 'less -R') | ||
end | ||
|
||
def screen | ||
TTY::Screen | ||
end | ||
|
||
def tree(input) | ||
TTY::Tree.new do | ||
input.each do |faker, methods| | ||
node Pastel.new.green(faker.to_s) do | ||
methods&.each { |m| leaf Pastel.new.cyan(m.to_s) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
def render(result, output = $stdout) | ||
result_tree = tree(result) | ||
view = result_tree.render | ||
if screen.height < result_tree.nodes.size | ||
# paginate when attached to terminal | ||
output.tty? ? pager.page(view) : output.puts(view) | ||
else | ||
output.puts view | ||
end | ||
def render(result, output) | ||
FakerBot::Renderer.call(result, options, output) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pastel' | ||
require 'tty/pager' | ||
require 'tty/screen' | ||
require 'tty/tree' | ||
|
||
module FakerBot | ||
class Renderer | ||
def self.call(hash, options, output) | ||
new(hash, options, output).call | ||
end | ||
|
||
def initialize(hash, options, output) | ||
@hash = hash | ||
@options = options | ||
@output = output | ||
@crayon = Pastel.new(enabled: output.tty?) | ||
@pager = TTY::Pager.new(command: 'less -R') | ||
@screen = TTY::Screen | ||
@tree = TTY::Tree | ||
end | ||
|
||
def call | ||
data_tree = tree.new(build_tree) | ||
view = data_tree.render | ||
if gt_screen_height?(data_tree) | ||
output.tty? ? pager.page(view) : output.puts(view) | ||
else | ||
output.puts view | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :crayon, :hash, :options, :output, :pager, :screen, :tree | ||
|
||
def build_tree | ||
hash.reduce({}) do |h, (faker, methods)| | ||
h.merge! node(faker, methods) | ||
end | ||
end | ||
|
||
def node(const, methods) | ||
{ | ||
crayon.green(const.to_s) => leaf(const, methods) | ||
} | ||
end | ||
|
||
def leaf(const, methods) | ||
(methods || []).map do |m| | ||
crayon.cyan(*leaf_args(m, const)) | ||
end | ||
end | ||
|
||
def leaf_args(method, const) | ||
[method.to_s].tap do |arr| | ||
if verbose? | ||
fake = begin | ||
const.public_send(method) | ||
rescue ArgumentError | ||
'N/A' | ||
end | ||
arr << crayon.dim.white("=> #{fake.to_s}") | ||
end | ||
end | ||
end | ||
|
||
def gt_screen_height?(data_tree) | ||
data_tree.nodes.size > screen.height | ||
end | ||
|
||
def verbose? | ||
options[:verbose] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module FakerBot | ||
VERSION = '0.3.0'.freeze | ||
VERSION = '0.4.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters