Module: Phronomy::WorkflowContext

Defined in:
lib/phronomy/workflow_context.rb

Overview

Module for defining Workflow context data.

In StateChart terminology this is the extended state/context, as opposed to the current FSM phase.

An application context may define handle_fsm_event(event). FSMSession calls it on the EventLoop thread before evaluating a declared transition. The method may mutate the context and return false to continue transition evaluation, return a replacement WorkflowContext, or return :consume to discard a stale/unrelated event without firing a transition.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#thread_idObject (readonly)

Returns the value of attribute thread_id.



43
44
45
# File 'lib/phronomy/workflow_context.rb', line 43

def thread_id
  @thread_id
end

Class Method Details

.included(base) ⇒ Object



15
16
17
18
# File 'lib/phronomy/workflow_context.rb', line 15

def self.included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@fields, {})
end

Instance Method Details

#halted?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/phronomy/workflow_context.rb', line 49

def halted?
  phase != :__end__
end

#initialize(**attrs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/phronomy/workflow_context.rb', line 59

def initialize(**attrs)
  unknown = attrs.keys - self.class.fields.keys
  unless unknown.empty?
    raise ArgumentError,
      "Unknown WorkflowContext field(s): #{unknown.inspect}"
  end

  self.class.fields.each do |name, config|
    default =
      if config[:default].is_a?(Proc)
        config[:default].call
      else
        config[:default]
      end
    instance_variable_set(
      :"@#{name}",
      attrs.fetch(name, default)
    )
  end
  @thread_id = nil
  @phase = :__end__
end

#merge(updates) ⇒ Object

Returns a new context with field update policies applied.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/phronomy/workflow_context.rb', line 83

def merge(updates)
  unknown = updates.keys - self.class.fields.keys
  unless unknown.empty?
    raise ArgumentError,
      "Unknown WorkflowContext field(s): #{unknown.inspect}"
  end

  new_attrs = {}
  self.class.fields.each_key do |name|
    field_config = self.class.fields[name]
    new_attrs[name] =
      if updates.key?(name)
        case field_config[:type]
        when :append
          Array(public_send(name)) + Array(updates[name])
        when :merge
          (public_send(name) || {}).merge(updates[name])
        else
          updates[name]
        end
      else
        deep_dup_value(public_send(name))
      end
  end

  new_context = self.class.new(**new_attrs)
  new_context.(
    thread_id: @thread_id,
    phase: @phase
  )
  new_context
end

#phaseObject



45
46
47
# File 'lib/phronomy/workflow_context.rb', line 45

def phase
  @phase || :__end__
end

#set_graph_metadata(thread_id: nil, phase: nil) ⇒ Object



53
54
55
56
57
# File 'lib/phronomy/workflow_context.rb', line 53

def (thread_id: nil, phase: nil)
  @thread_id = thread_id unless thread_id.nil?
  @phase = phase unless phase.nil?
  self
end

#to_hObject



116
117
118
119
120
# File 'lib/phronomy/workflow_context.rb', line 116

def to_h
  self.class.fields.keys.each_with_object({}) do |name, result|
    result[name] = public_send(name)
  end
end