Base class for {AWS::Record::Model::Scope} and {AWS::Record::HashModel::Scope}.
@return [Class] Returns the AWS::Record::Model extending class that
this scope will find records for.
@param #base_class A class that extends {AWS::Record::AbstractBase}. @param [Hash] options @option options : @private
# File lib/aws/record/scope.rb, line 27 def initialize base_class, options = {} @base_class = base_class @options = options.dup # backwards compat @options[:shard] = @options.delete(:domain) if @options[:domain] end
@return [Integer] Returns the number of records that match the
current scoped finder.
# File lib/aws/record/scope.rb, line 104 def count options = {} if scope = _handle_options(options) and scope != self scope.count else _item_collection.count end end
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
@yieldparam [Object] record
# File lib/aws/record/scope.rb, line 139 def each &block if block_given? _each_object(&block) else Enumerator.new(self, :"_each_object") end end
@overload find(id)
Finds and returns a single record by id. If no record is found with the given +id+, then a RecordNotFound error will be raised. @param [String] id ID of the record to find. @return Returns the record.
@overload find(:first, options = {})
Returns the first record found. If no records were matched then nil will be returned (raises no exceptions). @param [Symbol] mode (:first) @return [Object,nil] Returns the first record or nil if no records matched the conditions.
@overload find(:all, options = {})
Returns an enumerable Scope object that represents all matching records. No request is made to AWS until the scope is enumerated. Book.find(:all, :limit => 100).each do |book| # ... end @param [Symbol] mode (:all) @return [Scope] Returns an enumerable scope object.
# File lib/aws/record/scope.rb, line 89 def find id_or_mode, options = {} scope = _handle_options(options) 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, :shard => scope._shard) end end
@return Returns the first record found, returns
nil if the domain/table is empty.
# File lib/aws/record/scope.rb, line 115 def first options = {} _handle_options(options).find(:first) end
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)
@param [Integer] limit The maximum number of records to return. @return [Scope] Returns a new scope that has the applied limit.
# File lib/aws/record/scope.rb, line 126 def limit limit _with(:limit => limit) end
# File lib/aws/record/scope.rb, line 42 def new attributes = {} attributes = attributes.dup attributes[:shard] ||= attributes.delete(:shard) attributes[:shard] ||= attributes.delete('shard') # for backwards compatability, domain is accepted attributes[:shard] ||= attributes.delete('domain') attributes[:shard] ||= attributes.delete(:domain) attributes[:shard] ||= _shard base_class.new(attributes) end
@param [String] shard_name @return [Scope] Returns a scope that specifies which shard
(i.e. SimpleDB domain) should be used.
# File lib/aws/record/scope.rb, line 60 def shard shard_name _with(:shard => shard_name) end
# File lib/aws/record/scope.rb, line 148 def _shard @options[:shard] || base_class.shard_name end