Module: Lore::Prepare

Included in:
Table_Accessor, Table_Accessor
Defined in:
lib/lore/model/prepare.rb

Instance Method Summary collapse

Instance Method Details

#define_default_prepsObject

Defines prepared statements like The_Model.by_id(id), The_Model.latest_entries(id, amount) etc.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lore/model/prepare.rb', line 8

def define_default_preps
  return
  unless @@prepared_statements[:default_preps] then
    pkey_attrib_name = table_name + '.' << @primary_keys[table_name].first.to_s
    prepare(:_by_id, Lore::Type.integer) { |e|
      e.where(self.__send__(pkey_attrib_name.to_s.split('.')[-1].intern) == Lore::Clause.new('$1'))
      e.limit(1)
    }
    prepare(:_latest, Lore::Type.integer) { |e|
      e.where(true)
      e.order_by(pkey_attrib_name, :desc)
      e.limit(Lore::Clause.new('$1'))
    }
    @@prepared_statements[:default_preps] = true
  end
end