Class: AWS::SimpleDB::ItemCollection
- Inherits:
-
Object
- Object
- AWS::SimpleDB::ItemCollection
- Includes:
- Core::Collection::WithLimitAndNextToken
- Defined in:
- lib/aws/simple_db/item_collection.rb
Overview
Represents a collection of items in a SimpleDB domain.
Instance Attribute Summary collapse
-
#domain ⇒ Domain
readonly
The domain the items belong to.
Instance Method Summary collapse
-
#[](item_name) ⇒ Item
Retuns an item with the given name.
-
#count(options = {}, &block) ⇒ Integer
(also: #size)
Counts the items in the collection.
-
#create(item_name, attributes) ⇒ Item
Creates a new item in SimpleDB with the given attributes: .
-
#each(options = {}) {|item| ... } ⇒ String?
Yields to the block once for each item in the collection.
- #initialize(domain, options = {}) ⇒ ItemCollection constructor
-
#limit(*args) ⇒ Object
(also: #_limit)
Limits the number of items that are returned or yielded.
-
#order(attribute, order = nil) ⇒ ItemCollection
Changes the order in which results are returned or yielded.
-
#select(*attributes, &block) ⇒ ItemCollection
Specifies a list of attributes select from SimpleDB.
-
#where(conditions, *substitutions) ⇒ ItemCollection
Returns an item collection defined by the given conditions in addition to any conditions defined on this collection.
Methods included from Core::Collection
#enum, #first, #in_groups_of, #page
Constructor Details
#initialize(domain, options = {}) ⇒ ItemCollection
49 50 51 52 53 54 55 56 57 |
# File 'lib/aws/simple_db/item_collection.rb', line 49 def initialize domain, = {} @domain = domain @output_list = [:output_list] || 'itemName()' @conditions = [:conditions] || [] @sort_instructions = [:sort_instructions] @not_null_attribute = [:not_null_attribute] @limit = [:limit] super end |
Instance Attribute Details
#domain ⇒ Domain (readonly)
Returns The domain the items belong to.
36 37 38 |
# File 'lib/aws/simple_db/item_collection.rb', line 36 def domain @domain end |
Instance Method Details
#[](item_name) ⇒ Item
This does not make a request to SimpleDB.
Retuns an item with the given name.
You can ask for any item. The named item may or may not actually exist in SimpleDB.
91 92 93 |
# File 'lib/aws/simple_db/item_collection.rb', line 91 def [] item_name Item.new(domain, item_name.to_s) end |
#count(options = {}, &block) ⇒ Integer Also known as: size
Counts the items in the collection.
domain.items.count
You can specify what items to count with #where:
domain.items.where(:color => "red").count
You can also limit the number of items to count:
# count up to 500 items and then stop
domain.items.limit(500).count
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/aws/simple_db/item_collection.rb', line 205 def count = {}, &block () do |collection, opts| return collection.count(opts, &block) end = .merge(:output_list => "count(*)") count = 0 next_token = nil begin response = select_request(, next_token) if domain_item = response.items.first and count_attribute = domain_item.attributes.first then count += count_attribute.value.to_i end break unless next_token = response[:next_token] end while limit.nil? || count < limit count end |
#create(item_name, attributes) ⇒ Item
Creates a new item in SimpleDB with the given attributes:
domain.items.create('shirt', {
'colors' => ['red', 'blue'],
'category' => 'clearance'})
71 72 73 74 75 |
# File 'lib/aws/simple_db/item_collection.rb', line 71 def create item_name, *args item = self[item_name] item.attributes.replace(*args) item end |
#each(options = {}) {|item| ... } ⇒ String?
Yields to the block once for each item in the collection. This method can yield two type of objects:
-
AWS::SimpleDB::Item objects (only the item name is populated)
-
AWS::SimpleDB::ItemData objects (some or all attributes populated)
The default mode of an ItemCollection is to yield Item objects with no populated attributes.
# only receives item names from SimpleDB
domain.items.each do |item|
puts item.name
puts item.class.name # => AWS::SimpleDB::Item
end
You can switch a collection into yielded AWS::SimpleDB::ItemData objects by specifying what attributes to request:
domain.items.select(:all).each do |item_data|
puts item_data.class.name # => AWS::SimpleDB::ItemData
puts item_data.attributes # => { 'attr-name' => 'attr-value', ... }
end
You can also pass the standard scope options to #each as well:
# output the item names of the 10 most expesive items
domain.items.each(:order => [:price, :desc], :limit => 10).each do |item|
puts item.name
end
167 168 169 |
# File 'lib/aws/simple_db/item_collection.rb', line 167 def each = {}, &block super end |
#limit ⇒ Integer #limit(value) ⇒ ItemCollection Also known as: _limit
Limits the number of items that are returned or yielded. For example, to get the 100 most popular item names:
domain.items.
order(:popularity, :desc).
limit(100).
map(&:name)
446 447 448 449 |
# File 'lib/aws/simple_db/item_collection.rb', line 446 def limit *args return @limit if args.empty? collection_with(:limit => Integer(args.first)) end |
#order(attribute, order = nil) ⇒ ItemCollection
Changes the order in which results are returned or yielded. For example, to get item names in descending order of popularity, you can do:
domain.items.order(:popularity, :desc).map(&:name)
424 425 426 427 428 429 430 |
# File 'lib/aws/simple_db/item_collection.rb', line 424 def order(attribute, order = nil) sort = coerce_attribute(attribute) sort += " DESC" if order.to_s =~ /^desc(ending)?$/ sort += " ASC" if order.to_s =~ /^asc(ending)?$/ collection_with(:sort_instructions => sort, :not_null_attribute => attribute.to_s) end |
#select(*attributes, &block) ⇒ ItemCollection
Specifies a list of attributes select from SimpleDB.
domain.items.select('size', 'color').each do |item_data|
puts item_data.attributes # => { 'size' => ..., :color => ... }
end
You can select all attributes by passing :all
or ‘*’:
domain.items.select('*').each {|item_data| ... }
domain.items.select(:all).each {|item_data| ... }
Calling #select causes #each to yield AWS::SimpleDB::ItemData objects with #attribute hashes, instead of AWS::SimpleDB::Item objects with an item name.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/aws/simple_db/item_collection.rb', line 282 def select *attributes, &block # Before select was morphed into a chainable method, it accepted # a hash of options (e.g. :where, :order, :limit) that no longer # make sense, but to maintain backwards compatability we still # consume those. # # TODO : it would be a good idea to add a deprecation warning for # passing options to #select # (*attributes) do |collection, *args| return collection.select(*args, &block) end = attributes.last.is_a?(Hash) ? attributes.pop : {} output_list = case attributes.flatten when [] then '*' when ['*'] then '*' when [:all] then '*' else attributes.flatten.map{|attr| coerce_attribute(attr) }.join(', ') end collection = collection_with(:output_list => output_list) if block_given? # previously select accepted a block and it would enumerate items # this is for backwards compatability collection.each(, &block) nil else collection end end |
#where(conditions, *substitutions) ⇒ ItemCollection
Returns an item collection defined by the given conditions in addition to any conditions defined on this collection. For example:
items = domain.items.where(:color => 'blue').
where('engine_type is not null')
# does SELECT itemName() FROM `mydomain`
# WHERE color = "blue" AND engine_type is not null
items.each { |i| ... }
Hash Conditions
When conditions
is a hash, each entry produces a condition on the attribute named in the hash key. For example:
# produces "WHERE `foo` = 'bar'"
domain.items.where(:foo => 'bar')
You can pass an array value to use an “IN” operator instead of “=”:
# produces "WHERE `foo` IN ('bar', 'baz')"
domain.items.where(:foo => ['bar', 'baz'])
You can also pass a range value to use a “BETWEEN” operator:
# produces "WHERE `foo` BETWEEN 'bar' AND 'baz'
domain.items.where(:foo => 'bar'..'baz')
# produces "WHERE (`foo` >= 'bar' AND `foo` < 'baz')"
domain.items.where(:foo => 'bar'...'baz')
Placeholders
If conditions
is a string and “?” appears outside of any quoted part of the expression, placeholers
is expected to contain a value for each of the “?” characters in the expression. For example:
# produces "WHERE foo like 'fred''s % value'"
domain.items.where("foo like ?", "fred's % value")
Array values are surrounded with parentheses when they are substituted for a placeholder:
# produces "WHERE foo in ('1', '2')"
domain.items.where("foo in ?", [1, 2])
Note that no substitutions are made within a quoted region of the query:
# produces "WHERE `foo?` = 'red'"
domain.items.where("`foo?` = ?", "red")
# produces "WHERE foo = 'fuzz?' AND bar = 'zap'"
domain.items.where("foo = 'fuzz?' AND bar = ?", "zap")
Also note that no attempt is made to correct for syntax:
# produces "WHERE 'foo' = 'bar'", which is invalid
domain.items.where("? = 'bar'", "foo")
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/aws/simple_db/item_collection.rb', line 384 def where conditions, *substitutions case conditions when String conditions = [replace_placeholders(conditions, *substitutions)] when Hash conditions = conditions.map do |name, value| name = coerce_attribute(name) case value when Array "#{name} IN " + coerce_substitution(value) when Range if value.exclude_end? "(#{name} >= #{coerce_substitution(value.begin)} AND " + "#{name} < #{coerce_substitution(value.end)})" else "#{name} BETWEEN #{coerce_substitution(value.begin)} AND " + coerce_substitution(value.end) end else "#{name} = " + coerce_substitution(value) end end end collection_with(:conditions => self.conditions + conditions) end |