Class: FlowState::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- FlowState::Base
show all
- Defined in:
- lib/flow_state/base.rb
Overview
Base Model to be extended by app flows
Defined Under Namespace
Classes: GuardFailedError, InvalidTransitionError, MissingCompletedStateError, MissingInitialStateError, PayloadValidationError, PropsValidationError, UnknownArtefactError, UnknownStateError
Constant Summary
collapse
- DEPRECATOR =
ActiveSupport::Deprecation.new(FlowState::VERSION, 'FlowState')
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.all_states ⇒ Object
58
59
60
|
# File 'lib/flow_state/base.rb', line 58
def all_states
@all_states ||= []
end
|
.artefact_schema ⇒ Object
70
71
72
|
# File 'lib/flow_state/base.rb', line 70
def artefact_schema
@artefact_schema ||= {}
end
|
.completed_state(name = nil) ⇒ Object
38
39
40
|
# File 'lib/flow_state/base.rb', line 38
def completed_state(name = nil)
name ? @completed_state = name.to_sym : @completed_state
end
|
.destroy_on_complete(flag: true) ⇒ Object
42
43
44
|
# File 'lib/flow_state/base.rb', line 42
def destroy_on_complete(flag: true)
@destroy_on_complete = flag
end
|
.destroy_on_complete? ⇒ Boolean
46
47
48
|
# File 'lib/flow_state/base.rb', line 46
def destroy_on_complete?
!!@destroy_on_complete
end
|
.error_states ⇒ Object
62
63
64
|
# File 'lib/flow_state/base.rb', line 62
def error_states
@error_states ||= []
end
|
.initial_state(name = nil) ⇒ Object
34
35
36
|
# File 'lib/flow_state/base.rb', line 34
def initial_state(name = nil)
name ? @initial_state = name.to_sym : @initial_state
end
|
.persists(name, type) ⇒ Object
54
55
56
|
# File 'lib/flow_state/base.rb', line 54
def persists(name, type)
artefact_schema[name.to_sym] = type
end
|
.prop(name, type) ⇒ Object
50
51
52
|
# File 'lib/flow_state/base.rb', line 50
def prop(name, type)
props_schema[name.to_sym] = type
end
|
.props_schema ⇒ Object
66
67
68
|
# File 'lib/flow_state/base.rb', line 66
def props_schema
@props_schema ||= {}
end
|
.state(name, error: false) ⇒ Object
28
29
30
31
32
|
# File 'lib/flow_state/base.rb', line 28
def state(name, error: false)
name = name.to_sym
all_states << name
error_states << name if error
end
|
Instance Method Details
#completed? ⇒ Boolean
92
93
94
|
# File 'lib/flow_state/base.rb', line 92
def completed?
self.class.completed_state && current_state&.to_sym == self.class.completed_state
end
|
#errored? ⇒ Boolean
88
89
90
|
# File 'lib/flow_state/base.rb', line 88
def errored?
self.class.error_states.include?(current_state&.to_sym)
end
|
#handle_completion ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/flow_state/base.rb', line 96
def handle_completion
return unless completed?
if self.class.destroy_on_complete?
destroy!
elsif completed_at.nil?
update_column(:completed_at, Time.current)
end
end
|
#transition!(from:, to:, guard: nil, persist: nil, after_transition: nil, &block) ⇒ Object
82
83
84
85
86
|
# File 'lib/flow_state/base.rb', line 82
def transition!(from:, to:, guard: nil, persist: nil, after_transition: nil, &block)
setup_transition!(from, to, guard, persist, &block)
perform_transition!(to, persist)
after_transition&.call
end
|