fakerbot 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 837cc2d92e1c31ad78fd7adc057fb11178d89bea
4
- data.tar.gz: 02e139d11db43948282be5cf4694cdc1cdda9db2
3
+ metadata.gz: 8085df33e12f4bd878c9189e56be6366d52d3fca
4
+ data.tar.gz: 034b971fc30137bd814f21bb07e059dc7b3ab251
5
5
  SHA512:
6
- metadata.gz: 82e8003271e1e494021551910a83b8c252a41f0aae71fb77b01c067ac2efafda4b456a8d56714544dde8d7f888d0161ee2966ce14efb75534f043ce8faf8d465
7
- data.tar.gz: bca6bd5310bf359267b52ad0ea2ac454dfc0432e59bd023d13e54e2857e645e939eb8d52e900a4758b93e04b623ac9b6b55605083415beff49c82c0770426f9d
6
+ metadata.gz: 946ccac66e20b766d9e66a4da666107d122992cdb248596bdc017d366dec36b99db1c142cd8a5cd422c1fd6c510b951187a9fb70e815cab80200ce9237683d0b
7
+ data.tar.gz: 23fca28de4270f36471c746ad630b976a5d35efdf0c4508b2207bb8f79b4ec28fb217e1681ead0ab5714d6e5d811905f9b81eb3ea8a9d78d30d8e2c325355ae4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fakerbot (0.3.0)
4
+ fakerbot (0.4.0)
5
5
  faker
6
6
  pastel (~> 0.7.2)
7
7
  thor (~> 0.20.0)
@@ -67,8 +67,8 @@ GEM
67
67
  json (>= 1.8, < 3)
68
68
  simplecov-html (~> 0.10.0)
69
69
  simplecov-html (0.10.2)
70
- strings (0.1.1)
71
- unicode-display_width (~> 1.3.0)
70
+ strings (0.1.2)
71
+ unicode-display_width (~> 1.4.0)
72
72
  unicode_utils (~> 1.4.0)
73
73
  term-ansicolor (1.6.0)
74
74
  tins (~> 1.0)
@@ -85,7 +85,7 @@ GEM
85
85
  unf (0.1.4)
86
86
  unf_ext
87
87
  unf_ext (0.0.7.5)
88
- unicode-display_width (1.3.3)
88
+ unicode-display_width (1.4.0)
89
89
  unicode_utils (1.4.0)
90
90
 
91
91
  PLATFORMS
data/lib/fakerbot/cli.rb CHANGED
@@ -20,8 +20,10 @@ module FakerBot
20
20
  desc 'list', 'List all Faker constants'
21
21
  method_option :help, aliases: '-h', type: :boolean,
22
22
  desc: 'Display usage information'
23
+ method_option :show_methods, aliases: '-m', type: :boolean, default: true,
24
+ desc: 'Display Faker constants with methods'
23
25
  method_option :verbose, aliases: '-v', type: :boolean,
24
- desc: 'Display Faker constants with methods'
26
+ desc: 'Include sample Faker output'
25
27
  def list(*)
26
28
  if options[:help]
27
29
  invoke :help, ['list']
@@ -33,8 +35,10 @@ module FakerBot
33
35
  desc 'search [Faker]', 'Search Faker method(s)'
34
36
  method_option :help, aliases: '-h', type: :boolean,
35
37
  desc: 'Display usage information'
38
+ method_option :show_methods, aliases: '-m', type: :boolean, default: true,
39
+ desc: 'Display Faker constants with methods'
36
40
  method_option :verbose, aliases: '-v', type: :boolean,
37
- desc: 'Display Faker constants methods with examples'
41
+ desc: 'Include sample Faker output'
38
42
  def search(query)
39
43
  if options[:help]
40
44
  invoke :help, ['search']
@@ -1,43 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
- require 'pastel'
5
- require 'tty/pager'
6
- require 'tty/tree'
4
+ require_relative 'reflector'
5
+ require_relative 'renderer'
7
6
 
8
7
  module FakerBot
9
8
  class Command
10
9
  extend Forwardable
11
10
 
12
11
  def_delegators :command, :run
12
+ attr_reader :options
13
13
 
14
- def pager
15
- TTY::Pager.new(command: 'less -R')
16
- end
17
-
18
- def screen
19
- TTY::Screen
20
- end
21
-
22
- def tree(input)
23
- TTY::Tree.new do
24
- input.each do |faker, methods|
25
- node Pastel.new.green(faker.to_s) do
26
- methods&.each { |m| leaf Pastel.new.cyan(m.to_s) }
27
- end
28
- end
29
- end
30
- end
31
-
32
- def render(result, output = $stdout)
33
- result_tree = tree(result)
34
- view = result_tree.render
35
- if screen.height < result_tree.nodes.size
36
- # paginate when attached to terminal
37
- output.tty? ? pager.page(view) : output.puts(view)
38
- else
39
- output.puts view
40
- end
14
+ def render(result, output)
15
+ FakerBot::Renderer.call(result, options, output)
41
16
  end
42
17
  end
43
18
  end
@@ -10,7 +10,8 @@ module FakerBot
10
10
  end
11
11
 
12
12
  def execute(output: $stdout)
