Class: LcpRuby::Dsl::WorkflowBuilder

Inherits:
Object
  • Object
show all
Includes:
SourceLocationCapture
Defined in:
lib/lcp_ruby/dsl/workflow_builder.rb

Defined Under Namespace

Classes: ApprovalBlockContext, StepBlockContext, TransitionBlockContext

Instance Method Summary collapse

Methods included from SourceLocationCapture

capture_source_loc

Constructor Details

#initialize(name) ⇒ WorkflowBuilder

Returns a new instance of WorkflowBuilder.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 8

def initialize(name)
  @name = name.to_s
  @model_value = nil
  @field_value = nil
  @version_value = nil
  @audit_log_value = false
  @timeline_states_value = nil
  @states = {}
  @transitions = {}
  @approvals = {}
end

Instance Method Details

#approval(state_name, &block) ⇒ Object



97
98
99
100
101
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 97

def approval(state_name, &block)
  context = ApprovalBlockContext.new
  context.instance_eval(&block)
  @approvals[state_name.to_s] = context.to_hash
end

#audit_log(value = true) ⇒ Object



32
33
34
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 32

def audit_log(value = true)
  @audit_log_value = value
end

#field(value) ⇒ Object



24
25
26
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 24

def field(value)
  @field_value = value.to_s
end

#model(value) ⇒ Object



20
21
22
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 20

def model(value)
  @model_value = value.to_s
end

#state(name, **opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 40

def state(name, **opts)
  state_hash = {}
  state_hash["initial"] = opts[:initial] if opts.key?(:initial)
  state_hash["category"] = opts[:category].to_s if opts[:category]
  state_hash["color"] = opts[:color].to_s if opts[:color]
  state_hash["icon"] = opts[:icon].to_s if opts[:icon]
  if opts[:readonly_fields]
    state_hash["readonly_fields"] = opts[:readonly_fields] == :all ? "all" : opts[:readonly_fields].map(&:to_s)
  end
  state_hash["on_entry"] = Array(opts[:on_entry]).map(&:to_s) if opts[:on_entry]
  state_hash["on_exit"] = Array(opts[:on_exit]).map(&:to_s) if opts[:on_exit]
  if opts[:description]
    state_hash["description"] = opts[:description].to_s
    state_hash["_description_source_loc"] = capture_source_loc
  end
  # Capture state declaration site for missing-translation reports
  # on the state name (Tier 1 missing_only).
  state_hash["_source_loc"] = capture_source_loc
  @states[name.to_s] = state_hash
end

#timeline_states(*states) ⇒ Object



36
37
38
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 36

def timeline_states(*states)
  @timeline_states_value = states.flatten.map(&:to_s)
end

#to_hashObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 103

def to_hash
  hash = { "name" => @name }
  hash["model"] = @model_value if @model_value
  hash["field"] = @field_value if @field_value
  hash["version"] = @version_value if @version_value
  hash["audit_log"] = @audit_log_value if @audit_log_value
  hash["timeline_states"] = @timeline_states_value if @timeline_states_value
  hash["states"] = @states unless @states.empty?
  hash["transitions"] = @transitions unless @transitions.empty?
  hash["approvals"] = @approvals unless @approvals.empty?
  hash
end

#transition(name, **opts, &block) ⇒ Object



61
62
63
64
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
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 61

def transition(name, **opts, &block)
  trans_hash = {}
  if opts[:from] == :* || opts[:from] == "*"
    trans_hash["from"] = "*"
  elsif opts[:from]
    trans_hash["from"] = Array(opts[:from]).map(&:to_s)
  end
  trans_hash["to"] = opts[:to].to_s if opts[:to]
  if opts[:label]
    trans_hash["label"] = opts[:label].to_s
    trans_hash["_label_source_loc"] = capture_source_loc
  end
  trans_hash["icon"] = opts[:icon].to_s if opts[:icon]
  trans_hash["style"] = opts[:style].to_s if opts[:style]
  trans_hash["roles"] = Array(opts[:roles]).map(&:to_s) if opts[:roles]
  trans_hash["require_comment"] = opts[:require_comment] if opts.key?(:require_comment)
  trans_hash["confirm"] = opts[:confirm] if opts.key?(:confirm)
  # Source captured once for the transition's own declaration; the
  # confirm_message literal shares it (same file:line). Pass 1
  # missing_only mode handles transitions, so confirm_message
  # capture is forward-compat for a future Tier-2 sub-flag.
  trans_hash["_source_loc"] ||= capture_source_loc
  trans_hash["confirm_message"] = opts[:confirm_message].to_s if opts[:confirm_message]
  trans_hash["trigger"] = opts[:trigger].to_s if opts[:trigger]

  if block
    context = TransitionBlockContext.new
    context.instance_eval(&block)
    trans_hash["guard"] = context.guard_condition if context.guard_condition
    trans_hash["set_fields"] = context.fields_to_set unless context.fields_to_set.empty?
    trans_hash["events"] = context.events_to_fire unless context.events_to_fire.empty?
  end

  @transitions[name.to_s] = trans_hash
end

#version(value) ⇒ Object



28
29
30
# File 'lib/lcp_ruby/dsl/workflow_builder.rb', line 28

def version(value)
  @version_value = value
end