Module: Hyperactive::Record::Persistent

Included in:
Bass
Defined in:
lib/hyperactive/record.rb

Overview

The methods that make Hyperactive::Record::Bass persistent.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

Give the class a @record_id getter/setter pair, @transaction getter/setter pair, our instance methods and the class methods of ClassMethods.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hyperactive/record.rb', line 48

def self.append_features(base)
  super

  base.class_eval do
    #
    # Our semi-unique id.
    #
    attr_reader :record_id
    #
    # The transaction we are currently in.
    #
    attr_reader :transaction
  end

  base.extend(ClassMethods)
end

Instance Method Details

#<=>(o) ⇒ Object

Utility compare method. Override as you please.



92
93
94
95
96
97
98
# File 'lib/hyperactive/record.rb', line 92

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.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hyperactive/record.rb', line 105

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
    Archipelago::Pirate::BLACKBEARD[self.record_id, @transaction] = self
  end
  
  proxy = Archipelago::Pirate::BLACKBEARD[@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.



123
124
125
126
127
128
# File 'lib/hyperactive/record.rb', line 123

def destroy!
  self.class.with_hooks(:instance => self, :hooks => self.class.destroy_hooks) do
    Archipelago::Pirate::BLACKBEARD.delete(@record_id, @transaction)
    self.freeze
  end
end

#with_transaction(transaction, &block) ⇒ Object

Will execute block within a transaction.

What it does is just set the @transaction instance variable before calling the block, and unsetting it after.

This means that any classes that want to be transaction sensitive need to take heed regarding the @transaction instance variable.

For example, when creating new Record instances you may want to use get_instance_with_transaction(@transaction, *args) to ensure that the new instance exists within the same transaction as yourself.

See Hyperactive::List::Head and Hyperactive::Hash::Head for examples of this behaviour.



80
81
82
83
84
85
86
87
# File 'lib/hyperactive/record.rb', line 80

def with_transaction(transaction, &block)
  @transaction = transaction
  begin
    return yield
  ensure
    @transaction = nil
  end
end