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) ⇒ Dynamoid::Document/Array

Find using exciting method_missing finders attributes. Uses criteria chains under the hood to accomplish this neatness.

Examples:

find a user by a first name

User.find_by_first_name('Josh')

find all users by first and last name

User.find_all_by_first_name_and_last_name('Josh', 'Symonds')

Since:

  • 0.2.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dynamoid/finders.rb', line 113

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(*ids) ⇒ Dynamoid::Document

Find one or many objects, specified by one id or an array of ids.

Since:

  • 0.2.0



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dynamoid/finders.rb', line 18

def find(*ids)

  options = if ids.last.is_a? Hash
              ids.slice!(-1)
            else
              {}
            end

  ids = Array(ids.flatten.uniq)
  if ids.count == 1
    self.find_by_id(ids.first, options)
  else
    find_all(ids)
  end
end

#find_all(ids, options = {}) ⇒ Object

Return objects found by the given array of ids, either hash keys, or hash/range key combinations using BatchGet. Returns empty array if no results found.

Examples:

find all the user with hash key
User.find_all(['1', '2', '3'])

find all the tweets using hash key and range key with consistent read
Tweet.find_all([['1', 'red'], ['1', 'green']], :consistent_read => true)


46
47
48
49
# File 'lib/dynamoid/finders.rb', line 46

def find_all(ids, options = {})
  items = Dynamoid::Adapter.read(self.table_name, ids, options)
  items ? items[self.table_name].map{|i| from_database(i)} : []
end

#find_all_by_composite_key(hash_key, options = {}) ⇒ Array

Find all objects by hash and range keys.

Examples:

find all ChamberTypes whose level is greater than 1

class ChamberType
  include Dynamoid::Document
  field :chamber_type,            :string
  range :level,                   :integer
  table :key => :chamber_type
end
ChamberType.find_all_by_composite_key('DustVault', range_greater_than: 1)

Options Hash (options):

  • :range_value (Range)

    find the range key within this range

  • :range_greater_than (Number)

    find range keys greater than this

  • :range_less_than (Number)

    find range keys less than this

  • :range_gte (Number)

    find range keys greater than or equal to this

  • :range_lte (Number)

    find range keys less than or equal to this



96
97
98
99
100
# File 'lib/dynamoid/finders.rb', line 96

def find_all_by_composite_key(hash_key, options = {})
  Dynamoid::Adapter.query(self.table_name, options.merge({hash_value: hash_key})).collect do |item|
    from_database(item)
  end
end

#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object

Find one object directly by hash and range keys



71
72
73
# File 'lib/dynamoid/finders.rb', line 71

def find_by_composite_key(hash_key, range_key, options = {})
  find_by_id(hash_key, options.merge({:range_key => range_key}))
end

#find_by_id(id, options = {}) ⇒ Dynamoid::Document

Find one object directly by id.

Since:

  • 0.2.0



58
59
60
61
62
63
64
# File 'lib/dynamoid/finders.rb', line 58

def find_by_id(id, options = {})
  if item = Dynamoid::Adapter.read(self.table_name, id, options)
    from_database(item)
  else
    nil
  end
end