Class: FlowCore::Definition::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_core/definition/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net, tag, attributes = {}, &block) ⇒ Transition

Returns a new instance of Transition.

Raises:

  • (TypeError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flow_core/definition/transition.rb', line 8

def initialize(net, tag, attributes = {}, &block)
  raise TypeError unless net.is_a? FlowCore::Definition::Net
  raise TypeError unless tag.is_a? Symbol

  @net = net
  @tag = tag
  @input_tags = []
  @output_tags = []
  @callbacks = []

  input = attributes.delete(:input)
  output = attributes.delete(:output)
  raise ArgumentError, "Require `input`" unless input

  self.input input
  self.output output if output

  trigger = attributes.delete :with_trigger
  @trigger =
    if trigger
      FlowCore::Definition::Trigger.new trigger
    end

  callbacks = []
  callbacks.concat Array.wrap(attributes.delete(:with_callbacks))
  callbacks.concat Array.wrap(attributes.delete(:with_callback))
  @callbacks = callbacks.map { |cb| FlowCore::Definition::Callback.new cb }

  @attributes = attributes.with_indifferent_access.except(FlowCore::Transition::FORBIDDEN_ATTRIBUTES)
  @attributes[:name] ||= tag.to_s
  @attributes[:tag] ||= tag.to_s

  block&.call(self)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def attributes
  @attributes
end

#callbacksObject (readonly)

Returns the value of attribute callbacks.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def callbacks
  @callbacks
end

#input_tagsObject (readonly)

Returns the value of attribute input_tags.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def input_tags
  @input_tags
end

#output_tagsObject (readonly)

Returns the value of attribute output_tags.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def output_tags
  @output_tags
end

#tagObject (readonly)

Returns the value of attribute tag.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def tag
  @tag
end

#triggerObject (readonly)

Returns the value of attribute trigger.



5
6
7
# File 'lib/flow_core/definition/transition.rb', line 5

def trigger
  @trigger
end

Instance Method Details

#compileObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/flow_core/definition/transition.rb', line 96

def compile
  {
    tag: @tag,
    attributes: @attributes,
    trigger: @trigger&.compile,
    callbacks: @callbacks.map(&:compile),
    input_tags: @input_tags,
    output_tags: @output_tags.map { |output| { tag: output[:tag], guard: output[:guard]&.compile } }
  }
end

#input(tag) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/flow_core/definition/transition.rb', line 57

def input(tag)
  places = Array.wrap(tag)
  places.each do |place|
    case place
    when Symbol
      net.add_place(place)
      tag = place
    when Array # Expect `[:p1, {name: "Place 1"}]`
      net.add_place(*place)
      tag = place.first
    else
      raise TypeError, "Unknown pattern - #{place}"
    end

    @input_tags << tag
  end
end

#output(tag, attributes = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/flow_core/definition/transition.rb', line 75

def output(tag, attributes = {})
  guard = attributes.delete :with_guard
  guard = FlowCore::Definition::Guard.new(guard) if guard&.is_a?(Hash)

  places = Array.wrap(tag)
  places.each do |place|
    case place
    when Symbol
      net.add_place(place)
      tag = place
    when Array # Expect `[:p1, {name: "Place 1"}]`
      net.add_place(*place)
      tag = place.first
    else
      raise TypeError, "Unknown pattern - #{place}"
    end

    @output_tags << { tag: tag, guard: guard }
  end
end

#with_callback(type, attributes = {}) ⇒ Object



47
48
49
# File 'lib/flow_core/definition/transition.rb', line 47

def with_callback(type, attributes = {})
  @callbacks << FlowCore::Definition::Callback.new(attributes.merge(type: type))
end

#with_callbacks(*callbacks) ⇒ Object



51
52
53
54
55
# File 'lib/flow_core/definition/transition.rb', line 51

def with_callbacks(*callbacks)
  callbacks.each do |cb|
    with_callback(*cb)
  end
end

#with_trigger(type, attributes = {}) ⇒ Object



43
44
45
# File 'lib/flow_core/definition/transition.rb', line 43

def with_trigger(type, attributes = {})
  @trigger = FlowCore::Definition::Trigger.new attributes.merge(type: type)
end