Class: AWS::Record::Scope
- Inherits:
-
Object
- Object
- AWS::Record::Scope
- Includes:
- Enumerable
- Defined in:
- lib/aws/record/scope.rb
Overview
Base class for Model::Scope and HashModel::Scope.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base_class ⇒ Class
readonly
Returns the AWS::Record::Model 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.
-
#each {|record| ... } ⇒ Object
Yields once for each record matching the request made by this scope.
- #find(id_or_mode, options = {}) ⇒ Object
-
#first(options = {}) ⇒ Object
Returns the first record found, returns nil if the domain/table is empty.
-
#limit(limit) ⇒ Scope
Limits the maximum number of total records to return when finding or counting.
- #new(attributes = {}) ⇒ Object (also: #build)
-
#shard(shard_name) ⇒ Scope
(also: #domain)
Returns a scope that specifies which shard (i.e. SimpleDB domain) should be used.
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_class ⇒ Class (readonly)
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.
104 105 106 107 108 109 110 |
# File 'lib/aws/record/scope.rb', line 104 def count = {} if scope = () 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
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 to_enum(:"_each_object") end end |
#find(id) ⇒ Object #find(: first, options = {}) ⇒ Object? #find(: all, options = {}) ⇒ Scope
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, = {} 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, :shard => scope._shard) end end |
#first(options = {}) ⇒ Object
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 = {} ().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)
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.
60 61 62 |
# File 'lib/aws/record/scope.rb', line 60 def shard shard_name _with(:shard => shard_name) end |