Class: AWS::DynamoDB::AttributeCollection
- Inherits:
-
Object
- Object
- AWS::DynamoDB::AttributeCollection
- Includes:
- Expectations, Enumerable
- Defined in:
- lib/aws/dynamo_db/attribute_collection.rb
Overview
The SDK always returns numbers as BigDecimal objects and sets as Set objects; however, on input it accepts any numeric type for number attributes and either Arrays or Sets for set attributes.
Represents the attributes of a DynamoDB item. An attribute is a name-value pair. The name must be a string, but the value can be a string, number, string set, or number set. Attribute values cannot be null or empty.
Defined Under Namespace
Classes: UpdateBuilder
Instance Attribute Summary collapse
-
#item ⇒ Item
readonly
The item to which these attributes belong.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Retrieves the value of a single attribute.
-
#[]=(attribute, value) ⇒ Object
Replaces the value of a single attribute.
-
#add(attributes, options = {}) ⇒ Object
Adds to the values of one or more attributes.
- #delete(*args) ⇒ Object
-
#each(options = {}, &block) ⇒ Object
Behaves like Hash#each; yields each attribute as a name/value pair.
- #each_key(options = {}) {|name| ... } ⇒ Object
- #each_value(options = {}) {|value| ... } ⇒ Object
-
#set(attributes, options = {}) ⇒ Object
(also: #merge!, #put)
Replaces the values of one or more attributes.
- #to_hash(options = {}) ⇒ Object (also: #to_h)
-
#update(options = {}) {|builder| ... } ⇒ nil
Updates multiple attributes in a single operation.
-
#values_at(*attributes) ⇒ Array
Retrieves the values of the specified attributes.
Instance Attribute Details
#item ⇒ Item (readonly)
Returns The item to which these attributes belong.
72 73 74 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 72 def item @item end |
Instance Method Details
#[](attribute) ⇒ Object
Retrieves the value of a single attribute.
113 114 115 116 117 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 113 def [] attribute attribute = attribute.to_s response_attributes = get_item(:attributes_to_get => [attribute]) value_from_response(response_attributes[attribute]) end |
#[]=(attribute, value) ⇒ Object
Replaces the value of a single attribute.
131 132 133 134 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 131 def []= attribute, value set(attribute => value) value end |
#add(attributes, options = {}) ⇒ Object
Adds to the values of one or more attributes. Each attribute must be a set or number in the original item, and each input value must have the same type as the value in the original item. For example, it is invalid to add a single number to a set of numbers, but it is valid to add a set containing a single number to another set of numbers.
When the original attribute is a set, the values provided to this method are added to that set. When the original attribute is a number, the original value is incremented by the numeric value provided to this method. For example:
item = table.items.put(
:id => "abc123",
:colors => ["red", "white"],
:age => 3
)
item.attributes.add(
{ :colors => ["muave"],
:age => 1 },
:return => :updated_new
) # => { "colors" => Set["red", "white", "mauve"], "age" => 4 }
190 191 192 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 190 def add attributes, = {} update() { |u| u.add(attributes) } end |
#delete(*attributes) ⇒ Object #delete(attributes, options = {}) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 217 def delete *args if args.first.kind_of?(Hash) delete_args = [args.shift] else delete_args = args end = args.pop if args.last.kind_of?(Hash) update( || {}) { |u| u.delete(*delete_args) } end |
#each(options = {}, &block) ⇒ Object
Behaves like Hash#each; yields each attribute as a name/value pair.
attributes.each { |(name, value)| puts "#{name} = #{value}" }
attributes.each { |name, value| puts "#{name} = #{value}" }
90 91 92 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 90 def each( = {}, &block) to_hash().each(&block) end |
#each_key(options = {}) {|name| ... } ⇒ Object
95 96 97 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 95 def each_key( = {}) each() { |k, v| yield(k) if block_given? } end |
#each_value(options = {}) {|value| ... } ⇒ Object
102 103 104 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 102 def each_value( = {}) each() { |k, v| yield(v) if block_given? } end |
#set(attributes, options = {}) ⇒ Object Also known as: merge!, put
Replaces the values of one or more attributes.
149 150 151 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 149 def set attributes, = {} update() { |u| u.set(attributes) } end |
#to_hash(options = {}) ⇒ Object Also known as: to_h
420 421 422 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 420 def to_hash = {} values_from_response_hash(get_item()) end |
#update(options = {}) {|builder| ... } ⇒ nil
DnamoDB allows only one update per attribute in a single operation. This method will raise an ArgumentError if multiple updates are described for a single attribute.
Updates multiple attributes in a single operation. This is more efficient than performing each type of update in sequence, and it also allows you to group different kinds of updates into an atomic operation.
item.attributes.update do |u|
# add 12 to the (numeric) value of "views"
u.add(:views => 12)
# delete attributes
u.delete(:foo, :bar)
# delete values out of a set attribute
u.delete(:colors => ["red", "blue"])
# replace values
u.set(:title => "More Automobiles")
end
379 380 381 382 383 384 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 379 def update( = {}) builder = UpdateBuilder.new yield(builder) do_updates({ :attribute_updates => builder.updates }, ) end |
#values_at(*attributes) ⇒ Array
Retrieves the values of the specified attributes.
400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/aws/dynamo_db/attribute_collection.rb', line 400 def values_at(*attributes) = {} = attributes.pop if attributes.last.kind_of?(Hash) return [] if attributes.empty? attributes.map! { |a| a.to_s } response_attributes = get_item(, :attributes_to_get => attributes) values_from_response_hash(response_attributes). values_at(*attributes) end |