Class: AWS::SimpleDB::AttributeCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Model, ConsistentReadOption, DeleteAttributes, PutAttributes, Enumerable
Defined in:
lib/aws/simple_db/attribute_collection.rb

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from ConsistentReadOption

#consistent_read

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(item, options = {}) ⇒ AttributeCollection

Parameters:

  • item (Item)

    The item to create an attribute collection for.



26
27
28
29
# File 'lib/aws/simple_db/attribute_collection.rb', line 26

def initialize item, options = {}
  @item = item
  super
end

Instance Attribute Details

#itemItem (readonly)

Returns The item this collection belongs to.

Returns:

  • (Item)

    The item this collection belongs to.



32
33
34
# File 'lib/aws/simple_db/attribute_collection.rb', line 32

def item
  @item
end

Instance Method Details

#[](attribute_name) ⇒ Item

Note:

This does not make a request to SimpleDB.

Retuns an Attribute with the given name.

You can ask for any attribute by name. The attribute may or may not actually exist in SimpleDB.

Examples:

Get an attribute by symbol or string name

colors = item.attributes[:colors]
colors = item.attributes['colors']

Parameters:

  • attribute_name (String, Symbol)

    name of the attribute to get.

Returns:

  • (Item)

    An item with the given name.



46
47
48
# File 'lib/aws/simple_db/attribute_collection.rb', line 46

def [] attribute_name
  Attribute.new(item, attribute_name.to_s)
end

#[]=(attribute_name, *values) ⇒ Object

Sets the values for a given attribute.

Examples:

Replace all of the values for the named attribute.

item.attributes[:color] = 'red', 'blue'

Returns:

  • This method returns the values passed to it.



55
56
57
# File 'lib/aws/simple_db/attribute_collection.rb', line 55

def []= attribute_name, *values
  self[attribute_name].set(*values)
end

#add(attributes) ⇒ nil

Adds values to attributes on the #item.

The attributes_hash should have attribute names as keys. The hash values should be either strings or arrays of strings.

@param attribute_hash

Examples:


item.attributes.add(
  'colors' => ['red', 'blue'],  
  'category' => 'clearance')

Returns:

  • (nil)


154
155
156
# File 'lib/aws/simple_db/attribute_collection.rb', line 154

def add attributes
  do_put(attribute_hashes(attributes, false), attributes)
end

#delete(attributes) ⇒ nil #delete(*attribute_names) ⇒ nil

Delete one or more attributes from #item.

Examples:

Delete a list of attributes by name (accepts a list or array)

item.attributes.delete 'size', 'color'
item.attributes.delete %w(size color)

Delete a specific list of attribute values

item.attributes.delete(:color => 'red', :tags => %w(special limited))

Overloads:

  • #delete(attributes) ⇒ nil

    Parameters:

    • attributes (Hash)

      A hash of attribute names and values to delete.

    Returns:

    • (nil)
  • #delete(*attribute_names) ⇒ nil

    Parameters:

    • A (String)

      list of attribute names to delete.

    Returns:

    • (nil)


220
221
222
223
224
225
226
227
# File 'lib/aws/simple_db/attribute_collection.rb', line 220

def delete *args
  if args.size == 1 and args.first.kind_of?(Hash)
    delete_attribute_values(args.first)
  else
    delete_named_attributes(*args)
  end
  nil
end

#each(options = {}) {|attribute| ... } ⇒ nil

Yields all attribute for this item.

Examples:

Getting all attributes for an item


item.attributes.each do |attribute|
  puts attribute.name
end

Parameters:

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

Options Hash (options):

  • :consistent_read (Boolean) — default: false

    Causes this method to yield the most current attributes for this item.

Yields:

  • (attribute)

    Yields once for every attribute on the item. Yields each attribute only one time, even it has multiple values.

Yield Parameters:

Returns:

  • (nil)


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aws/simple_db/attribute_collection.rb', line 108

def each options = {}, &block
  yielded = {}
  each_value(options) do |attribute_name, attribute_value| 
    unless yielded[attribute_name]
      attribute = self[attribute_name]
      yield(attribute)
      yielded[attribute_name] = true
    end
  end
  nil
end

#each_value(options = {}) {|attribute_name, attribute_value| ... } ⇒ nil

Yields all attribute values with their names.

Examples:

Getting all values for an item


item.attributes.each_value do |name, value|
  puts "#{name}: #{value}"
end

Parameters:

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

Options Hash (options):

  • :consistent_read (Boolean) — default: false

    Causes this method to yield the most current attributes for this item.

Yields:

  • (attribute_name, attribute_value)

    Yields once for every attribute value on the item.

Yield Parameters:

  • attribute_name (String)
  • attribute_value (String)

Returns:

  • (nil)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/aws/simple_db/attribute_collection.rb', line 75

def each_value options = {}, &block

  list = client.get_attributes(
    :domain_name => item.domain.name,
    :item_name => item.name,
    :consistent_read => consistent_read(options))

  list.attributes.each do |attribute|
    attribute_name = attribute.name
    attribute_value = attribute.value
    yield(attribute_name, attribute_value)
  end

  nil

end

#put(options = {}) ⇒ nil

Perform a mixed update of added and replace attribues.

item.attributes.put(
  :add => { 'colors' => %w(green blue), 'tags' => 'cool' }
  :replace => { 'quantity' => 5 }
)

Parameters:

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

Options Hash (options):

  • :add (Hash)

    A hash of attribute names and values to append to this item.

  • :replace (Hash)

    A hash of attribute names and values to add to this item. If there are currently attributes of the same name they will be replaced (not appended to).

  • :replace (Hash)

Returns:

  • (nil)


173
174
175
176
177
178
179
# File 'lib/aws/simple_db/attribute_collection.rb', line 173

def put options = {}
  add = options[:add] || {}
  replace = options[:replace] || {}
  attributes = attribute_hashes(add, false)
  attributes += attribute_hashes(replace, true)
  do_put(attributes, options)
end

#replace(attributes) ⇒ nil Also known as: set

Replaces attributes for the #item.

The attributes_hash should have attribute names as keys. The hash values should be either strings or arrays of strings.

Attributes not named in this hash are left alone. Attributes named in this hash are replaced.

Examples:


item.attributes.set(
  'colors' => ['red', 'blue'],  
  'category' => 'clearance')

Parameters:

  • attributes (Hash)

Returns:

  • (nil)


136
137
138
# File 'lib/aws/simple_db/attribute_collection.rb', line 136

def replace attributes
  do_put(attribute_hashes(attributes, true), attributes)
end

#to_h(options = {}) ⇒ Hash

Returns a hash of all attributes (names and values). The attribute names are strings and the values are arrays of strings.

Examples:

item.attributes.to_h
#=> { 'colors' => ['red','blue'], 'size' => ['large'] }

Parameters:

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

Options Hash (options):

  • :consistent_read (Boolean) — default: false

    Causes this method to return the most current attributes values.

Returns:

  • (Hash)


193
194
195
196
197
198
199
200
# File 'lib/aws/simple_db/attribute_collection.rb', line 193

def to_h options = {}
  hash = {}
  each_value(options) do |attribute_name,attribute_value|
    hash[attribute_name] ||= []
    hash[attribute_name] << attribute_value
  end
  hash
end