13
- render FakerBot::Bot.list(verbose: @options[:verbose]), output
13
+ result = FakerBot::Reflector.list(show_methods: options[:show_methods])
14
+ render result, output
14
15
  end
15
16
  end
16
17
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'fakerbot/bot'
4
3
  require_relative '../command'
5
4
 
6
5
  module FakerBot
@@ -11,7 +10,7 @@ module FakerBot
11
10
  end
12
11
 
13
12
  def execute(input, output: $stdout)
14
- render FakerBot::Bot.find(input), output
13
+ render FakerBot::Reflector.find(input), output
15
14
  end
16
15
 
17
16
  private
@@ -3,20 +3,22 @@
3
3
  require 'faker'
4
4
 
5
5
  module FakerBot
6
- class Bot
6
+ # Exposes `Faker` reflection methods
7
+ # @api private
8
+ class Reflector
7
9
  Faker::Base.class_eval do
10
+ # Select `Faker` subclasses
11
+ # @return [Array] `Faker::Base` sub classes
8
12
  def self.descendants
9
13
  @descendants ||= ObjectSpace.each_object(Class).select do |klass|
10
14
  klass < self
11
15
  end
12
16
  end
13
17
 
18
+ # Select public class methods
19
+ # @return [Array] public methods
14
20
  def self.my_singleton_methods
15
- if superclass
16
- (singleton_methods - superclass.singleton_methods)
17
- else
18
- singleton_methods
19
- end
21
+ singleton_methods(false).select { |m| respond_to?(m) }
20
22
  end
21
23
  end
22
24
 
@@ -32,8 +34,8 @@ module FakerBot
32
34
  new(query).find
33
35
  end
34
36
 
35
- def list(verbose: false)
36
- new.list(verbose)
37
+ def list(show_methods: false)
38
+ new.list(show_methods)
37
39
  end
38
40
  end
39
41
 
@@ -42,8 +44,8 @@ module FakerBot
42
44
  descendants_with_methods
43
45
  end
44
46
 
45
- def list(verbose)
46
- verbose ? all_descendants_with_methods : faker_descendants
47
+ def list(show_methods)
48
+ show_methods ? all_descendants_with_methods : faker_descendants
47
49
  end
48
50
 
49
51
  private
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pastel'
4
+ require 'tty/pager'
5
+ require 'tty/screen'
6
+ require 'tty/tree'
7
+
8
+ module FakerBot
9
+ class Renderer
10
+ def self.call(hash, options, output)
11
+ new(hash, options, output).call
12
+ end
13
+
14
+ def initialize(hash, options, output)
15
+ @hash = hash
16
+ @options = options
17
+ @output = output
18
+ @crayon = Pastel.new(enabled: output.tty?)
19
+ @pager = TTY::Pager.new(command: 'less -R')
20
+ @screen = TTY::Screen
21
+ @tree = TTY::Tree
22
+ end
23
+
24
+ def call
25
+ data_tree = tree.new(build_tree)
26
+ view = data_tree.render
27
+ if gt_screen_height?(data_tree)
28
+ output.tty? ? pager.page(view) : output.puts(view)
29
+ else
30
+ output.puts view
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :crayon, :hash, :options, :output, :pager, :screen, :tree
37
+
38
+ def build_tree
39
+ hash.reduce({}) do |h, (faker, methods)|
40
+ h.merge! node(faker, methods)
41
+ end
42
+ end
43
+
44
+ def node(const, methods)
45
+ {
46
+ crayon.green(const.to_s) => leaf(const, methods)
47
+ }
48
+ end
49
+
50
+ def leaf(const, methods)
51
+ (methods || []).map do |m|
52
+ crayon.cyan(*leaf_args(m, const))
53
+ end
54
+ end
55
+
56
+ def leaf_args(method, const)
57
+ [method.to_s].tap do |arr|
58
+ if verbose?
59
+ fake = begin
60
+ const.public_send(method)
61
+ rescue ArgumentError
62
+ 'N/A'
63
+ end
64
+ arr << crayon.dim.white("=> #{fake.to_s}")
65
+ end
66
+ end
67
+ end
68
+
69
+ def gt_screen_height?(data_tree)
70
+ data_tree.nodes.size > screen.height
71
+ end
72
+
73
+ def verbose?
74
+ options[:verbose]
75
+ end
76
+ end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module FakerBot
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakerbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Kabiru
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-10 00:00:00.000000000 Z
11
+ date: 2018-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
@@ -202,11 +202,12 @@ files:
202
202
  - bin/setup
203
203
  - fakerbot.gemspec
204
204
  - lib/fakerbot.rb
205
- - lib/fakerbot/bot.rb
206
205
  - lib/fakerbot/cli.rb
207
206
  - lib/fakerbot/command.rb
208
207
  - lib/fakerbot/commands/list.rb
209
208
  - lib/fakerbot/commands/search.rb
209
+ - lib/fakerbot/reflector.rb
210
+ - lib/fakerbot/renderer.rb
210
211
  - lib/fakerbot/templates/list/.gitkeep
211
212
  - lib/fakerbot/version.rb
212
213
  homepage: https://github.com/akabiru/fakerbot