class Wilderpeople::Search
Attributes
args[R]
config[R]
data[R]
result[R]
Public Class Methods
new(data: [], config: {})
click to toggle source
# File lib/wilderpeople/search.rb, line 5 def initialize(data: [], config: {}) @data = data.collect(&:with_indifferent_access) @config = config end
Public Instance Methods
find(args)
click to toggle source
# File lib/wilderpeople/search.rb, line 10 def find(args) select(args) result.first if result.size == 1 end
select(args)
click to toggle source
# File lib/wilderpeople/search.rb, line 15 def select(args) @result = data @args = args select_must select_can if result.size > 1 result end
Private Instance Methods
can_rules()
click to toggle source
# File lib/wilderpeople/search.rb, line 57 def can_rules config[:can] end
find_occurrences(array)
click to toggle source
Returns a hash with each item as key, and the count of occurrences as value See: jerodsanto.net/2013/10/ruby-quick-tip-easily-count-occurrences-of-array-elements/
# File lib/wilderpeople/search.rb, line 63 def find_occurrences(array) array.flatten.each_with_object(Hash.new(0)){ |item,count| count[item] += 1 } end
must_rules()
click to toggle source
# File lib/wilderpeople/search.rb, line 36 def must_rules config[:must] end
select_can()
click to toggle source
# File lib/wilderpeople/search.rb, line 40 def select_can return if result.empty? return unless can_rules # Get all of the matches for each of the can rules. matches = can_rules.collect do |key, matcher_method| result.select do |datum| [matcher_method, datum[key], args[key]] Matcher.by matcher_method, datum[key], args[key] end end # Then determine which items appears in more matches # than any other, and if so return those. occurrences = find_occurrences(matches) count_of_commonest = occurrences.values.max @result = occurrences.select{|_k, v| v == count_of_commonest}.keys end
select_must()
click to toggle source
# File lib/wilderpeople/search.rb, line 25 def select_must return if result.empty? return unless must_rules @result = result.select do |datum| must_rules.all? do |key, matcher_method| return false unless datum[key] && args[key] Matcher.by matcher_method, datum[key], args[key] end end end