Module: Dynamoid::Finders::ClassMethods

Defined in:
lib/dynamoid/finders.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamoid/finders.rb', line 30

def method_missing(method, *args)
  if method =~ /find/
    finder = method.to_s.split('_by_').first
    attributes = method.to_s.split('_by_').last.split('_and_')

    chain = Dynamoid::Criteria::Chain.new(self)
    chain.query = Hash.new.tap {|h| attributes.each_with_index {|attr, index| h[attr.to_sym] = args[index]}}
    
    if finder =~ /all/
      return chain.all
    else
      return chain.first
    end
  else
    super
  end
end

Instance Method Details

#find(*id) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dynamoid/finders.rb', line 10

def find(*id)
  id = Array(id.flatten.uniq)
  if id.count == 1
    self.find_by_id(id.first)
  else
    items = Dynamoid::Adapter.batch_get_item(self.table_name => id)
    items[self.table_name].collect{|i| o = self.build(i); o.new_record = false; o}
  end
end

#find_by_id(id) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/dynamoid/finders.rb', line 20

def find_by_id(id)
  if item = Dynamoid::Adapter.get_item(self.table_name, id)
    obj = self.new(Dynamoid::Adapter.get_item(self.table_name, id))
    obj.new_record = false
    return obj
  else
    return nil
  end
end