From a4e3de738fd0501432e7b5ae7865a8902d70a11d Mon Sep 17 00:00:00 2001 From: Austin Kabiru Date: Thu, 9 Aug 2018 19:48:07 +0300 Subject: [PATCH] feat(output): Display tree output Redefine `Bot` as dynamic object to prevent retaining of outdated state --- Gemfile.lock | 10 +++++--- fakerbot.gemspec | 1 + lib/fakerbot/bot.rb | 45 +++++++++++++++++++-------------- lib/fakerbot/commands/search.rb | 13 +++++++++- lib/fakerbot/version.rb | 2 +- spec/fakerbot/bot_spec.rb | 4 +-- 6 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b9882f9..68942cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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) @@ -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) diff --git a/fakerbot.gemspec b/fakerbot.gemspec index 1392646..7c8bb8c 100644 --- a/fakerbot.gemspec +++ b/fakerbot.gemspec @@ -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" diff --git a/lib/fakerbot/bot.rb b/lib/fakerbot/bot.rb index 26058d3..94105f8 100644 --- a/lib/fakerbot/bot.rb +++ b/lib/fakerbot/bot.rb @@ -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 diff --git a/lib/fakerbot/commands/search.rb b/lib/fakerbot/commands/search.rb index f66645c..1950a4c 100644 --- a/lib/fakerbot/commands/search.rb +++ b/lib/fakerbot/commands/search.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'pastel' +require 'tty/tree' require 'fakerbot/bot' module FakerBot @@ -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 diff --git a/lib/fakerbot/version.rb b/lib/fakerbot/version.rb index e42d7d1..a8a0617 100644 --- a/lib/fakerbot/version.rb +++ b/lib/fakerbot/version.rb @@ -1,3 +1,3 @@ module FakerBot - VERSION = '0.1.2' + VERSION = '0.2.0'.freeze end diff --git a/spec/fakerbot/bot_spec.rb b/spec/fakerbot/bot_spec.rb index 498230f..446c531 100644 --- a/spec/fakerbot/bot_spec.rb +++ b/spec/fakerbot/bot_spec.rb @@ -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