Module: Wherever::Lookup

Included in:
Wherever
Defined in:
lib/wherever/wherever/lookup.rb

Instance Method Summary collapse

Instance Method Details

#create_lookup(name, keys) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wherever/wherever/lookup.rb', line 3

def create_lookup(name, keys)
  lookup_class = DbStore.new_lookup(name, keys)

  self.class.class_eval do
    define_method "set_#{name}" do |version, values|
      new_version?(name, version)
      lookup_class.create(:name => version, :values => key_to_string(values))
      recalculate(name) if set_price_lookup(name, version)
    end
  end

  self.class.class_eval do
    define_method "get_#{name}" do |marker, data|
      lookup = get_lookup(name)
      lookup_data = lookup_class.where(:name => lookup.lookups[marker]).first
      return 0 unless lookup_data 
      value_key = lookup.keys.map{|key| data[key.to_id]}.join('_')
      lookup_data.values[value_key] || 0
    end
  end
  MethodLogging.add_method(self.class, "set_#{name}")
  MethodLogging.add_method(self.class, "get_#{name}")
end