Class RightAws::ActiveSdb::Base
In: lib/sdb/active_sdb.rb
Parent: Object
RuntimeError AwsError AwsNoChange RightAWSParser RightErrorResponseParser RightHttp2xxParser AcfInterface SqsInterface SqsGen2Interface S3Interface Ec2 SdbInterface RightAwsBase ActiveSdbConnect ActiveSdb SqsGen2 S3 S3Generator Sqs RightDummyParser AWSErrorHandler AwsBenchmarkingBlock AwsUtils RightSaxParserCallback lib/sqs/right_sqs_interface.rb lib/sqs/right_sqs_gen2.rb lib/s3/right_s3.rb lib/acf/right_acf_interface.rb lib/sqs/right_sqs_gen2_interface.rb lib/sqs/right_sqs.rb lib/sdb/right_sdb_interface.rb lib/sdb/active_sdb.rb lib/ec2/right_ec2.rb lib/s3/right_s3_interface.rb lib/awsbase/right_awsbase.rb RightAwsBaseInterface VERSION RightAws dot/m_13_0.png

Methods

Included Modules

ActiveSdbConnect

Attributes

attributes  [RW]  instance attributes
id  [RW]  item name
next_token  [RW]  next_token value returned by last find: is useful to continue finding

Public Class methods

Returns a RightAws::SdbInterface object

 class A < RightAws::ActiveSdb::Base
 end

 class B < RightAws::ActiveSdb::Base
 end

 class C < RightAws::ActiveSdb::Base
 end

 RightAws::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  #=> #<RightAws::SdbInterface:0xb76d6d7c>
 puts B.connection  #=> #<RightAws::SdbInterface:0xb76d6d7c>
 puts C.connection  #=> #<RightAws::SdbInterface:0xb76d6ca0>

[Source]

     # File lib/sdb/active_sdb.rb, line 178
178:         def connection
179:           @connection || ActiveSdb::connection
180:         end

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"]}>

[Source]

     # File lib/sdb/active_sdb.rb, line 622
622:       def self.create(attributes={})
623:         item = self.new(attributes)
624:         item.save
625:         item
626:       end

Create domain at SDB. Raises no errors if the domain already exists.

 class Client < RightAws::ActiveSdb::Base
 end
 Client.create_domain  #=> {:request_id=>"6fc652a0-0000-41d5-91f4-3ed390a3d3b2", :box_usage=>"0.0055590278"}

[Source]

     # File lib/sdb/active_sdb.rb, line 231
231:         def create_domain
232:           connection.create_domain(domain)
233:         end

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

 class Client < RightAws::ActiveSdb::Base
 end
 Client.delete_domain  #=> {:request_id=>"e14d90d3-0000-4898-9995-0de28cdda270", :box_usage=>"0.0055590278"}

[Source]

     # File lib/sdb/active_sdb.rb, line 242
242:         def delete_domain
243:           connection.delete_domain(domain)
244:         end

Current domain name.

 # if 'ActiveSupport' is not loaded then class name converted to downcase
 class Client < RightAws::ActiveSdb::Base
 end
 puts Client.domain  #=> 'client'

 # if 'ActiveSupport' is loaded then class name being tableized
 require 'activesupport'
 class Client < RightAws::ActiveSdb::Base
 end
 puts Client.domain  #=> 'clients'

 # Explicit domain name definition
 class Client < RightAws::ActiveSdb::Base
   set_domain_name :foreign_clients
 end
 puts Client.domain  #=> 'foreign_clients'

[Source]

     # File lib/sdb/active_sdb.rb, line 203
203:         def domain
204:           unless @domain
205:             if defined? ActiveSupport::CoreExtensions::String::Inflections
206:               @domain = name.tableize
207:             else
208:               @domain = name.downcase
209:             end
210:           end
211:           @domain
212:         end

Perform a find request.

Single record:

 Client.find(:first)
 Client.find(:first, :conditions=> [ "['name'=?] intersection ['wife'=?]", "Jon", "Sandy"])

Bunch of records:

 Client.find(:all)
 Client.find(:all, :limit => 10)
 Client.find(:all, :conditions=> [ "['name'=?] intersection ['girlfriend'=?]", "Jon", "Judy"])
 Client.find(:all, :conditions=> [ "['name'=?]", "Sandy"], :limit => 3)

Records by ids:

 Client.find('1')
 Client.find('1234987b4583475347523948')
 Client.find('1','2','3','4', :conditions=> [ "['toys'=?]", "beer"])

Find helpers: RightAws::ActiveSdb::Base.find_by_… and RightAws::ActiveSdb::Base.find_all_by_…

 Client.find_by_name('Matias Rust')
 Client.find_by_name_and_city('Putin','Moscow')
 Client.find_by_name_and_city_and_post('Medvedev','Moscow','president')

 Client.find_all_by_author('G.Bush jr')
 Client.find_all_by_age_and_gender_and_ethnicity('34','male','russian')
 Client.find_all_by_gender_and_country('male', 'Russia', :auto_load => true, :order => 'name desc')

