Class: AWS::Record::Scope
- Inherits:
-
Object
- Object
- AWS::Record::Scope
- Includes:
- Enumerable
- Defined in:
- lib/aws/record/scope.rb
Overview
The primary interface for finding records with AWS::Record.
Getting a Scope Object
You should normally never need to construct a Scope object directly. Scope objects are returned from the AWS::Record::Base finder methods (e.g. find
all
, where
, order
, limit
, etc).
books = Book.where(:author => 'John Doe')
books.class #=> AWS::Record::Scope, not Array
Scopes are also returned from methods defined with the scope
method.
Delayed Execution
Scope objects represent a select expression, but do not actually cause a request to be made until enumerated.
# no request made yet
books = Book.where(:author => 'John Doe')
# a request is made now
books.each {|book| ... }
You can refine a scope object by calling other scope methods on it.
# refine the previous books Scope, no request
top_10 = books.order(:popularity, :desc).limit(10)
# another request is made now
top_10.first
Instance Attribute Summary collapse
-
#base_class ⇒ Class
readonly
Returns the AWS::Record::Base extending class that this scope will find records for.
Instance Method Summary collapse
-
#count(options = {}) ⇒ Integer
(also: #size)
Returns the number of records that match the current scoped finder.
-
#domain(name) ⇒ Scope
Returns a scope for restricting the domain of subsequent scope operations.
-
#each {|record| ... } ⇒ Object
Yields once for each record matching the request made by this scope.
- #find(id_or_mode, options = {}) ⇒ Object
-
#first(options = {}) ⇒ Record::Base?
Gets the first record from the domain and returns it, or returns nil if the domain is empty.
-
#limit(limit) ⇒ Scope
Limits the maximum number of total records to return when finding or counting.
- #new(attributes = {}) ⇒ Object (also: #build)
-
#order(attribute_name, order = :asc) ⇒ Object
Specifies how to sort records returned.
-
#where(*conditions) ⇒ Scope
Applies conditions to the scope that limit which records are returned.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(scope_name, *args) ⇒ Object (private)
258 259 260 261 |
# File 'lib/aws/record/scope.rb', line 258 def method_missing scope_name, *args # @todo only proxy named scope methods _merge_scope(base_class.send(scope_name, *args)) end |
Instance Attribute Details
#base_class ⇒ Class (readonly)
Returns the AWS::Record::Base extending class that this scope will find records for.
68 69 70 |
# File 'lib/aws/record/scope.rb', line 68 def base_class @base_class end |
Instance Method Details
#count(options = {}) ⇒ Integer Also known as: size
Returns the number of records that match the current scoped finder.
135 136 137 138 139 140 141 |
# File 'lib/aws/record/scope.rb', line 135 def count = {} if scope = () and scope != self scope.count else _item_collection.count end end |
#domain(name) ⇒ Scope
Returns a scope for restricting the domain of subsequent scope operations
92 93 94 |
# File 'lib/aws/record/scope.rb', line 92 def domain name _with(:domain => name) end |
#each {|record| ... } ⇒ Object
Yields once for each record matching the request made by this scope.
books = Book.where(:author => 'me').order(:price, :asc).limit(10)
books.each do |book|
puts book.attributes.to_yaml
end
223 224 225 226 227 228 229 |
# File 'lib/aws/record/scope.rb', line 223 def each &block if block_given? _each_object(&block) else Enumerator.new(self, :"_each_object") end end |
#find(id) ⇒ Record::Base #find(: first, options = {}) ⇒ Object? #find(: all, options = {}) ⇒ Scope
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/aws/record/scope.rb', line 120 def find id_or_mode, = {} scope = () case when id_or_mode == :all then scope when id_or_mode == :first then scope.limit(1).to_a.first else base_class.find_by_id(id_or_mode, :domain => scope._domain) end end |
#first(options = {}) ⇒ Record::Base?
Returns Gets the first record from the domain and returns it, or returns nil if the domain is empty.
146 147 148 |
# File 'lib/aws/record/scope.rb', line 146 def first = {} ().find(:first) end |
#limit(limit) ⇒ Scope
Limits the maximum number of total records to return when finding or counting. Returns a scope, does not make a request.
books = Book.limit(100)
210 211 212 |
# File 'lib/aws/record/scope.rb', line 210 def limit limit _with(:limit => limit) end |
#new(attributes = {}) ⇒ Object Also known as: build
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/aws/record/scope.rb', line 70 def new attributes = {} = {} .merge!(attributes) unless .key?(:domain) or .key?('domain') [:domain] = _domain end @options[:where].each do |conditions| if conditions.size == 1 and conditions.first.is_a?(Hash) .merge!(conditions.first) end end base_class.new() end |
#order(attribute_name, order = :asc) ⇒ Object
Specifies how to sort records returned.
# enumerate books, starting with the most recently published ones
Book.order(:published_at, :desc).each do |book|
# ...
end
Only one order may be applied. If order is specified more than once the last one in the chain takes precedence:
# books returned by this scope will be ordered by :published_at
# and not :author.
Book.where(:read => false).order(:author).order(:published_at)
199 200 201 |
# File 'lib/aws/record/scope.rb', line 199 def order attribute_name, order = :asc _with(:order => [attribute_name, order]) end |
#where(conditions_hash) ⇒ Scope #where(conditions_string, *values) ⇒ Scope
Applies conditions to the scope that limit which records are returned. Only those matching all given conditions will be returned.
175 176 177 178 179 180 |
# File 'lib/aws/record/scope.rb', line 175 def where *conditions if conditions.empty? raise ArgumentError, 'missing required condition' end _with(:where => @options[:where] + [conditions]) end |