Class: RightAws::ActiveSdb

Inherits:
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'

Additionally, the ActiveSdb class requires the 'uuidtools' gem; this gem is not normally required by RightAws and is not installed as a dependency of RightAws.

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].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 #=>
    {"name"=>["Putin"],
     "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 => 'nulla.facilisis@metus.com', :score => 100, :is_active => true, :registered_at => Time.local(2000, 1, 1) )

person = Person.find_by_email 'nulla.facilisis@metus.com'
person.reload

pp person.attributes #=>
  {"name"=>["Yetta 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"=>["nulla.facilisis@metus.com"]}
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

+ (Object) create_domain(domain_name)

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

RightAws::ActiveSdb.create_domain('alpha')  #=> {:request_id=>"6fc652a0-0000-41d5-91f4-3ed390a3d3b2", :box_usage=>"0.0055590278"}


189
190
191
# File 'lib/sdb/active_sdb.rb', line 189

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

+ (Object) delete_domain(domain_name)

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

RightAws::ActiveSdb.create_domain('alpha')  #=> {:request_id=>"6fc652a0-0000-41c6-91f4-3ed390a3d3b2", :box_usage=>"0.0055590001"}


198
199
200
# File 'lib/sdb/active_sdb.rb', line 198

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

+ (Object) domains

Retreive a list of domains.

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


180
181
182
# File 'lib/sdb/active_sdb.rb', line 180

def domains
  connection.list_domains[:domains]
end