Module: AWS::Record::FinderMethods
- Included in:
- Base
- Defined in:
- lib/aws/record/finder_methods.rb
Instance Method Summary collapse
-
#[](id) ⇒ Record::Base
Returns the record, as a sub-class of Record::Base.
-
#all(options = {}) ⇒ Scope
Returns an enumerable scope object represents all records.
-
#count(options = {}) ⇒ Object
(also: #size)
Counts records in SimpleDB.
-
#find(*args) ⇒ Object
Finds records in SimpleDB and returns them as objects of the current class.
-
#first(options = {}) ⇒ Object?
Returns the first record found for the current class.
-
#limit(limit) ⇒ Object
The maximum number of records to return.
-
#order(attribute, direction = :asc) ⇒ Object
Defines the order in which records are returned when performing a find.
-
#where(*args) ⇒ Object
Limits which records are retried from SimpleDB when performing a find.
Instance Method Details
#[](id) ⇒ Record::Base
Returns the record, as a sub-class of Record::Base
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/aws/record/finder_methods.rb', line 27 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 = {}) ⇒ Scope
Returns an enumerable scope object represents all records.
Book.all.each do |book|
# ...
end
This method is equivalent to find(:all), and therefore you can also pass aditional options. See #find for more information on what options you can pass.
Book.all(:where => { :author' => 'me' }).each do |my_book|
# ...
end
102 103 104 |
# File 'lib/aws/record/finder_methods.rb', line 102 def all = {} find(:all, ) 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.
128 129 130 |
# File 'lib/aws/record/finder_methods.rb', line 128 def count( = {}) find(:all).count() 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.
83 84 85 |
# File 'lib/aws/record/finder_methods.rb', line 83 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.
136 137 138 |
# File 'lib/aws/record/finder_methods.rb', line 136 def first = {} _new_scope.find(:first, ) 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| ... }
202 203 204 |
# File 'lib/aws/record/finder_methods.rb', line 202 def limit limit _new_scope.limit(limit) 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| ... }
189 190 191 |
# File 'lib/aws/record/finder_methods.rb', line 189 def order *args _new_scope.order(*args) 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)
171 172 173 |
# File 'lib/aws/record/finder_methods.rb', line 171 def where *args _new_scope.where(*args) end |