Class: Stator::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/stator/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @class_name       = klass.name
  @field            = options[:field] || :state
  @namespace        = options[:namespace]

  @initial_state    = options[:initial] && options[:initial].to_s
  @tracking_enabled = options[: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       = options
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/stator/machine.rb', line 5

def field
  @field
end

#initial_stateObject (readonly)

Returns the value of attribute initial_state.



4
5
6
# File 'lib/stator/machine.rb', line 4

def initial_state
  @initial_state
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'lib/stator/machine.rb', line 9

def namespace
  @namespace
end

#statesObject (readonly)

Returns the value of attribute states.



8
9
10
# File 'lib/stator/machine.rb', line 8

def states
  @states
end

#transition_namesObject (readonly)

Returns the value of attribute transition_names.



6
7
8
# File 'lib/stator/machine.rb', line 6

def transition_names
  @transition_names
end

#transitionsObject (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

#evaluateObject



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

#klassObject



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, options = {}, &block)
  a = ::Stator::Alias.new(self, name, options)
  a.instance_eval(&block) if block_given?
  @aliases << a
  a
end

#tracking_enabled?Boolean

Returns:

  • (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