Class: RightAws::ActiveSdb::Base
- Extended by:
- ActiveSdbConnect
- Defined in:
- lib/sdb/active_sdb.rb
Class Attribute Summary collapse
-
.next_token ⇒ Object
next_token value returned by last find: is useful to continue finding.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns a hash of all the attributes.
-
#id ⇒ Object
Returns an item id.
Class Method Summary collapse
-
.connection ⇒ Object
Returns a RightAws::SdbInterface object.
-
.create(attributes = {}) ⇒ Object
Create and save new Item instance.
-
.create_domain ⇒ Object
Create domain at SDB.
-
.delete_domain ⇒ Object
Remove domain from SDB.
-
.domain ⇒ Object
Current domain name.
-
.find(*args) ⇒ Object
Perform a find request.
-
.generate_id ⇒ Object
:nodoc:.
-
.set_domain_name(domain) ⇒ Object
Change the default domain name to user defined.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Returns the values of the attribute identified by
attribute. -
#[]=(attribute, values) ⇒ Object
Updates the attribute identified by
attributewith the specifiedvalues. - #connection ⇒ Object
-
#delete ⇒ Object
Delete the Item entirely from SDB.
-
#delete_attributes(*attrs_list) ⇒ Object
Removes specified attributes from the item.
-
#delete_values(attrs) ⇒ Object
Remove specified values from corresponding attributes.
-
#domain ⇒ Object
Item domain name.
-
#initialize(attrs = {}) ⇒ Base
constructor
Create new Item instance.
-
#mark_as_old ⇒ Object
:nodoc:.
-
#new_record? ⇒ Boolean
Returns true if this object hasn‘t been saved yet.
-
#put ⇒ Object
Stores in-memory attributes to SDB.
-
#put_attributes(attrs) ⇒ Object
Stores specified attributes.
-
#reload ⇒ Object
Reload attributes from SDB.
-
#reload_attributes(*attrs_list) ⇒ Object
Reload a set of attributes from SDB.
-
#save ⇒ Object
Store in-memory attributes to SDB.
-
#save_attributes(attrs) ⇒ Object
Replaces the attributes at SDB by the given values.
-
#to_s ⇒ Object
Item ID.
Methods included from ActiveSdbConnect
Constructor Details
#initialize(attrs = {}) ⇒ Base
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"]}>
462 463 464 465 |
# File 'lib/sdb/active_sdb.rb', line 462 def initialize(attrs={}) @attributes = uniq_values(attrs) @new_record = true end |
Class Attribute Details
.next_token ⇒ Object
next_token value returned by last find: is useful to continue finding
156 157 158 |
# File 'lib/sdb/active_sdb.rb', line 156 def next_token @next_token end |
Instance Attribute Details
#attributes ⇒ Object
Returns a hash of all the attributes.
puts item.attributes.inspect #=> {"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "clew", "mice"]}
449 450 451 |
# File 'lib/sdb/active_sdb.rb', line 449 def attributes @attributes end |
#id ⇒ Object
Returns an item id. Same as: item or item.attributes
452 453 454 |
# File 'lib/sdb/active_sdb.rb', line 452 def id @id end |
Class Method Details
.connection ⇒ Object
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>
178 179 180 |
# File 'lib/sdb/active_sdb.rb', line 178 def connection @connection || ActiveSdb::connection end |
.create(attributes = {}) ⇒ Object
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"]}>
473 474 475 476 477 |
# File 'lib/sdb/active_sdb.rb', line 473 def self.create(attributes={}) item = self.new(attributes) item.save item end |
.create_domain ⇒ Object
231 232 233 |
# File 'lib/sdb/active_sdb.rb', line 231 def create_domain connection.create_domain(domain) end |
.delete_domain ⇒ Object
242 243 244 |
# File 'lib/sdb/active_sdb.rb', line 242 def delete_domain connection.delete_domain(domain) end |
.domain ⇒ Object
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'
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/sdb/active_sdb.rb', line 203 def domain unless @domain if defined? ActiveSupport::CoreExtensions::String::Inflections @domain = name.tableize else @domain = name.downcase end end @domain end |
.find(*args) ⇒ Object
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.('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>
297 298 299 300 301 302 303 304 |
# File 'lib/sdb/active_sdb.rb', line 297 def find(*args) = args.last.is_a?(Hash) ? args.pop : {} case args.first when :all then find_every when :first then find_initial else find_from_ids args, end end |
.generate_id ⇒ Object
:nodoc:
442 443 444 |
# File 'lib/sdb/active_sdb.rb', line 442 def self.generate_id # :nodoc: UUID.().to_s end |
Instance Method Details
#[](attribute) ⇒ Object
Returns the values of the attribute identified by attribute.
puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"]
528 529 530 |
# File 'lib/sdb/active_sdb.rb', line 528 def [](attribute) @attributes[attribute.to_s] end |
#[]=(attribute, values) ⇒ Object
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"]
538 539 540 541 |
# File 'lib/sdb/active_sdb.rb', line 538 def []=(attribute, values) attribute = attribute.to_s @attributes[attribute] = attribute == 'id' ? values.to_s : values.to_a.uniq end |
#connection ⇒ Object
515 516 517 |
# File 'lib/sdb/active_sdb.rb', line 515 def connection self.class.connection end |
#delete ⇒ Object
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>
733 734 735 736 |
# File 'lib/sdb/active_sdb.rb', line 733 def delete raise_on_id_absence connection.delete_attributes(domain, id) end |
#delete_attributes(*attrs_list) ⇒ Object
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"}>
713 714 715 716 717 718 719 720 721 722 |
# File 'lib/sdb/active_sdb.rb', line 713 def delete_attributes(*attrs_list) raise_on_id_absence attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s } attrs_list.delete('id') unless attrs_list.blank? connection.delete_attributes(domain, id, attrs_list) attrs_list.each { |attribute| @attributes.delete(attribute) } end attrs_list end |
#delete_values(attrs) ⇒ Object
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"]}>
692 693 694 695 696 697 698 699 700 701 |
# File 'lib/sdb/active_sdb.rb', line 692 def delete_values(attrs) raise_on_id_absence attrs = uniq_values(attrs) attrs.delete('id') unless attrs.blank? connection.delete_attributes(domain, id, attrs) attrs.each { |attribute, values| @attributes[attribute] -= values } end attrs end |
#domain ⇒ Object
Item domain name.
520 521 522 |
# File 'lib/sdb/active_sdb.rb', line 520 def domain self.class.domain end |
#mark_as_old ⇒ Object
:nodoc:
748 749 750 |
# File 'lib/sdb/active_sdb.rb', line 748 def mark_as_old # :nodoc: @new_record = false end |
#new_record? ⇒ Boolean
Returns true if this object hasn‘t been saved yet.
744 745 746 |
# File 'lib/sdb/active_sdb.rb', line 744 def new_record? @new_record end |
#put ⇒ Object
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
604 605 606 607 608 609 610 611 612 613 |
# File 'lib/sdb/active_sdb.rb', line 604 def put @attributes = uniq_values(@attributes) prepare_for_update attrs = @attributes.dup attrs.delete('id') connection.put_attributes(domain, id, attrs) unless attrs.blank? connection.put_attributes(domain, id, { 'id' => id }, :replace) mark_as_old @attributes end |
#put_attributes(attrs) ⇒ Object
Stores specified attributes. attrs is a hash: { attribute1 => values1, …, attributeN => valuesN }. Returns a hash of saved attributes.
see to put method
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'lib/sdb/active_sdb.rb', line 620 def put_attributes(attrs) attrs = uniq_values(attrs) prepare_for_update # if 'id' is present in attrs hash: # replace internal 'id' attribute and remove it from the attributes to be sent @attributes['id'] = attrs['id'] unless attrs['id'].blank? attrs.delete('id') # add new values to all attributes from list connection.put_attributes(domain, id, attrs) unless attrs.blank? connection.put_attributes(domain, id, { 'id' => id }, :replace) attrs.each do |attribute, values| @attributes[attribute] ||= [] @attributes[attribute] += values @attributes[attribute].uniq! end mark_as_old attributes end |
#reload ⇒ Object
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>
548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/sdb/active_sdb.rb', line 548 def reload raise_on_id_absence old_id = id attrs = connection.get_attributes(domain, id)[:attributes] @attributes = {} unless attrs.blank? attrs.each { |attribute, values| @attributes[attribute] = values } @attributes['id'] = old_id end mark_as_old @attributes end |
#reload_attributes(*attrs_list) ⇒ Object
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"]}
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/sdb/active_sdb.rb', line 571 def reload_attributes(*attrs_list) raise_on_id_absence attrs_list = attrs_list.flatten.map{ |attribute| attribute.to_s } attrs_list.delete('id') result = {} attrs_list.flatten.uniq.each do |attribute| attribute = attribute.to_s values = connection.get_attributes(domain, id, attribute)[:attributes][attribute] unless values.blank? @attributes[attribute] = result[attribute] = values else @attributes.delete(attribute) end end mark_as_old result end |
#save ⇒ Object
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
654 655 656 657 658 659 660 |
# File 'lib/sdb/active_sdb.rb', line 654 def save @attributes = uniq_values(@attributes) prepare_for_update connection.put_attributes(domain, id, @attributes, :replace) mark_as_old @attributes end |
#save_attributes(attrs) ⇒ Object
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
668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/sdb/active_sdb.rb', line 668 def save_attributes(attrs) prepare_for_update attrs = uniq_values(attrs) # if 'id' is present in attrs hash then replace internal 'id' attribute unless attrs['id'].blank? @attributes['id'] = attrs['id'] else attrs['id'] = id end connection.put_attributes(domain, id, attrs, :replace) unless attrs.blank? attrs.each { |attribute, values| attrs[attribute] = values } mark_as_old attrs end |