Class: FlowCore::Definition::Transition
- Inherits:
-
Object
- Object
- FlowCore::Definition::Transition
- Defined in:
- lib/flow_core/definition/transition.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#input_tags ⇒ Object
readonly
Returns the value of attribute input_tags.
-
#output_tags ⇒ Object
readonly
Returns the value of attribute output_tags.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#trigger ⇒ Object
readonly
Returns the value of attribute trigger.
Instance Method Summary collapse
- #compile ⇒ Object
-
#initialize(net, tag, attributes = {}, &block) ⇒ Transition
constructor
A new instance of Transition.
- #input(tag, attributes = {}) ⇒ Object
- #inputs(*tags) ⇒ Object
- #output(tag, attributes = {}) ⇒ Object
- #outputs(*tags) ⇒ Object
- #with_callback(type, attributes = {}) ⇒ Object
- #with_callbacks(*callbacks) ⇒ Object
- #with_trigger(type, attributes = {}) ⇒ Object
Constructor Details
#initialize(net, tag, attributes = {}, &block) ⇒ Transition
Returns a new instance of Transition.
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 42 |
# File 'lib/flow_core/definition/transition.rb', line 9 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 inputs(*input) outputs(*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.compact.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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/flow_core/definition/transition.rb', line 5 def attributes @attributes end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
5 6 7 |
# File 'lib/flow_core/definition/transition.rb', line 5 def callbacks @callbacks end |
#input_tags ⇒ Object (readonly)
Returns the value of attribute input_tags.
5 6 7 |
# File 'lib/flow_core/definition/transition.rb', line 5 def @input_tags end |
#output_tags ⇒ Object (readonly)
Returns the value of attribute output_tags.
5 6 7 |
# File 'lib/flow_core/definition/transition.rb', line 5 def @output_tags end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
5 6 7 |
# File 'lib/flow_core/definition/transition.rb', line 5 def tag @tag end |
#trigger ⇒ Object (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
#compile ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/flow_core/definition/transition.rb', line 108 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], guards: output[:guards].map(&:compile) } } } end |
#input(tag, attributes = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/flow_core/definition/transition.rb', line 58 def input(tag, attributes = {}) unless net.places.find { |place| place.tag == tag } net.add_place(tag, attributes) end @input_tags << tag end |
#inputs(*tags) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flow_core/definition/transition.rb', line 66 def inputs(*) .each do |tag| case tag when Symbol input(tag) when Array # Expect `[:p1, {name: "Place 1"}]` input(*tag) else raise TypeError, "Unknown pattern - #{place}" end end end |
#output(tag, attributes = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/flow_core/definition/transition.rb', line 79 def output(tag, attributes = {}) guards = [] guards.concat Array.wrap(attributes.delete(:with_guards)) guards.concat Array.wrap(attributes.delete(:with_guard)) guards.compact! guards.map! { |guard| FlowCore::Definition::Guard.new guard } fallback = attributes.delete(:fallback) || false unless net.places.find { |place| place.tag == tag } net.add_place(tag, attributes) end @output_tags << { tag: tag, guards: guards, fallback: fallback } end |
#outputs(*tags) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/flow_core/definition/transition.rb', line 95 def outputs(*) .each do |tag| case tag when Symbol output(tag) when Array # Expect `[:p1, {name: "Place 1"}]` output(*tag) else raise TypeError, "Unknown pattern - #{place}" end end end |
#with_callback(type, attributes = {}) ⇒ Object
48 49 50 |
# File 'lib/flow_core/definition/transition.rb', line 48 def with_callback(type, attributes = {}) @callbacks << FlowCore::Definition::Callback.new(attributes.merge(type: type)) end |
#with_callbacks(*callbacks) ⇒ Object
52 53 54 55 56 |
# File 'lib/flow_core/definition/transition.rb', line 52 def with_callbacks(*callbacks) callbacks.each do |cb| with_callback(*cb) end end |
#with_trigger(type, attributes = {}) ⇒ Object
44 45 46 |
# File 'lib/flow_core/definition/transition.rb', line 44 def with_trigger(type, attributes = {}) @trigger = FlowCore::Definition::Trigger.new attributes.merge(type: type) end |