Class: FlowCore::Definition::Net

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ Net

Returns a new instance of Net.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
14
15
# File 'lib/flow_core/definition/net.rb', line 7

def initialize(attributes = {})
  @attributes = attributes.with_indifferent_access.except(FlowCore::Workflow::FORBIDDEN_ATTRIBUTES)
  @places = []
  @transitions = []
  @start_tag = nil
  @end_tag = nil

  yield(self)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#end_tagObject (readonly)

Returns the value of attribute end_tag.



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

def end_tag
  @end_tag
end

#placesObject (readonly)

Returns the value of attribute places.



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

def places
  @places
end

#start_tagObject (readonly)

Returns the value of attribute start_tag.



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

def start_tag
  @start_tag
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Instance Method Details

#add_place(tag_or_place, attributes = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flow_core/definition/net.rb', line 17

def add_place(tag_or_place, attributes = {})
  entity =
    if tag_or_place.is_a? FlowCore::Definition::Place
      tag_or_place
    else
      attributes[:name] ||= tag_or_place.to_s
      FlowCore::Definition::Place.new(tag_or_place, attributes)
    end

  @places << entity unless @places.include?(entity)
  entity
end

#compileObject



55
56
57
58
59
60
61
62
63
# File 'lib/flow_core/definition/net.rb', line 55

def compile
  {
    attributes: @attributes,
    start_tag: @start_tag,
    end_tag: @end_tag,
    places: @places.map(&:compile),
    transitions: @transitions.map(&:compile)
  }
end

#deploy!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/flow_core/definition/net.rb', line 65

def deploy!
  # TODO: Simple validation

  workflow = nil
  FlowCore::ApplicationRecord.transaction do
    workflow = FlowCore::Workflow.new attributes
    workflow.save!

    # Places
    place_records = {}
    places.each do |pd|
      place_records[pd.tag] =
        workflow.places.create! pd.attributes
    end

    # Transitions
    transition_records = {}
    transitions.each do |td|
      transition_records[td.tag] =
        workflow.transitions.create! td.attributes

      if td.trigger
        transition_records[td.tag].create_trigger! td.trigger.compile
      end

      td.callbacks.each do |cb|
        transition_records[td.tag].callbacks.create! cb.compile
      end

      td.input_tags.each do |place_tag|
        workflow.arcs.in.create! transition: transition_records[td.tag],
                                 place: place_records[place_tag]
      end

      td.output_tags.each do |output|
        arc = workflow.arcs.out.create! transition: transition_records[td.tag],
                                        place: place_records[output[:tag]],
                                        fallback_arc: output[:fallback]
        output[:guards].each do |guard|
          arc.guards.create! guard.compile
        end
      end
    end
  end
  workflow&.verify!
  workflow
end

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



39
40
41
42
43
44
45
46
# File 'lib/flow_core/definition/net.rb', line 39

def end_place(tag, attributes = {})
  raise "`end_place` can only call once" if @end_tag

  place = FlowCore::Definition::Place.new(tag, attributes.merge(type: FlowCore::EndPlace.to_s))
  @places << place
  @end_tag = place.tag
  place
end

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



30
31
32
33
34
35
36
37
# File 'lib/flow_core/definition/net.rb', line 30

def start_place(tag, attributes = {})
  raise "`start_place` can only call once" if @start_tag

  place = FlowCore::Definition::Place.new(tag, attributes.merge(type: FlowCore::StartPlace.to_s))
  @places << place
  @start_tag = place.tag
  place
end

#transition(tag, options = {}, &block) ⇒ Object

Raises:

  • (TypeError)


48
49
50
51
52
53
# File 'lib/flow_core/definition/net.rb', line 48

def transition(tag, options = {}, &block)
  raise TypeError unless tag.is_a? Symbol
  raise ArgumentError if @transitions.include? tag

  @transitions << FlowCore::Definition::Transition.new(self, tag, options, &block)
end