Class: Hyperactive::Record::Bass
- Inherits:
-
Object
- Object
- Hyperactive::Record::Bass
- Defined in:
- lib/hyperactive/record.rb
Overview
A convenient base class to inherit when you want the basic utility methods provided by for example ActiveRecord::Base *hint hint*.
NB: When an instance is created you will actually have a copy within your local machine which is not what you usually want. Every other time you fetch it using a select or other method you will instead receive a proxy object to the database. This means that nothing you do to it at that point will be persistent or even necessarily have a defined result. Therefore: do not use the instantiated object, instead call my_instance.save to get a proxy to the object stored into the database.
Direct Known Subclasses
Constant Summary collapse
- HOST =
The host we are running on.
"#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"
Instance Attribute Summary collapse
-
#record_id ⇒ Object
readonly
Our semi-unique id.
Attributes included from Transactions::Participant
Class Method Summary collapse
-
.find(record_id, transaction = nil) ⇒ Object
Return the record with
record_id, optionally within atransaction. -
.get_instance(*args) ⇒ Object
Utility method to get a proxy to a newly saved instance of this class in one call.
Instance Method Summary collapse
-
#<=>(o) ⇒ Object
Utility compare method.
-
#create ⇒ Object
Save this Record instance into the distributed database and return a proxy to the saved object.
-
#destroy! ⇒ Object
Remove this instance from the database wrapped in our destroy_hooks.
-
#initialize ⇒ Bass
constructor
A new instance of Bass.
Methods included from Hooker::Pimp
append_features, #load_hook, #save_hook
Methods included from Transactions::Participant
append_features, #with_transaction
Methods included from Transactions::Accessors
Methods included from Index::Indexable
Constructor Details
#initialize ⇒ Bass
Returns a new instance of Bass.
93 94 95 96 |
# File 'lib/hyperactive/record.rb', line 93 def initialize @record_id = nil @transaction = nil end |
Instance Attribute Details
#record_id ⇒ Object (readonly)
Our semi-unique id.
80 81 82 |
# File 'lib/hyperactive/record.rb', line 80 def record_id @record_id end |
Class Method Details
.find(record_id, transaction = nil) ⇒ Object
Return the record with record_id, optionally within a transaction.
65 66 67 |
# File 'lib/hyperactive/record.rb', line 65 def self.find(record_id, transaction = nil) CAPTAIN[record_id, transaction] end |
.get_instance(*args) ⇒ Object
Utility method to get a proxy to a newly saved instance of this class in one call.
72 73 74 75 |
# File 'lib/hyperactive/record.rb', line 72 def self.get_instance(*args) instance = self.new(*args) return instance.create end |
Instance Method Details
#<=>(o) ⇒ Object
Utility compare method. Override as you please.
85 86 87 88 89 90 91 |
# File 'lib/hyperactive/record.rb', line 85 def <=>(o) if Record === o @record_id <=> o.record_id else 0 end end |
#create ⇒ Object
Save this Record instance into the distributed database and return a proxy to the saved object.
This will also wrap the actual insertion within the create_hooks you have defined for this class.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/hyperactive/record.rb', line 103 def create @record_id ||= Digest::SHA1.hexdigest("#{HOST}:#{Time.new.to_f}:#{self.object_id}:#{rand(1 << 32)}").to_s @transaction ||= nil self.class.with_hooks(:instance => self, :hooks => self.class.create_hooks) do CAPTAIN[self.record_id, @transaction] = self end proxy = CAPTAIN[@record_id, @transaction] return proxy end |
#destroy! ⇒ Object
Remove this instance from the database wrapped in our destroy_hooks.
Freezes this instance after having deleted it.
121 122 123 124 125 126 |
# File 'lib/hyperactive/record.rb', line 121 def destroy! self.class.with_hooks(:instance => self, :hooks => self.class.destroy_hooks) do CAPTAIN.delete(@record_id, @transaction) self.freeze end end |