Class: Patriarch::TransactionStep

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

Overview

Represents a “step” i.e. what should occur from no sql database point of view when a model instance invokes a behaviour. It is encapsulated into Patriarch::Transaction item and is never exposed. When needed, Patriarch::Transaction will forward some method call to the current TransactionStep that is being built

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation_type, actor, target, medium = nil) ⇒ TransactionStep

Returns a new instance of TransactionStep.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/patriarch/transaction_step.rb', line 9

def initialize(relation_type,actor,target,medium = nil)
  @context = {
    :relation_type  => relation_type,
    :actor_type     => actor.class.name.underscore.to_sym,
    :target_type    => target.class.name.underscore.to_sym,
    :actor_id       => actor.id,
    :target_id      => target.id,
  }
  if medium
    @context.merge! :medium_id => medium.id, :medium_type => medium.class.name.underscore.to_sym
  end
  @queue = []
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/patriarch/transaction_step.rb', line 7

def context
  @context
end

#queueObject

Returns the value of attribute queue.



7
8
9
# File 'lib/patriarch/transaction_step.rb', line 7

def queue
  @queue
end

Instance Method Details

#actorObject

Returns the actor of this step.

Returns:

  • the actor of this step



37
38
39
# File 'lib/patriarch/transaction_step.rb', line 37

def actor
  actor_type.to_s.camelize.constantize.find actor_id
end

#add_to_queue(struct) ⇒ Object

Parameters:



63
64
65
# File 'lib/patriarch/transaction_step.rb', line 63

def add_to_queue(struct)
  queue << struct
end

#executeObject

execute the redis instructions embedded into this step against instructions are a dao, a method to call upon and its argument embedded into a OpenStruct before Patriarch 0.2.5, was embedded directly in a proc, was a complete failure for unknown reasons Patriach::Transaction use this method on each of the steps it registered within a multi block



56
57
58
59
60
# File 'lib/patriarch/transaction_step.rb', line 56

def execute
  queue.each do |redis_struct_instruction|
    redis_struct_instruction.execute
  end
end

#mediumObject

Returns the medium of this step if it exists.

Returns:

  • the medium of this step if it exists



31
32
33
34
# File 'lib/patriarch/transaction_step.rb', line 31

def medium
  return nil unless medium_id && medium_type
  medium_type.to_s.camelize.constantize.find medium_id
end

#protagonists_modelsObject



46
47
48
49
50
# File 'lib/patriarch/transaction_step.rb', line 46

def protagonists_models 
  # By convention always actor, target, model for tripartite and 
  # actor, model for bipartite
  [actor,target,medium].compact.map(&:class)
end

#targetObject

Returns the target of this step.

Returns:

  • the target of this step



42
43
44
# File 'lib/patriarch/transaction_step.rb', line 42

def target
  target_type.to_s.camelize.constantize.find target_id
end