Class: Factbase::Best

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/terms/best.rb

Overview

The ‘best’ term evaluates the best value for a given key.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Best

Returns a new instance of Best.



8
9
10
# File 'lib/factbase/terms/best.rb', line 8

def initialize(&block)
  @criteria = block
end

Instance Method Details

#evaluate(key, maps) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/factbase/terms/best.rb', line 12

def evaluate(key, maps)
  raise "A symbol is expected, but #{key} provided" unless key.is_a?(Symbol)
  best = nil
  maps.each do |m|
    vv = m[key.to_s]
    next if vv.nil?
    vv = [vv] unless vv.respond_to?(:to_a)
    vv.each do |v|
      best = v if best.nil? || @criteria.call(v, best)
    end
  end
  best
end