Class: AWS::Record::Scope

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws/record/scope.rb

Overview

Base class for Model::Scope and HashModel::Scope.

Direct Known Subclasses

HashModel::Scope, Model::Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class, options = {}) ⇒ Scope

Returns a new instance of Scope.

Parameters:

  • base_class

    A class that extends AbstractBase.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • : (Object)


27
28
29
30
31
32
33
34
35
36
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(scope_name, *args) ⇒ Object (private)



167
168
169
170
# File 'lib/aws/record/scope.rb', line 167

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_classClass (readonly)

Returns the AWS::Record::Model extending class that this scope will find records for.

Returns:

  • (Class)

    Returns the AWS::Record::Model extending class that this scope will find records for.



40
41
42
# File 'lib/aws/record/scope.rb', line 40

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.

Returns:

  • (Integer)

    Returns the number of records that match the current scoped finder.



104
105
106
107
108
109
110
# 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

#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

Yield Parameters:

  • record (Object)


139
140
141
142
143
144
145
# 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) ⇒ Object #find(: first, options = {}) ⇒ Object? #find(: all, options = {}) ⇒ Scope

Overloads:

  • #find(id) ⇒ Object

    Finds and returns a single record by id. If no record is found with the given id, then a RecordNotFound error will be raised.

    Parameters:

    • id (String)

      ID of the record to find.

    Returns:

    • Returns the record.

  • #find(: first, options = {}) ⇒ Object?

    Returns the first record found. If no records were matched then nil will be returned (raises no exceptions).

    Parameters:

    • mode (Symbol)

      (:first)

    Returns:

    • (Object, nil)

      Returns the first record or nil if no records matched the conditions.

  • #find(: all, options = {}) ⇒ Scope

    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
    

    Parameters:

    • mode (Symbol)

      (:all)

    Returns:

    • (Scope)

      Returns an enumerable scope object.



89
90
91
92
93
94
95
96
97
98
99
100
# 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 = {}) ⇒ Object

Returns the first record found, returns nil if the domain/table is empty.

Returns:

  • Returns the first record found, returns nil if the domain/table is empty.



115
116
117
# File 'lib/aws/record/scope.rb', line 115

def first options = {}
  _handle_options(options).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)

Parameters:

  • limit (Integer)

    The maximum number of records to return.

Returns:

  • (Scope)

    Returns a new scope that has the applied limit.



126
127
128
# File 'lib/aws/record/scope.rb', line 126

def limit limit
  _with(:limit => limit)
end

#new(attributes = {}) ⇒ Object Also known as: build



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

#shard(shard_name) ⇒ Scope Also known as: domain

Returns a scope that specifies which shard (i.e. SimpleDB domain) should be used.

Parameters:

  • shard_name (String)

Returns:

  • (Scope)

    Returns a scope that specifies which shard (i.e. SimpleDB domain) should be used.



60
61
62
# File 'lib/aws/record/scope.rb', line 60

def shard shard_name
  _with(:shard => shard_name)
end