Skip to content

Commit

Permalink
feat(commands): Redefine -v as including examples
Browse files Browse the repository at this point in the history
Provide an option to include sample output on all commands
in order to better communicate what a faker methods does.

Closes #5
  • Loading branch information
akabiru committed Aug 13, 2018
1 parent 6cf8ce3 commit c6563bd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fakerbot (0.3.0)
fakerbot (0.4.0)
faker
pastel (~> 0.7.2)
thor (~> 0.20.0)
Expand Down Expand Up @@ -67,8 +67,8 @@ GEM
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
strings (0.1.1)
unicode-display_width (~> 1.3.0)
strings (0.1.2)
unicode-display_width (~> 1.4.0)
unicode_utils (~> 1.4.0)
term-ansicolor (1.6.0)
tins (~> 1.0)
Expand All @@ -85,7 +85,7 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
unicode-display_width (1.3.3)
unicode-display_width (1.4.0)
unicode_utils (1.4.0)

PLATFORMS
Expand Down
8 changes: 6 additions & 2 deletions lib/fakerbot/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def version
desc 'list', 'List all Faker constants'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :show_methods, aliases: '-m', type: :boolean, default: true,
desc: 'Display Faker constants with methods'
method_option :verbose, aliases: '-v', type: :boolean,
desc: 'Display Faker constants with methods'
desc: 'Include sample Faker output'
def list(*)
if options[:help]
invoke :help, ['list']
Expand All @@ -33,8 +35,10 @@ def list(*)
desc 'search [Faker]', 'Search Faker method(s)'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :show_methods, aliases: '-m', type: :boolean, default: true,
desc: 'Display Faker constants with methods'
method_option :verbose, aliases: '-v', type: :boolean,
desc: 'Display Faker constants methods with examples'
desc: 'Include sample Faker output'
def search(query)
if options[:help]
invoke :help, ['search']
Expand Down
3 changes: 2 additions & 1 deletion lib/fakerbot/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def initialize(options)
end

def execute(output: $stdout)
render FakerBot::Reflector.list(verbose: @options[:verbose]), output
result = FakerBot::Reflector.list(show_methods: options[:show_methods])
render result, output
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/fakerbot/reflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def find(query)
new(query).find
end

def list(verbose: false)
new.list(verbose)
def list(show_methods: false)
new.list(show_methods)
end
end

Expand All @@ -44,8 +44,8 @@ def find
descendants_with_methods
end

def list(verbose)
verbose ? all_descendants_with_methods : faker_descendants
def list(show_methods)
show_methods ? all_descendants_with_methods : faker_descendants
end

private
Expand Down
25 changes: 14 additions & 11 deletions lib/fakerbot/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,29 @@ def node(const, methods)

def leaf(const, methods)
(methods || []).map do |m|
args = [m.to_s].tap do |a|
if show_examples?
fake = begin
const.public_send(m)
rescue ArgumentError
'N/A'
end
a << pastel.dim.white("=> #{fake.to_s}")
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
crayon.cyan(*args)
end
end

def gt_screen_height?(data_tree)
data_tree.nodes.size > screen.height
end

def show_examples?
options[:show_examples]
def verbose?
options[:verbose]
end
end
end
2 changes: 1 addition & 1 deletion lib/fakerbot/version.rb
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
8 changes: 5 additions & 3 deletions spec/integration/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
fakerbot list
Options:
-h, [--help], [--no-help] # Display usage information
-v, [--verbose], [--no-verbose] # Display Faker constants with methods
-h, [--help], [--no-help] # Display usage information
-m, [--show-methods], [--no-show-methods] # Display Faker constants with methods
# Default: true
-v, [--verbose], [--no-verbose] # Include sample Faker output
List all Faker constants
OUT

expect(output).to eq(expected_output)
expect(output).to match(expected_output)
end

it 'executes `fakerbot list` command successfully' do
Expand Down
9 changes: 5 additions & 4 deletions spec/integration/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
fakerbot search [Faker]
Options:
-h, [--help], [--no-help] # Display usage information
-v, [--verbose], [--no-verbose] # Display Faker constants methods with examples
-h, [--help], [--no-help] # Display usage information
-m, [--show-methods], [--no-show-methods] # Display Faker constants with methods
# Default: true
-v, [--verbose], [--no-verbose] # Include sample Faker output
Search Faker method(s)
OUT

expect(output).to eq(expected_output)
expect(output).to match(expected_output)
end

context 'when search query exists' do
Expand Down
3 changes: 0 additions & 3 deletions spec/unit/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

it 'executes successfully' do
constant = output.string.lines[0]
method = output.string.lines[1]

expect(constant).to match(/Faker::/)
expect(method).not_to match(/Faker::/)
expect(method).to match(/└──/)
end
end
end

0 comments on commit c6563bd

Please sign in to comment.