Class: Dynflow::ExecutionPlan::OutputReference

Inherits:
Serializable
  • Object
show all
Includes:
Algebrick::TypeCheck
Defined in:
lib/dynflow/execution_plan/output_reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Serializable

constantize, from_hash

Constructor Details

#initialize(execution_plan_id, step_id, action_id, subkeys = []) ⇒ OutputReference

Returns a new instance of OutputReference.



41
42
43
44
45
46
47
# File 'lib/dynflow/execution_plan/output_reference.rb', line 41

def initialize(execution_plan_id, step_id, action_id, subkeys = [])
  @execution_plan_id = Type! execution_plan_id, String
  @step_id           = Type! step_id, Integer
  @action_id         = Type! action_id, Integer
  Type! subkeys, Array
  @subkeys = subkeys.map { |v| Type!(v, String, Symbol).to_s }.freeze
end

Instance Attribute Details

#action_idObject (readonly)

Returns the value of attribute action_id.



39
40
41
# File 'lib/dynflow/execution_plan/output_reference.rb', line 39

def action_id
  @action_id
end

#execution_plan_idObject (readonly)

Returns the value of attribute execution_plan_id.



39
40
41
# File 'lib/dynflow/execution_plan/output_reference.rb', line 39

def execution_plan_id
  @execution_plan_id
end

#step_idObject (readonly)

Returns the value of attribute step_id.



39
40
41
# File 'lib/dynflow/execution_plan/output_reference.rb', line 39

def step_id
  @step_id
end

#subkeysObject (readonly)

Returns the value of attribute subkeys.



39
40
41
# File 'lib/dynflow/execution_plan/output_reference.rb', line 39

def subkeys
  @subkeys
end

Class Method Details

.dereference(object, persistence) ⇒ Object

dereferences all OutputReferences in Hash-Array structure



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dynflow/execution_plan/output_reference.rb', line 6

def self.dereference(object, persistence)
  case object
  when Hash
    object.reduce(HashWithIndifferentAccess.new) do |h, (key, val)|
      h.update(key => dereference(val, persistence))
    end
  when Array
    object.map { |val| dereference(val, persistence) }
  when self
    object.dereference(persistence)
  else
    object
  end
end

.deserialize(value) ⇒ Object

dereferences all hashes representing OutputReferences in Hash-Array structure



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynflow/execution_plan/output_reference.rb', line 22

def self.deserialize(value)
  case value
  when Hash
    if value[:class] == self.to_s
      new_from_hash(value)
    else
      value.reduce(HashWithIndifferentAccess.new) do |h, (key, val)|
        h.update(key => deserialize(val))
      end
    end
  when Array
    value.map { |val| deserialize(val) }
  else
    value
  end
end

Instance Method Details

#[](subkey) ⇒ Object



49
50
51
# File 'lib/dynflow/execution_plan/output_reference.rb', line 49

def [](subkey)
  self.class.new(execution_plan_id, step_id, action_id, subkeys + [subkey])
end

#dereference(persistence) ⇒ Object



69
70
71
72
# File 'lib/dynflow/execution_plan/output_reference.rb', line 69

def dereference(persistence)
  action_data = persistence.adapter.load_action(execution_plan_id, action_id)
  @subkeys.reduce(action_data[:output]) { |v, k| v.fetch k }
end

#to_hashObject



53
54
55
56
57
58
59
# File 'lib/dynflow/execution_plan/output_reference.rb', line 53

def to_hash
  recursive_to_hash class:             self.class.to_s,
                    execution_plan_id: execution_plan_id,
                    step_id:           step_id,
                    action_id:         action_id,
                    subkeys:           subkeys
end

#to_sObject Also known as: inspect



61
62
63
64
65
# File 'lib/dynflow/execution_plan/output_reference.rb', line 61

def to_s
  "Step(#{step_id}).output".tap do |ret|
    ret << subkeys.map { |k| "[:#{k}]" }.join('') if subkeys.any?
  end
end