Class: AWS::SimpleDB::ItemCollection
- Inherits:
-
Object
- Object
- AWS::SimpleDB::ItemCollection
- Includes:
- Enumerable
- Defined in:
- lib/aws/simple_db/item_collection.rb
Overview
Represents a collection of items in a SimpleDB domain.
Constant Summary collapse
- OUTSIDE_QUOTES_REGEX =
Identifies quoted regions in the string, giving access to the regions before and after each quoted region, for example:
"? ? `foo?``bar?` ? 'foo?' ?".scan(OUTSIDE_QUOTES_REGEX) # => [["? ? ", "`foo?``bar?`", " ? "], ["", "'foo?'", " ?"]]
Regexp.compile('([^\'"`]*)(`(?:[^`]*(?:``))*[^`]*`|'+ '\'(?:[^\']*(?:\'\'))*[^\']*\'|'+ '"(?:[^"]*(?:""))*[^"]*")([^\'`"]*)')
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) ⇒ Object
(also: #size)
Counts the items in the collection.
-
#create(item_name, attribute_hash) ⇒ Item
Creates a new item in SimpleDB with the given attributes: .
-
#each(options = {}) {|item| ... } ⇒ nil
Yields to the block once for each item in the domain.
- #initialize(domain, options = {}) ⇒ ItemCollection constructor
-
#limit(*args) ⇒ Object
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(*attribute_names, options = {}) ⇒ Object
Retrieves data from each item in the domain.
-
#where(conditions, *substitutions) ⇒ ItemCollection
Returns an item collection defined by the given conditions in addition to any conditions defined on this collection.
Constructor Details
#initialize(domain, options = {}) ⇒ ItemCollection
40 41 42 43 44 45 46 47 48 |
# File 'lib/aws/simple_db/item_collection.rb', line 40 def initialize domain, = {} @domain = domain @conditions = [] @conditions += [:conditions] if [:conditions] @sort_instructions = [:sort_instructions] if [:sort_instructions] @not_null_attribute = [:not_null_attribute] @limit = [:limit] if [:limit] super end |
Instance Attribute Details
#domain ⇒ Domain (readonly)
Returns The domain the items belong to.
51 52 53 |
# File 'lib/aws/simple_db/item_collection.rb', line 51 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.
85 86 87 |
# File 'lib/aws/simple_db/item_collection.rb', line 85 def [] item_name Item.new(domain, item_name.to_s) end |
#count(options = {}, &block) ⇒ Object Also known as: size
Counts the items in the collection.
domain.items.count
You can use this method to get the total number of items in the domain, or you can use it with #where to count a subset of items. For example, to count the items where the “color” attribute is “red”:
domain.items.where("color" => "red").count
You can also limit the number of items searched using the #limit method. For example, to count the number of items up to 500:
domain.items.limit(500).count
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/aws/simple_db/item_collection.rb', line 240 def count = {}, &block return if (:count, , &block) = .merge(:output_list => "count(*)") count = 0 next_token = nil while limit.nil? || count < limit and response = select_request(, next_token) if domain_item = response.items.first and count_attribute = domain_item.attributes.first count += count_attribute.value.to_i end next_token = response.next_token break unless next_token end count end |
#create(item_name, attribute_hash) ⇒ Item
Creates a new item in SimpleDB with the given attributes:
domain.items.create('shirt', {
'colors' => ['red', 'blue'],
'category' => 'clearance'})
65 66 67 68 69 |
# File 'lib/aws/simple_db/item_collection.rb', line 65 def create item_name, *args item = self[item_name] item.attributes.replace(*args) item end |
#each(options = {}) {|item| ... } ⇒ nil
Yields to the block once for each item in the domain.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/aws/simple_db/item_collection.rb', line 108 def each = {}, &block return if (:each, , &block) if attributes = .delete(:select) return select(attributes, , &block) end perform_select() do |response| response.items.each do |item| yield(self[item.name]) end end end |
#limit ⇒ Integer #limit(value) ⇒ ItemCollection
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)
398 399 400 401 |
# File 'lib/aws/simple_db/item_collection.rb', line 398 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)
378 379 380 381 382 383 384 |
# File 'lib/aws/simple_db/item_collection.rb', line 378 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(*attribute_names, options = {}) ⇒ Object
Retrieves data from each item in the domain.
domain.items.select('size', 'color')
You may optionally filter by a set of conditions. For example, to retrieve the attributes of each of the top 100 items in order of descending popularity as an array of hashes, you could do:
items.order(:popularity, :desc).limit(100).select do |data|
puts data.to_yaml
end
You can select specific attributes; for example, to get all the unique colors in the collection you could do:
colors = Set.new
items.select(:color) {|data| colors += data.attributes["color"] }
Finally, you can specify conditions, sort instructions, and a limit in the same method call:
items.select(:color,
:where => "rating > 4",
:order => [:popularity, :desc],
:limit => 100) do |data|
puts "Data for #{data.name}: #{data.attributes.inspect}"
end
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/aws/simple_db/item_collection.rb', line 182 def select *attributes, &block = attributes.last.is_a?(Hash) ? attributes.pop : {} args = attributes + [] return if (:select, *args, &block) unless block_given? return Enumerator.new(self, :select, *args) end if attributes.empty? output_list = '*' #elsif attributes == ['*'] # output_list = '*' else output_list = [attributes].flatten.collect do |attr| coerce_attribute(attr) end.join(', ') end perform_select(.merge(:output_list => output_list)) do |response| response.items.each do |item| yield(ItemData.new(:domain => domain, :response_object => item)) end end nil 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")
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/aws/simple_db/item_collection.rb', line 338 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 |