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 = {}) ⇒ Object
Equivalent to find(:all).
-
#attributes ⇒ Hash
A hash of Attribute objects that represent the “columns” objects in this domain use.
-
#count(options = {}) ⇒ Object
(also: #size)
Counts records in SimpleDB.
-
#create_domain ⇒ Object
Creates the SimpleDB domain that is configured for this class.
-
#domain_name ⇒ String
Returns the full prefixed domain name for this class.
-
#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.
- #optimistic_locking(attribute_name = :version_id) ⇒ Object
-
#order(attribute, direction = :asc) ⇒ Object
Defines the order in which records are returned when performing a find.
-
#scope(name, scope = nil, &block) ⇒ Object
Adding a scope using built-in scope modifiers:.
-
#sdb_domain ⇒ AWS::SimpleDB::Domain
A reference to the domain this class will save data to.
-
#set_domain_name(name) ⇒ Object
A configuration method to override the default domain name.
-
#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
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( = {}) find(:all, ) end |
#attributes ⇒ Hash
Returns 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.
113 114 115 |
# File 'lib/aws/record/finder_methods.rb', line 113 def count( = {}) find(:all).count() end |
#create_domain ⇒ Object
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_name ⇒ 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.
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.
121 122 123 |
# File 'lib/aws/record/finder_methods.rb', line 121 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| ... }
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| ... }
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)
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_domain ⇒ AWS::SimpleDB::Domain
Returns A reference to the domain this class will save data to.
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.
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)
156 157 158 |
# File 'lib/aws/record/finder_methods.rb', line 156 def where *args _new_scope.where(*args) end |