Returned records have to be reloaded to access their 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>

Continue listing:

 # initial listing
 Client.find(:all, :limit => 10)
 # continue listing
 begin
   Client.find(:all, :limit => 10, :next_token => Client.next_token)
 end while Client.next_token

 Sort oder:
   Client.find(:all, :order => 'gender')
   Client.find(:all, :order => 'name desc')

 Attributes auto load (be carefull - this may take lot of time for a huge bunch of records):
   Client.find(:first)                      #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7"}, @new_record=false>
   Client.find(:first, :auto_load => true)  #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "name"=>["Cat"], "toys"=>["Jons socks", "clew", "mice"]}, @new_record=false>

see docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?UsingQuery.html

[Source]

     # File lib/sdb/active_sdb.rb, line 299
299:         def find(*args)
300:           options = args.last.is_a?(Hash) ? args.pop : {}
301:           case args.first
302:             when :all   then find_every    options
303:             when :first then find_initial  options
304:             else             find_from_ids args, options
305:           end
306:         end

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"]}>

[Source]

     # File lib/sdb/active_sdb.rb, line 611
611:       def initialize(attrs={})
612:         @attributes = uniq_values(attrs)
613:         @new_record = true
614:       end

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: RightAws::ActiveSdb::Base.select_by_… and RightAws::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

[Source]

     # File lib/sdb/active_sdb.rb, line 357
357:         def select(*args)
358:           options = args.last.is_a?(Hash) ? args.pop : {}
359:           case args.first
360:             when :all   then sql_select(options)
361:             when :first then sql_select(options.merge(:limit => 1)).first
362:             else             select_from_ids args, options
363:           end
364:         end

Change the default domain name to user defined.

 class Client < RightAws::ActiveSdb::Base
   set_domain_name :foreign_clients
 end

[Source]

     # File lib/sdb/active_sdb.rb, line 220
220:         def set_domain_name(domain)
221:           @domain = domain.to_s
222:         end

Protected Class methods

Returns an array of [attribute_name, ‘asc’|’desc’]

[Source]

     # File lib/sdb/active_sdb.rb, line 449
449:         def sort_options(sort_string)
450:           sort_string[/['"]?(\w+)['"]? *(asc|desc)?/i]
451:           [$1, ($2 || 'asc')]
452:         end

Public Instance methods

Returns the values of the attribute identified by attribute.

 puts item['Cat'].inspect  #=> ["Jons socks", "clew", "mice"]

[Source]

     # File lib/sdb/active_sdb.rb, line 677
677:       def [](attribute)
678:         @attributes[attribute.to_s]
679:       end

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"]

[Source]

     # File lib/sdb/active_sdb.rb, line 687
687:       def []=(attribute, values)
688:         attribute = attribute.to_s
689:         @attributes[attribute] = attribute == 'id' ? values.to_s : values.to_a.uniq
690:       end

Returns a hash of all the attributes.

 puts item.attributes.inspect #=> {"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "clew", "mice"]}

[Source]

     # File lib/sdb/active_sdb.rb, line 642
642:       def attributes
643:         @attributes.dup
644:       end

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"]}

[Source]

     # File lib/sdb/active_sdb.rb, line 657
657:       def attributes=(attrs)
658:         old_id = @attributes['id']
659:         @attributes = uniq_values(attrs)
660:         @attributes['id'] = old_id if @attributes['id'].blank? && !old_id.blank?
661:         self.attributes
662:       end

[Source]

     # File lib/sdb/active_sdb.rb, line 664
664:       def connection 
665:         self.class.connection
666:       end

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>

[Source]

     # File lib/sdb/active_sdb.rb, line 890
890:       def delete
891:         raise_on_id_absence
892:         connection.delete_attributes(domain, id)
893:       end

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"}>

[Source]

     # File lib/sdb/active_sdb.rb, line 870
870:       def delete_attributes(*attrs_list)
871:         raise_on_id_absence
872:         attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s }
873:         attrs_list.delete('id')
874:         unless attrs_list.blank?
875:           connection.delete_attributes(domain, id, attrs_list)
876:           attrs_list.each { |attribute| @attributes.delete(attribute) }
877:         end
878:         attrs_list
879:       end

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"]}>

[Source]

     # File lib/sdb/active_sdb.rb, line 841
841:       def delete_values(attrs)
842:         raise_on_id_absence
843:         attrs = uniq_values(attrs)
844:         attrs.delete('id')
845:         unless attrs.blank?
846:           connection.delete_attributes(domain, id, attrs)
847:           attrs.each do |attribute, values|
848:             # remove the values from the attribute
849:             if @attributes[attribute]
850:               @attributes[attribute] -= values
851:             else
852:               # if the attribute is unknown remove it from a resulting list of fixed attributes
853:               attrs.delete(attribute)
854:             end
855:           end
856:         end
857:         attrs
858:       end

Item domain name.

[Source]

     # File lib/sdb/active_sdb.rb, line 669
669:       def domain
670:         self.class.domain
671:       end

Returns an item id. Same as: item[‘id’] or item.attributes[‘id’]

[Source]

     # File lib/sdb/active_sdb.rb, line 629
629:       def id
630:         @attributes['id']
631:       end

Sets an item id.

[Source]

     # File lib/sdb/active_sdb.rb, line 634
634:       def id=(id)
635:         @attributes['id'] = id.to_s
636:       end

Returns true if this object hasn‘t been saved yet.

[Source]

     # File lib/sdb/active_sdb.rb, line 901
901:       def new_record?
902:         @new_record
903:       end

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

[Source]

     # File lib/sdb/active_sdb.rb, line 753
753:       def put
754:         @attributes = uniq_values(@attributes)
755:         prepare_for_update
756:         attrs = @attributes.dup
757:         attrs.delete('id')
758:         connection.put_attributes(domain, id, attrs) unless attrs.blank?
759:         connection.put_attributes(domain, id, { 'id' => id }, :replace)
760:         mark_as_old
761:         @attributes
762:       end

Stores specified attributes. attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }. Returns a hash of saved attributes.

