Class: FlowCore::Workflow

Inherits:
ApplicationRecord show all
Defined in:
app/models/flow_core/workflow.rb

Constant Summary collapse

FORBIDDEN_ATTRIBUTES =
%i[verified verified_at created_at updated_at].freeze

Instance Method Summary collapse

Instance Method Details

#create_instance!(attributes = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'app/models/flow_core/workflow.rb', line 18

def create_instance!(attributes = {})
  unless verified?
    raise FlowCore::UnverifiedWorkflow, "Workflow##{id} didn't do soundness check yet."
  end

  instances.create! attributes.with_indifferent_access.except(FlowCore::Instance::FORBIDDEN_ATTRIBUTES)
end

#invalid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/flow_core/workflow.rb', line 26

def invalid?
  !verified?
end

#reset_workflow_verification!Object



103
104
105
# File 'app/models/flow_core/workflow.rb', line 103

def reset_workflow_verification!
  update! verified: false, verified_at: nil
end

#verify!Object



98
99
100
101
# File 'app/models/flow_core/workflow.rb', line 98

def verify!
  update! verified: verify?, verified_at: Time.zone.now
  violations.empty?
end

#verify?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/flow_core/workflow.rb', line 30

def verify?
  violations.clear

  unless start_place
    violations.add(:start_place, :presence)
  end
  unless end_place
    violations.add(:end_place, :presence)
  end

  return false unless start_place && end_place

  # TODO: Naive implementation for now, Help wanted!

  rgl = to_rgl

  start_place_code = "P_#{start_place.id}"
  end_place_code = "P_#{end_place.id}"

  unless rgl.path?(start_place_code, end_place_code)
    violations.add :end_place, :unreachable
  end

  places.find_each do |p|
    next if p == start_place
    next if p == end_place

    place_code = "P_#{p.id}"

    unless rgl.path?(start_place_code, place_code)
      violations.add p, :unreachable
    end

    unless rgl.path?(place_code, end_place_code)
      violations.add p, :impassable
    end
  end
  transitions.includes(:trigger).find_each do |t|
    transition_code = "T_#{t.id}"

    unless rgl.path?(start_place_code, transition_code)
      violations.add t, :unreachable
    end

    unless rgl.path?(transition_code, end_place_code)
      violations.add t, :impassable
    end

    t.verify violations: violations
  end

  violations.empty?
end

#verify_statusObject



88
89
90
91
92
93
94
95
96
# File 'app/models/flow_core/workflow.rb', line 88

def verify_status
  if verified_at.blank?
    :unverified
  elsif verified?
    :verified
  else
    :invalid
  end
end

#violationsObject



84
85
86
# File 'app/models/flow_core/workflow.rb', line 84

def violations
  @violations ||= FlowCore::Violations.new
end