Class: SdbService::Service

Inherits:
Object
  • Object
show all
Includes:
Constantizer
Defined in:
lib/sdb_service/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constantizer

#constantize

Constructor Details

#initialize(database, serializer = :json) ⇒ Service

initialization has one required argument, and one optional argument. the first argument should be the name of the database on Amazon’s SimpleDB servers. the secondary, optional argument, is the type of serialization strategy you’d like to use for this database.



18
19
20
21
22
23
24
25
# File 'lib/sdb_service/service.rb', line 18

def initialize(database, serializer = :json)
  @serializer_format = serializer
  load_serializer!(@serializer_format)
  unless database.nil?
    @database = database
    self.class.create_database!(@database)
  end
end

Instance Attribute Details

#databaseObject (readonly)

this is the name of the amazon SimpleDB domain.



11
12
13
# File 'lib/sdb_service/service.rb', line 11

def database
  @database
end

#serializer_formatObject (readonly)

this is a symbol representation of the serializer format.



12
13
14
# File 'lib/sdb_service/service.rb', line 12

def serializer_format
  @serializer_format
end

Class Method Details

.all_databasesObject

this method returns a list of all databases associated with this AWS account.



105
106
107
# File 'lib/sdb_service/service.rb', line 105

def self.all_databases
  self.send(:data_store).list_domains[0]
end

.create_database!(database) ⇒ Object

this method creates a new database associated with this AWS account, unless one with the same name already exists. returns true on success, false on failure.



111
112
113
114
115
116
117
118
# File 'lib/sdb_service/service.rb', line 111

def self.create_database!(database)
  unless self.all_databases.include?(database)
    self.send(:data_store).create_domain(database) 
    return true
  else
    return false
  end
end

.destroy_database!(database) ⇒ Object

this method destroys an existing database associated with this AWS account, unless one with the name specified does not exist. returns true on success, false on failure.



122
123
124
125
126
127
128
129
# File 'lib/sdb_service/service.rb', line 122

def self.destroy_database!(database)
  if self.all_databases.include?(database)
    self.send(:data_store).delete_domain(database)
    return true
  else
    return false
  end
end

Instance Method Details

#allObject

this method returns all of the fully qualified, and deserialized objects stored in Amazon SimpleDB for the current domain.



100
101
102
# File 'lib/sdb_service/service.rb', line 100

def all
  self.query(nil)
end

#all!Object

this method returns all of the UUIDS stored in Amazon SimpleDB for the current domain.



94
95
96
# File 'lib/sdb_service/service.rb', line 94

def all!
  self.query!(nil)
end

#clear!Object

this method can be used to reset all the data stored in an Amazon SimpleDB database associated with the current AWS account.



133
134
135
136
# File 'lib/sdb_service/service.rb', line 133

def clear!
  self.class.destroy_database!(self.database)
  self.class.create_database!(self.database)
end

#delete(id) ⇒ Object

this method deletes an object from Amazon SimpleDB based on its UUID identifier.



72
73
74
# File 'lib/sdb_service/service.rb', line 72

def delete(id)
  data_store.delete_attributes(self.database, id)
end

#get(id) ⇒ Object

This method returns an object from Amazon SimpleDB based on its UUID identifier, and will also attempt to deserialize its various values from whatever serialization format you’ve specified when constructing the data service.



59
60
61
62
63
# File 'lib/sdb_service/service.rb', line 59

def get(id)
  _parse(self.get!(id)) do |value|
    serializer.new(value.first).deserialize!
  end
end

#get!(id) ⇒ Object

This method returns an object from Amazon SimpleDB based on its UUID identifier, it returns the raw, unparsed data that sits at that key in the store.



67
68
69
# File 'lib/sdb_service/service.rb', line 67

def get!(id)
  data_store.get_attributes(self.database, id)
end

#put(*args) ⇒ Object

This method just takes a ruby object, of any depth or complexity; anything you wish to send out to the Amazon SimpleDB service, can be sent there through this method. eg: SERV.put(“some” => “data”, “more_data” => { “here” => [1,2,3] })

all data passed through the #put method is serialized into whatever format you constructed the data service to be aware of, however, you can pass a false in as the first argument of #put to have it ignore serialization entirely. eg: SERV.put(false, “some” => => [1,2,3]) #=> { “some” => [“more123”]}

put returns the unique identifier (UUID) of the object that was persisted into SimpleDB.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sdb_service/service.rb', line 37

def put(*args)
  data_payload, raw_payload = args
  serializer_result = nil
  if raw_payload.nil?
    data = _parse(data_payload) do |value|
      serializer_result = serializer.new(value)
      serializer_result.serialize!
    end
    data["mime_type"] = "text/#{self.serializer_format}"
  else
    data = raw_payload
    data["mime_type"] = "text/plain"
  end

  ident = self.class.generate_unique_identifier
  data_store.put_attributes(self.database, ident, data)
  return ident
end

#query(statement, limit = 30, token = nil) ⇒ Object

this method allows you to query simple for raw information using its query language.



82
83
84
85
86
87
88
89
90
91
# File 'lib/sdb_service/service.rb', line 82

def query(statement, limit = 30, token = nil)
  response = Hash.new
  response['results'] = Hash.new
  query, token = self.query!(statement, limit, token)
  query.each do |item|
    response['results'][item[:name]] = item[:attributes]
  end
  response['token'] = token unless token.nil?
  return response
end

#query!(statement, limit = 30, token = nil) ⇒ Object



76
77
78
79
# File 'lib/sdb_service/service.rb', line 76

def query!(statement, limit = 30, token = nil)
  stmt = statement.nil? ? "" : statement
  data_store.query_with_attributes(self.database, stmt, limit, token)
end