see to put method

[Source]

     # File lib/sdb/active_sdb.rb, line 769
769:       def put_attributes(attrs)
770:         attrs = uniq_values(attrs)
771:         prepare_for_update
772:         # if 'id' is present in attrs hash:
773:         # replace internal 'id' attribute and remove it from the attributes to be sent
774:         @attributes['id'] = attrs['id'] unless attrs['id'].blank?
775:         attrs.delete('id')
776:         # add new values to all attributes from list
777:         connection.put_attributes(domain, id, attrs) unless attrs.blank?
778:         connection.put_attributes(domain, id, { 'id' => id }, :replace)
779:         attrs.each do |attribute, values|
780:           @attributes[attribute] ||= []
781:           @attributes[attribute] += values
782:           @attributes[attribute].uniq!
783:         end
784:         mark_as_old
785:         attributes
786:       end

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>

[Source]

     # File lib/sdb/active_sdb.rb, line 697
697:       def reload
698:         raise_on_id_absence
699:         old_id = id
700:         attrs = connection.get_attributes(domain, id)[:attributes]
701:         @attributes = {}
702:         unless attrs.blank?
703:           attrs.each { |attribute, values| @attributes[attribute] = values }
704:           @attributes['id'] = old_id
705:         end
706:         mark_as_old
707:         @attributes
708:       end

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"]}

[Source]

     # File lib/sdb/active_sdb.rb, line 720
720:       def reload_attributes(*attrs_list)
721:         raise_on_id_absence
722:         attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s }
723:         attrs_list.delete('id')
724:         result = {}
725:         attrs_list.flatten.uniq.each do |attribute|
726:           attribute = attribute.to_s
727:           values = connection.get_attributes(domain, id, attribute)[:attributes][attribute]
728:           unless values.blank?
729:             @attributes[attribute] = result[attribute] = values
730:           else
731:             @attributes.delete(attribute)
732:           end
733:         end
734:         mark_as_old
735:         result
736:       end

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.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"=>["kids"]}

compare to put method

[Source]

     # File lib/sdb/active_sdb.rb, line 803
803:       def save
804:         @attributes = uniq_values(@attributes)
805:         prepare_for_update
806:         connection.put_attributes(domain, id, @attributes, :replace)
807:         mark_as_old
808:         @attributes
809:       end

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

[Source]

     # File lib/sdb/active_sdb.rb, line 817
817:       def save_attributes(attrs)
818:         prepare_for_update
819:         attrs = uniq_values(attrs)
820:         # if 'id' is present in attrs hash then replace internal 'id' attribute
821:         unless attrs['id'].blank?
822:           @attributes['id'] = attrs['id']
823:         else
824:           attrs['id'] = id
825:         end
826:         connection.put_attributes(domain, id, attrs, :replace) unless attrs.blank?
827:         attrs.each { |attribute, values| attrs[attribute] = values }
828:         mark_as_old
829:         attrs
830:       end

Item ID

[Source]

     # File lib/sdb/active_sdb.rb, line 896
896:       def to_s
897:         @id
898:       end

Private Instance methods

[Source]

     # File lib/sdb/active_sdb.rb, line 915
915:       def prepare_for_update
916:         @attributes['id'] = self.class.generate_id if @attributes['id'].blank?
917:       end

[Source]

     # File lib/sdb/active_sdb.rb, line 911
911:       def raise_on_id_absence
912:         raise ActiveSdbError.new('Unknown record id') unless id
913:       end

[Validate]