Class: Lumix::Lookup::LookupCollection

Inherits:
Hash
  • Object
show all
Defined in:
lib/lumix/lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(ds, column) ⇒ LookupCollection

Returns a new instance of LookupCollection.



24
25
26
27
28
29
30
31
32
# File 'lib/lumix/lookup.rb', line 24

def initialize(ds, column)
  @ds = ds
  @column = column
  super(){ |h,k| h[k] = create(k) }

  @ds.each do |e|
    self[e[@column]] = e[:id]
  end
end

Instance Method Details

#create(value) ⇒ Object



34
35
36
37
38
# File 'lib/lumix/lookup.rb', line 34

def create(value)
  @ds.db.transaction(:isolation => :serializable) do
    @ds.where(@column => value).select(:id).single_value || @ds.insert(@column => value)
  end
end