Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

[View source]

171
172
173
174
# File 'lib/aws/record/scope.rb', line 171

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.

[View source]

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.

Examples:


books = Book.where(:author => 'me').order(:price, :asc).limit(10)

books.each do |book|
  puts book.attributes.to_yaml
end

Yield Parameters:

  • record (Object)
[View source]

143
144
145
146
147
148
149
# File 'lib/aws/record/scope.rb', line 143

def each &block
  if block_given?
    _each_object(&block)
  else
    to_enum(:_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.

[View source]

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.

[View source]

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.

Examples:


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.

[View source]

128
129
130
# File 'lib/aws/record/scope.rb', line 128

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

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

[View source]

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.

[View source]

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

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