class AWS::SimpleDB::Attribute

Represents a single named item attribute in SimpleDB.

Attributes

item[R]

@return [Item] The item this attribute belongs to.

name[R]

@return [String] The name of this attribute.

Public Class Methods

new(item, name, options = {}) click to toggle source

@private

# File lib/aws/simple_db/attribute.rb, line 27
def initialize item, name, options = {}
  @item = item
  @name = name
  super
end

Public Instance Methods

<<(*values) click to toggle source
Alias for: add
add(*values) click to toggle source

Appends values to this attribute. Duplicate values are ignored by SimpleDB.

@example Adding a list of values

attributes['colors'].add 'red', 'blue', 'green'

@example Adding an array of values

attributes['colors'].add ['red', 'blue']

@param *values An array or list of attribute values to add. @return [nil]

# File lib/aws/simple_db/attribute.rb, line 67
def add *values
  put(values, false)
  nil
end
Also aliased as: <<
delete(*values) click to toggle source

Deletes this attribute or specific values from this attribute.

@example Delete the attribute and all of its values

item.attributes['color'].delete

@example Delete specific attribute values

item.attributes['color'].delete('red', 'blue')

@param values One ore more values to remove from this attribute.

If values is empty, then all attribute values are deleted 
(which deletes this attribute).

@return [nil]

# File lib/aws/simple_db/attribute.rb, line 87
def delete *values
  expect_opts = values.pop if values.last.kind_of?(Hash)

  if values.empty?
    delete_named_attributes(name, expect_opts || {})
  else
    delete_attribute_values(Hash[[[name, values]]].
                            merge(expect_opts || {}))
  end
  nil
end
each(options = {}) { |value| ... } click to toggle source

Yields once for each value on this attribute.

@yield [attribute_value] Yields once for each domain in the account. @yieldparam [String] attribute_value @param [Hash] options @option options [Boolean] :consistent_read (false) A consistent read

returns values that reflects all writes that received a successful
response prior to the read.

@return [nil]

# File lib/aws/simple_db/attribute.rb, line 108
def each options = {}, &block

  resp = client.get_attributes(
    :domain_name => item.domain.name,
    :item_name => item.name,
    :attribute_names => [name],
    :consistent_read => consistent_read(options))

  resp.attributes.each do |attribute|
    yield(attribute.value)
  end

  nil

end
set(*values) click to toggle source

Sets all values for this attribute, replacing current values.

@example Setting a list of values

attributes['colors'].set 'red', 'blue', 'green'

@example Setting an array of values

attributes['colors'].set ['red', 'blue']

@param *values An array or list of attribute values to set. @return [nil]

# File lib/aws/simple_db/attribute.rb, line 49
def set *values
  put(values, true)
  nil
end
values(options = {}) click to toggle source

Returns all values for this attribute as an array of strings.

@example

item.attributes['ratings'].values
#=> ['5', '3', '4']

@param [Hash] options @option options [Boolean] :consistent_read (false) A consistent read

returns values that reflects all writes that received a successful
response prior to the read.

@return [Array<String>] An array of attribute values

# File lib/aws/simple_db/attribute.rb, line 135
def values options = {}
  values = []
  self.each(options) do |value|
    values << value
  end
  values
end

Protected Instance Methods

put(values, replace) click to toggle source

@private

# File lib/aws/simple_db/attribute.rb, line 145
def put values, replace
  expect_opts = values.pop if values.last.kind_of?(Hash)
  do_put(attribute_hashes(Hash[[[name, values]]],
                          replace),
         expect_opts || {})
end