Skip to content

Commit

Permalink
feat(output): Display tree output
Browse files Browse the repository at this point in the history
Redefine `Bot` as dynamic object to prevent retaining
of outdated state
  • Loading branch information
akabiru committed Aug 9, 2018
1 parent a122987 commit a4e3de7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
10 changes: 6 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
PATH
remote: .
specs:
fakerbot (0.1.2)
fakerbot (0.2.0)
faker
pastel (~> 0.7.2)
thor (~> 0.20.0)
tty-tree

GEM
remote: https://rubygems.org/
Expand All @@ -22,11 +23,11 @@ GEM
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
equatable (0.5.0)
faker (1.8.7)
faker (1.9.1)
i18n (>= 0.7)
http-cookie (1.0.3)
domain_name (~> 0.5)
i18n (1.0.1)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
json (2.1.0)
method_source (0.9.0)
Expand Down Expand Up @@ -68,7 +69,8 @@ GEM
tins (~> 1.0)
thor (0.20.0)
tins (1.16.3)
tty-color (0.4.2)
tty-color (0.4.3)
tty-tree (0.1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
Expand Down
1 change: 1 addition & 0 deletions fakerbot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "faker"
spec.add_dependency "pastel", "~> 0.7.2"
spec.add_dependency "thor", "~> 0.20.0"
spec.add_dependency "tty-tree"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "coveralls"
Expand Down
45 changes: 26 additions & 19 deletions lib/fakerbot/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,41 @@ def self.my_singleton_methods
end
end

attr_reader :matching_descendants, :query

def initialize(query)
@matching_descendants = Hash.new { |h, k| h[k] = [] }
@query = query
end

class << self
def find(query)
search_descendants_matching(query)
matching_descendants
new(query).find
end
end

private
def find
search_descendants_matching_query
matching_descendants
end

def search_descendants_matching(query)
faker_descendants.each do |faker|
methods = faker.my_singleton_methods
matching = methods.select { |m| m.match?(/#{query}/i) }
save_matching(faker, matching)
end
end
private

def save_matching(descendant, matching)
return if matching.empty?
matching_descendants[descendant].concat(matching)
def search_descendants_matching_query
faker_descendants.each do |faker|
methods = faker.my_singleton_methods
matching = methods.select { |m| m.match?(/#{query}/i) }
save_matching(faker, matching)
end
end

def matching_descendants
@matching_descendants ||= Hash.new { |h, k| h[k] = [] }
end
def save_matching(descendant, matching)
return if matching.empty?
matching_descendants[descendant].concat(matching)
end

def faker_descendants
@faker_descendants ||= Faker::Base.descendants
end
def faker_descendants
@faker_descendants ||= Faker::Base.descendants
end
end
end
13 changes: 12 additions & 1 deletion lib/fakerbot/commands/search.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'pastel'
require 'tty/tree'
require 'fakerbot/bot'

module FakerBot
Expand All @@ -12,7 +13,17 @@ def initialize(options)

def execute(input)
result = FakerBot::Bot.find(input)
puts(result.map { |r| Pastel.new.green(r) })
puts tree(result).render
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
end
end
Expand Down
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.1.2'
VERSION = '0.2.0'.freeze
end
4 changes: 2 additions & 2 deletions spec/fakerbot/bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
let(:result) { bot.find('name') }

it 'it returns the list of matches' do
expect(result).to include('Faker::Name')
expect(result).to be_a(Array)
expect(result[Faker::Name]).to include(:name)
expect(result).to be_a(Hash)
end
end

Expand Down

0 comments on commit a4e3de7

Please sign in to comment.