class Patriarch::Transaction
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.
Attributes
Public Class Methods
# 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
Public Instance Methods
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
# 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
# File lib/patriarch/transaction.rb, line 86 def add_to_queue(instructions) current_step.add_to_queue(instructions) end
# File lib/patriarch/transaction.rb, line 92 def current_step steps[current_step_number-1] end
Executes the calls to redis in one block here.
# 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
# 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
# File lib/patriarch/transaction.rb, line 74 def tripartite? !medium.nil? end