Skip to content

Commit

Permalink
Merge pull request #38 from GhostGroup/feature/36-namespace-searching
Browse files Browse the repository at this point in the history
Allow search to accept namespaces and partial matches
  • Loading branch information
akabiru authored Jul 26, 2019
2 parents ab38a40 + 2cc8bae commit a5ebe64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
18 changes: 12 additions & 6 deletions lib/faker/bot/reflectors/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def self.call(query)
end

def initialize(query)
@query = query
@query = query.downcase

super
end
Expand All @@ -31,14 +31,20 @@ def call
def search_descendants_matching_query
faker_descendants.each do |descendant|
methods = descendant.my_singleton_methods
matching = methods.select { |method| query_matches?(method.to_s) }
store(descendant, matching)
if query_matches_class_name?(descendant.to_s)
store(descendant, methods)
else
store(descendant, methods.select { |method| query_matches_method?(method.to_s) })
end
end
end

def query_matches?(method_name)
method_name_parts = method_name.split(/_/).reject(&:empty?)
query.match(/#{method_name_parts.join('|')}/)
def query_matches_method?(method_name)
method_name.match(/#{query}/)
end

def query_matches_class_name?(class_name)
class_name.downcase.match(/#{query}/)
end
end
end
Expand Down
41 changes: 32 additions & 9 deletions spec/faker/bot/reflectors/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,48 @@

RSpec.describe Faker::Bot::Reflectors::Search do
describe '#call' do
context 'when a match is found' do
let(:result) { described_class.new(query).call }

context 'when passing a full method name' do
let(:query) { 'first_name' }

it 'it returns the list of matches' do
query = 'firstname'
expect(result[Faker::Name]).to include(:first_name)
expect(result).to be_a(Hash)
end
end

reflector = described_class.new(query)
result = reflector.call
context 'when passing a partial method name' do
let(:query) { 'fir' }

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

context 'when no match is found' do
it 'returns an empty hash' do
query = 'foobar'
context 'when passing a full class name' do
let(:query) { 'Beer' }

reflector = described_class.new(query)
result = reflector.call
it 'returns the list of matches' do
expect(result).to include(Faker::Beer)
expect(result).to be_a(Hash)
end
end

context 'when passing a partial class name' do
let(:query) { 'bee' }

it 'returns the list of matches' do
expect(result).to include(Faker::Beer)
expect(result).to be_a(Hash)
end
end

context 'when no match is found' do
let(:query) { 'firstname'}

it 'returns an empty hash' do
expect(result).to be_empty
expect(result).to be_a(Hash)
end
Expand Down

0 comments on commit a5ebe64

Please sign in to comment.