Class: Patriarch::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/patriarch/transaction.rb

Overview

Class made to handle transaction even cascading ones (i.e behaviours called in the after callback of other behaviour) It serves as an interface that hide the different steps included in a transaction and allow us to decide when to perform a transaction with #execute once transaction has been built Passing an instance of Patriarch::Tansaction around will in some cases prevent from building another transaction and hence write the following events (transaction steps) in this instance Thus, it will build a transaction that engluf all the transaction step and can be performed entirely in one time It is useful to permit sql rollbacks to occur before the transaction is being executed when destroying items with a cascading effect. It covers the edge case where a cascading destroy that would rollback at step 3 would have performed the 3 destroy into the no sql database model before.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id) ⇒ Transaction

relation_type,actor,target,



28
29
30
31
32
33
# File 'lib/patriarch/transaction.rb', line 28

def initialize(type,id) #relation_type,actor,target,
  @type = type 
  @id = id 
  @steps = []
  @current_step_number = 0
end

Instance Attribute Details

#current_step_numberObject (readonly)

Returns the value of attribute current_step_number.



12
13
14
# File 'lib/patriarch/transaction.rb', line 12

def current_step_number
  @current_step_number
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/patriarch/transaction.rb', line 12

def id
  @id
end

#stepsObject

Returns the value of attribute steps.



12
13
14
# File 'lib/patriarch/transaction.rb', line 12

def steps
  @steps
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/patriarch/transaction.rb', line 14

def type
  @type
end

Instance Method Details

#add_step(relation_type, actor, target, medium = nil) ⇒ Object

Initializes a new step and stores it in the steps array right away A step matches a “like”, “follow”, “own”, etc. Register that we went a step further into the transaction processus Medium is nil by default (bipartite transactions does not require more than target/actor) It is there for later support of tripartite transactions



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/patriarch/transaction.rb', line 40

def add_step(relation_type,actor,target,medium=nil)
  # Initializes a new step and stores it in the steps array right away
  new_step = Patriarch::TransactionStep.new(relation_type,actor,target,medium)
  
  # if initilization failed we should not move forward ...
  raise PatriarchTransactionStepInstanciationException unless new_step

  # Register that we went a step further into the transaction processus
  @steps << new_step 
  @current_step_number += 1
end

#add_to_queue(instructions) ⇒ Object



86
87
88
# File 'lib/patriarch/transaction.rb', line 86

def add_to_queue(instructions)
  current_step.add_to_queue(instructions)
end

#current_stepObject



92
93
94
# File 'lib/patriarch/transaction.rb', line 92

def current_step
  steps[current_step_number-1]
end

#executeObject

Executes the calls to redis in one block here. – XXX Take care when decoupling redis logoc from gem later ++



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/patriarch/transaction.rb', line 56

def execute
  begin
    $redis.multi do
      steps.each do |step|
        if step.is_a? TransactionStep
          step.execute
        elsif step.is_a? Transaction
          step.steps.each do |nested_step|
            nested_step.execute
          end
        end
      end
    end
  rescue Redis::BaseError => e #TODO find redis exception
    raise ActiveRecord::Rollback, "redis error #{e.class.name} occured hence triggered transaction rollback"
  end
end

#transaction_queueObject Also known as: queue



78
79
80
81
82
83
84
# File 'lib/patriarch/transaction.rb', line 78

def transaction_queue
  transaction_queue = []
  steps.each do |step|
    transaction_queue.concat(step.queue)
  end
  transaction_queue
end

#tripartite?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/patriarch/transaction.rb', line 74

def tripartite?
  !medium.nil?
end