Module: AWS::Record::FinderMethods

Included in:
Base
Defined in:
lib/aws/record/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#[](id) ⇒ Record::Base

Returns the record, as a sub-class of Record::Base

Parameters:

  • id (String)

    The id of the record to load.

Returns:

  • (Record::Base)

    Returns the record, as a sub-class of Record::Base

Raises:

  • (RecordNotFound)

    Raises a record not found exception if there was no data found for the given id.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws/record/finder_methods.rb', line 26

def [] id

  data = sdb_domain.items[id].data.attributes

  raise RecordNotFound, "no data found for id: #{id}" if data.empty?

  obj = self.new
  obj.send(:hydrate, id, data)
  obj

end

#all(options = {}) ⇒ Object

Equivalent to find(:all)



87
88
89
# File 'lib/aws/record/finder_methods.rb', line 87

def all(options = {})
  find(:all, options)
end

#attributesHash

Returns A hash of Attribute objects that represent the “columns” objects in this domain use.

Returns:

  • (Hash)

    A hash of Attribute objects that represent the “columns” objects in this domain use.



223
224
225
# File 'lib/aws/record/finder_methods.rb', line 223

def attributes
  @attributes ||= {}
end

#count(options = {}) ⇒ Object Also known as: size

Counts records in SimpleDB.

With no arguments, counts all records:

People.count

Accepts query options to count a subset of records:

People.count(:where => { :boss => true })

You can also count records on a scope object:

People.find(:all).where(:boss => true).count

See #find and Scope#count for more details.

Parameters:

  • options (Hash) (defaults to: {})

    ({}) Options for counting records.

Options Hash (options):

  • :where (Mixed)

    Conditions that determine what records are counted.

  • :limit (Integer)

    The max number of records to count.



113
114
115
# File 'lib/aws/record/finder_methods.rb', line 113

def count(options = {})
  find(:all).count(options)
end

#create_domainObject

Creates the SimpleDB domain that is configured for this class.



217
218
219
# File 'lib/aws/record/finder_methods.rb', line 217

def create_domain
  AWS::SimpleDB.new.domains.create(domain_name)
end

#domain_nameString

Returns the full prefixed domain name for this class.

Returns:

  • (String)

    Returns the full prefixed domain name for this class.



205
206
207
208
# File 'lib/aws/record/finder_methods.rb', line 205

def domain_name
  @_domain_name ||= self.to_s
  "#{Record.domain_prefix}#{@_domain_name}"
end

#find(id) ⇒ Object #find(mode, options = {}) ⇒ Object

Finds records in SimpleDB and returns them as objects of the current class.

Finding :all returns an enumerable scope object

People.find(:all, :order => [:age, :desc], :limit => 10).each do |person|
  puts person.name
end

Finding :first returns a single record (or nil)

boss = People.find(:first, :where => { :boss => true })

