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.



34
35
36
# File 'lib/aws/simple_db/attribute.rb', line 34

def item
  @item
end

#nameString (readonly)

Returns The name of this attribute.

Returns:

  • (String)

    The name of this attribute.



37
38
39
# File 'lib/aws/simple_db/attribute.rb', line 37

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 (String)

    A list of attribute values to add.

Returns:

  • (nil)


67
68
69
70
# File 'lib/aws/simple_db/attribute.rb', line 67

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)


87
88
89
90
91
92
93
94
95
96
97
# 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 = {}) {|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)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ 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 (String)

    A list of attribute values to set.

Returns:

  • (nil)


49
50
51
52
# File 'lib/aws/simple_db/attribute.rb', line 49

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



135
136
137
138
139
140
141
# File 'lib/aws/simple_db/attribute.rb', line 135

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