Class: Stator::Machine
- Inherits:
-
Object
- Object
- Stator::Machine
- Defined in:
- lib/stator/machine.rb
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#initial_state ⇒ Object
readonly
Returns the value of attribute initial_state.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#transition_names ⇒ Object
readonly
Returns the value of attribute transition_names.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
- #conditional(*states, &block) ⇒ Object
- #evaluate ⇒ Object
- #get_transition(name) ⇒ Object
-
#initialize(klass, options = {}) ⇒ Machine
constructor
A new instance of Machine.
- #integration(record) ⇒ Object
- #klass ⇒ Object
- #matching_transition(from, to) ⇒ Object
- #state(name, &block) ⇒ Object
- #state_alias(name, options = {}, &block) ⇒ Object
- #tracking_enabled? ⇒ Boolean
- #transition(name, &block) ⇒ Object
Constructor Details
#initialize(klass, options = {}) ⇒ Machine
Returns a new instance of Machine.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/stator/machine.rb', line 11 def initialize(klass, = {}) @class_name = klass.name @field = [:field] || :state @namespace = [:namespace] @initial_state = [:initial] && [:initial].to_s @tracking_enabled = [:track] || false @transitions = [] @aliases = [] # pushed out into their own variables for performance reasons (AR integration can use method missing - see the HelperMethods module) @transition_names = [] @states = [@initial_state].compact @options = end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
5 6 7 |
# File 'lib/stator/machine.rb', line 5 def field @field end |
#initial_state ⇒ Object (readonly)
Returns the value of attribute initial_state.
4 5 6 |
# File 'lib/stator/machine.rb', line 4 def initial_state @initial_state end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/stator/machine.rb', line 9 def namespace @namespace end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
8 9 10 |
# File 'lib/stator/machine.rb', line 8 def states @states end |
#transition_names ⇒ Object (readonly)
Returns the value of attribute transition_names.
6 7 8 |
# File 'lib/stator/machine.rb', line 6 def transition_names @transition_names end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
7 8 9 |
# File 'lib/stator/machine.rb', line 7 def transitions @transitions end |
Instance Method Details
#conditional(*states, &block) ⇒ Object
69 70 71 72 73 |
# File 'lib/stator/machine.rb', line 69 def conditional(*states, &block) _namespace = @namespace klass.instance_exec(proc { states.map(&:to_s).include?(self._stator(_namespace).integration(self).state) }, &block) end |
#evaluate ⇒ Object
81 82 83 84 85 |
# File 'lib/stator/machine.rb', line 81 def evaluate @transitions.each(&:evaluate) @aliases.each(&:evaluate) generate_methods end |
#get_transition(name) ⇒ Object
33 34 35 |
# File 'lib/stator/machine.rb', line 33 def get_transition(name) @transitions.detect{|t| t.name.to_s == name.to_s} end |
#integration(record) ⇒ Object
29 30 31 |
# File 'lib/stator/machine.rb', line 29 def integration(record) ::Stator::Integration.new(self, record) end |
#klass ⇒ Object
87 88 89 |
# File 'lib/stator/machine.rb', line 87 def klass @class_name.constantize end |
#matching_transition(from, to) ⇒ Object
75 76 77 78 79 |
# File 'lib/stator/machine.rb', line 75 def matching_transition(from, to) @transitions.detect do |transition| transition.valid?(from, to) end end |
#state(name, &block) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/stator/machine.rb', line 57 def state(name, &block) transition(nil) do from any to name instance_eval(&block) if block_given? end end |
#state_alias(name, options = {}, &block) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/stator/machine.rb', line 50 def state_alias(name, = {}, &block) a = ::Stator::Alias.new(self, name, ) a.instance_eval(&block) if block_given? @aliases << a a end |
#tracking_enabled? ⇒ Boolean
65 66 67 |
# File 'lib/stator/machine.rb', line 65 def tracking_enabled? @tracking_enabled end |
#transition(name, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/stator/machine.rb', line 37 def transition(name, &block) t = ::Stator::Transition.new(@class_name, name, @namespace) t.instance_eval(&block) if block_given? verify_transition_validity(t) @transitions << t @transition_names |= [t.full_name] unless t.full_name.blank? @states |= [t.to_state] unless t.to_state.nil? t end |