Find accepts a hash of find modifiers (:where, :order and :limit). You can also choose to omit these modifiers and chain them on the scope object returned. In the following example only one request is made to SimpleDB (when #each is called)

people = People.find(:all)

johns = people.where(:name => 'John Doe')

johns.order(:age, :desc).limit(10).each do |suspects|
  # ...
end

See also #where, #order and #limit for more information and options.

Overloads:

  • #find(id) ⇒ Object

    Parameters:

    • id

      The record to find, raises an exception if the record is not found.

  • #find(mode, options = {}) ⇒ Object

    Parameters:

    • mode (:all, :first)

      (:all) When finding :all matching records and array is returned of records. When finding :first then nil or a single record will be returned.

    • options (Hash) (defaults to: {})

    Options Hash (options):

    • :where (Mixed)

      Conditions that determine what records are returned.

    • :sort (String, Array)

      The order records should be returned in.

    • :limit (Integer)

      The max number of records to fetch.



82
83
84
# File 'lib/aws/record/finder_methods.rb', line 82

def find *args
  _new_scope.find(*args)
end

#first(options = {}) ⇒ Object?

Returns the first record found for the current class. If there are no records in the current classes domain, then nil is returned.

Returns:

  • (Object, nil)

    Returns the first record found for the current class. If there are no records in the current classes domain, then nil is returned.



121
122
123
# File 'lib/aws/record/finder_methods.rb', line 121

def first options = {}
  _new_scope.find(:first, options)
end

#limit(limit) ⇒ Object

The maximum number of records to return. By default, all records matching the where conditions will be returned from a find.

People.limit(10).each {|person| ... }

Limit can be chained with other scope modifiers:

People.where(:age => 40).limit(10).each {|person| ... }


187
188
189
# File 'lib/aws/record/finder_methods.rb', line 187

def limit limit
  _new_scope.limit(limit)
end

#optimistic_locking(attribute_name = :version_id) ⇒ Object



250
251
252
253
# File 'lib/aws/record/finder_methods.rb', line 250

def optimistic_locking attribute_name = :version_id
  attribute = integer_attr(attribute_name)
  @optimistic_locking_attr = attribute
end

#order(attribute, direction = :asc) ⇒ Object

Defines the order in which records are returned when performing a find. SimpleDB only allows sorting by one attribute per request.

# oldest to youngest
People.order(:age, :desc).each {|person| ... }

You can chain order with the other scope modifiers:

Pepole.order(:age, :desc).limit(10).each {|person| ... }

Parameters:

  • attribute (String, Symbol)

    The attribute in SimpleDB to sort by.

  • direction (:asc, :desc)

    (:asc) The direction to sort, ascending or descending order.



174
175
176
# File 'lib/aws/record/finder_methods.rb', line 174

def order *args
  _new_scope.order(*args)
end

#scope(name, scope = nil, &block) ⇒ Object

Adding a scope using built-in scope modifiers:

scope :top_10, order(:rating, :desc).limit(10)

Parameters:

  • name (Symbol)

    The name of the scope. Scope names should be method-safe and should not conflict with any of the class methods of Base or Scope.

Raises:

  • (ArgumentError)


235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aws/record/finder_methods.rb', line 235

def scope name, scope = nil, &block
  
  raise ArgumentError, "only a scope or block may be passed, not both" if
    scope and block_given?

  if scope
    method_definition = lambda { scope }
  else
    method_definition = block
  end

  extend(Module.new { define_method(name, &method_definition) })

end

#sdb_domainAWS::SimpleDB::Domain

Returns A reference to the domain this class will save data to.

Returns:



212
213
214
# File 'lib/aws/record/finder_methods.rb', line 212

def sdb_domain
  AWS::SimpleDB.new.domains[domain_name]
end

#set_domain_name(name) ⇒ Object

A configuration method to override the default domain name.

Parameters:

  • The (String)

    domain name that should be used for this class.



199
200
201
# File 'lib/aws/record/finder_methods.rb', line 199

def set_domain_name name
  @_domain_name = name
end

#where(conditions_hash) ⇒ Object #where(sql_fragment[, quote_params, ...]) ⇒ Object

Limits which records are retried from SimpleDB when performing a find.

Simple string condition

Car.where('color = "red" or color = "blue"').each {|car| ... }

String with placeholders for quoting params

Car.where('color = ?', 'red')

Car.where('color = ? OR style = ?', 'red', 'compact')

# produces a condition using in, like: WHERE color IN ('red', 'blue')
Car.where('color = ?', ['red','blue'])

Hash arguments

# WHERE age = '40' AND gender = 'male'
People.where(:age => 40, :gender => 'male').each {|person| ... }

Chaining where with other scope modifiers

# 10 most expensive red cars
Car.where(:color => 'red').order(:price, :desc).limit(10)

Parameters:

  • conditions_hash (Hash)

    A hash of attributes to values. Each key/value pair from the hash becomes a find condition. All conditions are joined by AND.



156
157
158
# File 'lib/aws/record/finder_methods.rb', line 156

def where *args
  _new_scope.where(*args)
end