Class: Hyperactive::Record::Bass

Inherits:
Object
  • Object
show all
Includes:
Hooker::Pimp, Index::Indexable, Transactions::Accessors, Transactions::Participant
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

Hash::Element, Hash::Head, List::Element, List::Head

Constant Summary collapse

HOST =

The host we are running on.

"#{Socket::gethostbyname(Socket::gethostname)[0]}" rescue "localhost"

Instance Attribute Summary collapse

Attributes included from Transactions::Participant

#transaction

Class Method Summary collapse

Instance Method Summary collapse

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

append_features

Methods included from Index::Indexable

append_features

Constructor Details

#initializeBass

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_idObject (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

#createObject

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