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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hyperactive/record.rb', line 56

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
    #
    # We depend on hooks.
    #
    include(Hyperactive::Hooker::Pimp)
  end

  base.extend(ClassMethods)
end

Instance Method Details

#<=>(o) ⇒ Object

Utility compare method. Override as you please.



105
106
107
108
109
110
111
# File 'lib/hyperactive/record.rb', line 105

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.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hyperactive/record.rb', line 118

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.



136
137
138
139
140
141
142
# File 'lib/hyperactive/record.rb', line 136

def destroy!
  self.class.with_hooks(:instance => self, :hooks => self.class.destroy_hooks) do
    Archipelago::Pirate::BLACKBEARD.delete(@record_id, @transaction)
    self.freeze
  end
  return nil
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.



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

def with_transaction(transaction, &block)
  old_transaction = @transaction if defined?(@transaction)
  @transaction = transaction
  begin
    return yield
  ensure
    @transaction = old_transaction
  end
end