Class | Aws::ActiveSdb::Base |
In: |
lib/sdb/active_sdb.rb
|
Parent: | Object |
attributes | [RW] | instance attributes |
id | [RW] | item name |
next_token | [RW] | next_token value returned by last find: is useful to continue finding |
Returns a Aws::SdbInterface object
class A < Aws::ActiveSdb::Base end class B < Aws::ActiveSdb::Base end class C < Aws::ActiveSdb::Base end Aws::ActiveSdb.establish_connection 'key_id_1', 'secret_key_1' C.establish_connection 'key_id_2', 'secret_key_2' # A and B uses the default connection, C - uses its own puts A.connection #=> #<Aws::SdbInterface:0xb76d6d7c> puts B.connection #=> #<Aws::SdbInterface:0xb76d6d7c> puts C.connection #=> #<Aws::SdbInterface:0xb76d6ca0>
Create and save new Item instance. Attributes is a hash: { attribute1 => values1, …, attributeN => valuesN }.
item = Client.create('name' => 'Cat', 'toys' => ['Jons socks', 'mice', 'clew']) puts item.inspect #=> #<Client:0xb77a0a78 @new_record=false, @attributes={"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "mice", "clew"]}>
Current domain name.
# if 'ActiveSupport' is not loaded then class name converted to downcase class Client < Aws::ActiveSdb::Base end puts Client.domain #=> 'client' # if 'ActiveSupport' is loaded then class name being tableized require 'activesupport' class Client < Aws::ActiveSdb::Base end puts Client.domain #=> 'clients' # Explicit domain name definition class Client < Aws::ActiveSdb::Base set_domain_name :foreign_clients end puts Client.domain #=> 'foreign_clients'
Same as find, but will return SimpleDB metadata like :request_id and :box_usage
Create new Item instance. attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }.
item = Client.new('name' => 'Jon', 'toys' => ['girls', 'beer', 'pub']) puts item.inspect #=> #<Client:0xb77a2698 @new_record=true, @attributes={"name"=>["Jon"], "toys"=>["girls", "beer", "pub"]}> item.save #=> {"name"=>["Jon"], "id"=>"c03edb7e-e45c-11dc-bede-001bfc466dd7", "toys"=>["girls", "beer", "pub"]} puts item.inspect #=> #<Client:0xb77a2698 @new_record=false, @attributes={"name"=>["Jon"], "id"=>"c03edb7e-e45c-11dc-bede-001bfc466dd7", "toys"=>["girls", "beer", "pub"]}>
Perform a SQL-like select request.
Single record:
Client.select(:first) Client.select(:first, :conditions=> [ "name=? AND wife=?", "Jon", "Sandy"]) Client.select(:first, :conditions=> { :name=>"Jon", :wife=>"Sandy" }, :select => :girfriends)
Bunch of records:
Client.select(:all) Client.select(:all, :limit => 10) Client.select(:all, :conditions=> [ "name=? AND 'girlfriend'=?", "Jon", "Judy"]) Client.select(:all, :conditions=> { :name=>"Sandy" }, :limit => 3)
Records by ids:
Client.select('1') Client.select('1234987b4583475347523948') Client.select('1','2','3','4', :conditions=> ["toys=?", "beer"])
Find helpers: Aws::ActiveSdb::Base.select_by_… and Aws::ActiveSdb::Base.select_all_by_…
Client.select_by_name('Matias Rust') Client.select_by_name_and_city('Putin','Moscow') Client.select_by_name_and_city_and_post('Medvedev','Moscow','president') Client.select_all_by_author('G.Bush jr') Client.select_all_by_age_and_gender_and_ethnicity('34','male','russian') Client.select_all_by_gender_and_country('male', 'Russia', :order => 'name')
Continue listing:
# initial listing Client.select(:all, :limit => 10) # continue listing begin Client.select(:all, :limit => 10, :next_token => Client.next_token) end while Client.next_token Sort oder: If :order=>'attribute' option is specified then result response (ordered by 'attribute') will contain only items where attribute is defined (is not null). Client.select(:all) # returns all records Client.select(:all, :order => 'gender') # returns all records ordered by gender where gender attribute exists Client.select(:all, :order => 'name desc') # returns all records ordered by name in desc order where name attribute exists
see docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?UsingSelect.html
Change the default domain name to user defined.
class Client < Aws::ActiveSdb::Base set_domain_name :foreign_clients end
Returns the values of the attribute identified by attribute.
puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"]
Updates the attribute identified by attribute with the specified values.
puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"] item['Cat'] = ["Whiskas", "chicken"] puts item['Cat'].inspect #=> ["Whiskas", "chicken"]
Returns a hash of all the attributes.
puts item.attributes.inspect #=> {"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "clew", "mice"]}
Allows one to set all the attributes at once by passing in a hash with keys matching the attribute names. if ‘id’ attribute is not set in new attributes has then it being derived from old attributes.
puts item.attributes.inspect #=> {"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "clew", "mice"]} # set new attributes ('id' is missed) item.attributes = { 'name'=>'Dog', 'toys'=>['bones','cats'] } puts item.attributes.inspect #=> {"name"=>["Dog"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["bones", "cats"]} # set new attributes ('id' is set) item.attributes = { 'id' => 'blah-blah', 'name'=>'Birds', 'toys'=>['seeds','dogs tail'] } puts item.attributes.inspect #=> {"name"=>["Birds"], "id"=>"blah-blah", "toys"=>["seeds", "dogs tail"]}
Delete the Item entirely from SDB.
sandy = Client.find_by_name 'Sandy' sandy.reload sandy.inspect #=> #<Client:0xb7761d28 @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"}> puts sandy.delete sandy.reload puts sandy.inspect #=> #<Client:0xb7761d28 @attributes={}, @new_record=false>
Removes specified attributes from the item. attrs_list is an array or comma separated list of attributes names. Returns the list of deleted attributes.
sandy = Client.find_by_name 'Sandy' sandy.reload puts sandy.inspect #=> #<Client:0xb7761d28 @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"}> puts sandy.delete_attributes('toys') #=> ['toys'] puts sandy.inspect #=> #<Client:0xb7761d28 @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7"}>
Remove specified values from corresponding attributes. attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }.
sandy = Client.find_by_name 'Sandy' sandy.reload puts sandy.inspect #=> #<Client:0xb77b48fc @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"]}> puts sandy.delete_values('toys' => 'patchwork') #=> { 'toys' => ['patchwork'] } puts sandy.inspect #=> #<Client:0xb77b48fc @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids"]}>
This is to separate initialization from user vs coming from db (ie: find())
Stores in-memory attributes to SDB. Adds the attributes values to already stored at SDB. Returns a hash of stored attributes.
sandy = Client.new(:name => 'Sandy') #=> #<Client:0xb775a7a8 @attributes={"name"=>["Sandy"]}, @new_record=true> sandy['toys'] = 'boys' sandy.put sandy['toys'] = 'patchwork' sandy.put sandy['toys'] = 'kids' sandy.put puts sandy.attributes.inspect #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["kids"]} sandy.reload #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"]}
compare to save method
Stores specified attributes. attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }. Returns a hash of saved attributes.
see to put method
Reload attributes from SDB. Replaces in-memory attributes.
item = Client.find_by_name('Cat') #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7"}, @new_record=false> item.reload #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "name"=>["Cat"], "toys"=>["Jons socks", "clew", "mice"]}, @new_record=false>
Reload a set of attributes from SDB. Adds the loaded list to in-memory data. attrs_list is an array or comma separated list of attributes names. Returns a hash of loaded attributes.
This is not the best method to get a bunch of attributes because a web service call is being performed for every attribute.
item = Client.find_by_name('Cat') item.reload_attributes('toys', 'name') #=> {"name"=>["Cat"], "toys"=>["Jons socks", "clew", "mice"]}
Store in-memory attributes to SDB. Replaces the attributes values already stored at SDB by in-memory data. Returns a hash of stored attributes.
sandy = Client.new(:name => 'Sandy') #=> #<Client:0xb775a7a8 @attributes={"name"=>["Sandy"]}, @new_record=true> sandy['toys'] = 'boys' sandy.save sandy['toys'] = 'patchwork' sandy.save sandy['toys'] = 'kids' sandy.save puts sandy.attributes.inspect #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["kids"]} sandy.reload #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["kids"]}
Options:
- :except => Array of attributes to NOT save
compare to put method
Replaces the attributes at SDB by the given values. Attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }. The other in-memory attributes are not being saved. Returns a hash of stored attributes.
see save method