Class: AWS::SimpleDB::Attribute

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws/simple_db/attribute.rb

Overview

Represents a single named item attribute in SimpleDB.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#itemItem (readonly)

Returns The item this attribute belongs to.

Returns:

  • (Item)

    The item this attribute belongs to.



39
40
41
# File 'lib/aws/simple_db/attribute.rb', line 39

def item
  @item
end

#nameString (readonly)

Returns The name of this attribute.

Returns:

  • (String)

    The name of this attribute.



42
43
44
# File 'lib/aws/simple_db/attribute.rb', line 42

def name
  @name
end

Instance Method Details

#add(*values) ⇒ nil Also known as: <<

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

Examples:

Adding a list of values


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

Adding an array of values


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

Parameters:

  • *values

    An array or list of attribute values to add.

Returns:

  • (nil)


72
73
74
75
# File 'lib/aws/simple_db/attribute.rb', line 72

def add *values
  put(values, false)
  nil
end

#delete(*values) ⇒ nil

Deletes this attribute or specific values from this attribute.

Examples:

Delete the attribute and all of its values


item.attributes['color'].delete

Delete specific attribute values


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

Parameters:

  • values

    One ore more values to remove from this attribute. If values is empty, then all attribute values are deleted (which deletes this attribute).

Returns:

  • (nil)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aws/simple_db/attribute.rb', line 92

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 = {}) {|attribute_value| ... } ⇒ nil

Yields once for each value on this attribute.

Parameters:

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

Options Hash (options):

  • :consistent_read (Boolean) — default: false

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

Yields:

  • (attribute_value)

    Yields once for each domain in the account.

Yield Parameters:

  • attribute_value (String)

Returns:

  • (nil)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/aws/simple_db/attribute.rb', line 113

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) ⇒ nil

Sets all values for this attribute, replacing current values.

Examples:

Setting a list of values

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

Setting an array of values

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

Parameters:

  • *values

    An array or list of attribute values to set.

Returns:

  • (nil)


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

def set *values
  put(values, true)
  nil
end

#values(options = {}) ⇒ Array<String>

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

Examples:

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

Parameters:

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

Options Hash (options):

  • :consistent_read (Boolean) — default: false

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

Returns:

  • (Array<String>)

    An array of attribute values



140
141
142
143
144
145
146
# File 'lib/aws/simple_db/attribute.rb', line 140

def values options = {}
  values = []
  self.each(options) do |value|
    values << value
  end
  values
end