Class: Amazon::SDB::Item

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amazon_sdb/item.rb

Overview

An item from SimpleDB. This basically is a key for the item in the domain and a Multimap of the attributes. You should never call Item#new, instead it is returned by various methods in Domain and ResultSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, key, multimap = nil) ⇒ Item

Returns a new instance of Item.



10
11
12
13
14
# File 'lib/amazon_sdb/item.rb', line 10

def initialize(domain, key, multimap=nil)
  @domain = domain
  @key = key
  @attributes = multimap
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/amazon_sdb/item.rb', line 8

def attributes
  @attributes
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/amazon_sdb/item.rb', line 8

def key
  @key
end

Instance Method Details

#[](key) ⇒ Object



49
50
51
# File 'lib/amazon_sdb/item.rb', line 49

def [](key)
  get(key)
end

#destroy!Object

Deletes the item in SimpleDB



32
33
34
# File 'lib/amazon_sdb/item.rb', line 32

def destroy!
  @domain.delete_attributes(@key)
end

#eachObject



53
54
55
# File 'lib/amazon_sdb/item.rb', line 53

def each
  @attributes.each
end

#each_pairObject



57
58
59
# File 'lib/amazon_sdb/item.rb', line 57

def each_pair
  @attributes.each_pair
end

#empty?Boolean

Returns true if the attributes are empty

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/amazon_sdb/item.rb', line 25

def empty?
  return true if @attributes.nil?
  @attributes.size == 0
end

#get(key) ⇒ Object

Reloads the item if necessary



44
45
46
47
# File 'lib/amazon_sdb/item.rb', line 44

def get(key)
  reload! if @attributes.nil?
  @attributes.get(key)
end

#reload!Object

Reloads from the domain



18
19
20
21
# File 'lib/amazon_sdb/item.rb', line 18

def reload!
  item = @domain.get_attributes(@key)
  @attributes = item.attributes
end

#saveObject

Saves the item back (like a put_attributes with :replace => :all



38
39
40
# File 'lib/amazon_sdb/item.rb', line 38

def save
  @domain.put_attributes(@key, @attributes, :replace => :all)
end