class AWS::Record::Model::Scope
Book.where(‘author’ => ‘John Doe’).first
# terminate a scope by getting the first value
Book.limit(10).each {|book| … }
# terminate a scope by enumerating
To terminate a scope you can enumerate it or call #first.
## Terminating Scopes
* {#limit}
* {#order}
* {#where}
* {#shard}
Each of the following methods returns a scope that can be chained.
end
# yields up to 10 books
books.each do |book|
books = books.limit(10)
books = Book.where(:author => ‘John Doe’)
# no request made by the following 2 statements
until required. This allows you to chain requests
Scope objects represent a request, but do not actualy make a request
## Chaining Scopes
Scopes are also returned from methods defined with the ‘scope` method.
books.class #=> AWS::Record::Scope, not Array
books = Book.where(:author => ’John Doe’)
(e.g. ‘shard`, `where`, `order`, `limit`, etc).
Scope objects are returned from the AWS::Record::Model finder methods
You should normally never need to construct a Scope object directly.
## Getting a Scope Object
The primary interface for finding records with {AWS::Record::Model}.
def _each_object &block
def _each_object &block items = _item_collection items.select.each do |item_data| obj = base_class.new(:shard => _shard) obj.send(:hydrate, item_data.name, item_data.attributes) yield(obj) end end
def _handle_options options
def _handle_options options scope = self options.each_pair do |method, args| if method == :where and args.is_a?(Hash) # splatting a hash turns it into an array, bad juju scope = scope.send(method, args) else scope = scope.send(method, *args) end end scope end
def _item_collection
def _item_collection items = base_class.sdb_domain(_shard).items items = items.order(*@options[:order]) if @options[:order] items = items.limit(*@options[:limit]) if @options[:limit] @options[:where].each do |where_condition| items = items.where(*where_condition) end items end
def _merge_scope scope
def _merge_scope scope merged = self scope.instance_variable_get('@options').each_pair do |opt_name,opt_value| unless [nil, []].include?(opt_value) if opt_name == :where opt_value.each do |condition| merged = merged.where(*condition) end else merged = merged.send(opt_name, *opt_value) end end end merged end
def initialize base_class, options = {}
- Api: - private
def initialize base_class, options = {} super @options[:where] ||= [] end
def new attributes = {}
def new attributes = {} attributes = attributes.dup @options[:where].each do |conditions| if conditions.size == 1 and conditions.first.is_a?(Hash) attributes.merge!(conditions.first) end end super(attributes) end
def order attribute_name, order = :asc
-
order
(:asc, :desc
) -- (:asc) The direct to sort. -
attribute_name
(String, Symbol
) -- The attribute to sort by.
def order attribute_name, order = :asc _with(:order => [attribute_name, order]) end
def where *conditions
-
(Scope)
- Returns a new scope with the passed conditions applied.
Parameters:
-
*values
(String
) -- A value that should be quoted into the -
conditions_string
(String
) -- A sql-like where string with -
conditions
(Hash
) --
Overloads:
-
where(conditions_string, *values)
-
where(conditions_hash)
def where *conditions if conditions.empty? raise ArgumentError, 'missing required condition' end _with(:where => @options[:where] + [conditions]) end