class AWS::Record::Scope

Base class for {AWS::Record::Model::Scope} and {AWS::Record::HashModel::Scope}.

Attributes

base_class[R]

@return [Class] Returns the AWS::Record::Model extending class that

this scope will find records for.

Public Class Methods

new(base_class, options = {}) click to toggle source

@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

Public Instance Methods

build(attributes = {}) click to toggle source
Alias for: new
count(options = {}) click to toggle source

@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
Also aliased as: size
domain(shard_name) click to toggle source
Alias for: shard
each(&block) click to toggle source

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
find(id_or_mode, options = {}) click to toggle source

@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
first(options = {}) click to toggle source

@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
limit(limit) click to toggle source

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
new(attributes = {}) click to toggle source
# 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
Also aliased as: build
shard(shard_name) click to toggle source

@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
Also aliased as: domain, domain
size(options = {}) click to toggle source
Alias for: count

Protected Instance Methods

_shard() click to toggle source
# File lib/aws/record/scope.rb, line 148
def _shard
  @options[:shard] || base_class.shard_name
end