Skip to content

Commit

Permalink
Add ability to specify filters to search by (#4)
Browse files Browse the repository at this point in the history
* Add ability to specify filters to search by

* Address feedback
  • Loading branch information
doutatsu authored and antstorm committed Dec 17, 2017
1 parent 1827d95 commit 538eb6b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
19 changes: 14 additions & 5 deletions lib/money-heuristics/analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module MoneyHeuristics
class Analyzer
attr_reader :search_tree, :words
attr_accessor :str, :currencies
attr_accessor :str, :currencies, :methods

def initialize(str, search_tree)
@str = (str || '').dup
@search_tree = search_tree
@currencies = []
@methods = { iso_code: :search_by_iso_code,
symbol: :search_by_symbol,
name: :search_by_name }
end

def process
def process(filters)
format
return [] if str.empty?

search_by_symbol
search_by_iso_code
search_by_name
filter_methods(filters).each { |method| send(method) }

prepare_reply
end
Expand Down Expand Up @@ -80,5 +81,13 @@ def prepare_reply
codes.sort!
codes
end

def filter_methods(filters)
filtered = methods.values_at(*filters).compact

return methods.values if filtered.empty?

filtered
end
end
end
4 changes: 2 additions & 2 deletions lib/money-heuristics/heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module Heuristics
# currency in an entire sentence
#
# Returns: Array (matched results)
def analyze(str)
def analyze(str, filters: [])
@_heuristics_search_tree ||= MoneyHeuristics::SearchTree.new(table).build

return MoneyHeuristics::Analyzer.new(str, @_heuristics_search_tree).process
return MoneyHeuristics::Analyzer.new(str, @_heuristics_search_tree).process(filters)
end
end
end
11 changes: 11 additions & 0 deletions spec/heuristics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,16 @@
it "should function with unicode characters" do
expect(it.analyze("10 դր.")).to eq ["AMD"]
end

context "with filters" do
it "finds only by specified filters" do
expect(it.analyze("10 ₮ + 2USD", filters: [:iso_code])).to eq ["USD"]
expect(it.analyze("10 ₮ + 2USD", filters: [:iso_code, :symbol])).to eq ["MNT", "USD"]
end

it "ignores nonexistent filters" do
expect(it.analyze("10 ₮ + 2USD", filters: [:wrong_filter])).to eq ["MNT", "USD"]
end
end
end
end

0 comments on commit 538eb6b

Please sign in to comment.