Class: RightAws::ActiveSdb

Inherits:
Object
  • Object
show all
Extended by:
ActiveSdbConnect
Defined in:
lib/sdb/active_sdb.rb

Overview

RightAws::ActiveSdb -- RightScale SDB interface (alpha release)

The RightAws::ActiveSdb class provides a complete interface to Amazon's Simple Database Service.

ActiveSdb is in alpha and does not load by default with the rest of RightAws. You must use an additional require statement to load the ActiveSdb class. For example:

require 'right_aws'
require 'sdb/active_sdb'

Simple ActiveSdb usage example:

class Client < RightAws::ActiveSdb::Base end

connect to SDB

RightAws::ActiveSdb.establish_connection

create domain

Client.create_domain

create initial DB

Client.create 'name' => 'Bush', 'country' => 'USA', 'gender' => 'male', 'expiration' => '2009', 'post' => 'president' Client.create 'name' => 'Putin', 'country' => 'Russia', 'gender' => 'male', 'expiration' => '2008', 'post' => 'president' Client.create 'name' => 'Medvedev', 'country' => 'Russia', 'gender' => 'male', 'expiration' => '2012', 'post' => 'president' Client.create 'name' => 'Mary', 'country' => 'USA', 'gender' => 'female', 'hobby' => ['patchwork', 'bundle jumping'] Client.create 'name' => 'Mary', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking'] sandy_id = Client.create('name' => 'Sandy', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking']).id

find all Bushes in USA

Client.find(:all, :conditions => ["['name'=?] intersection ['country'=?]",'Bush','USA']).each do |client| client.reload puts client.attributes.inspect end

find all Maries through the world

Client.find_all_by_name_and_gender('Mary','female').each do |mary| mary.reload puts "#mary[:name], gender: #mary[:gender], hobbies: #mary[:hobby]mary[:hobby].join(',')" end

find new russian president

medvedev = Client.find_by_post_and_country_and_expiration('president','Russia','2012') if medvedev medvedev.reload medvedev.save_attributes('age' => '42', 'hobby' => 'Gazprom') end

retire old president

Client.find_by_name('Putin').delete

Sandy disappointed in 'cooking' and decided to hide her 'gender' and 'country' ()

sandy = Client.find(sandy_id) sandy.reload sandy.delete_values('hobby' => ['cooking'] ) sandy.delete_attributes('country', 'gender')

remove domain

Client.delete_domain

Dynamic attribute accessors

class KdClient < RightAws::ActiveSdb::Base end

client = KdClient.select(:all, :order => 'expiration').first pp client.attributes #=> "post"=>["president"], "country"=>["Russia"], "expiration"=>["2008"], "id"=>"376d2e00-75b0-11dd-9557-001bfc466dd7", "gender"=>["male"]

pp client.name    #=> ["Putin"]
pp client.country #=> ["Russia"]
pp client.post    #=> ["president"]

Columns and simple typecasting

class Person < RightAws::ActiveSdb::Base columns do name email score :Integer is_active :Boolean registered_at :DateTime created_at :DateTime, :default => lambda{ Time.now } end end Person::create( :name => 'Yetta E. Andrews', :email => '[email protected]', :score => 100, :is_active => true, :registered_at => Time.local(2000, 1, 1) )

person = Person.find_by_email '[email protected]' person.reload

pp person.attributes #=> E. Andrews"], "created_at"=>["2010-04-02T20:51:58+0400"], "id"=>"0ee24946-3e60-11df-9d4c-0025b37efad0", "registered_at"=>["2000-01-01T00:00:00+0300"], "is_active"=>["T"], "score"=>["100"], "email"=>["[email protected]"] pp person.name #=> "Yetta E. Andrews" pp person.name.class #=> String pp person.registered_at.to_s #=> "2000-01-01T00:00:00+03:00" pp person.registered_at.class #=> DateTime pp person.is_active #=> true pp person.is_active.class #=> TrueClass pp person.score #=> 100 pp person.score.class #=> Fixnum pp person.created_at.to_s #=> "2010-04-02T20:51:58+04:00"

Defined Under Namespace

Modules: ActiveSdbConnect Classes: ActiveSdbError, Base, BooleanSerialization, ColumnSet, DateTimeSerialization, IntegerSerialization

Class Method Summary collapse

Methods included from ActiveSdbConnect

connection, establish_connection

Class Method Details

.create_domain(domain_name) ⇒ Object

Create new domain. Raises no errors if the domain already exists.

RightAws::ActiveSdb.create_domain('alpha') #=> :box_usage=>"0.0055590278"



178
179
180
# File 'lib/sdb/active_sdb.rb', line 178

def create_domain(domain_name)
  connection.create_domain(domain_name)
end

.delete_domain(domain_name) ⇒ Object

Remove domain from SDB. Raises no errors if the domain does not exist.

RightAws::ActiveSdb.create_domain('alpha') #=> :box_usage=>"0.0055590001"



187
188
189
# File 'lib/sdb/active_sdb.rb', line 187

def delete_domain(domain_name)
  connection.delete_domain(domain_name)
end

.domainsObject

Retreive a list of domains.

put RightAws::ActiveSdb.domains #=> ['co-workers','family','friends','clients']



169
170
171
# File 'lib/sdb/active_sdb.rb', line 169

def domains
  connection.list_domains[:domains]
end