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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stator/machine.rb', line 14

def initialize(klass, options = {})
  @class_name    = klass.name
  @field         = options[:field] || :state
  @namespace     = options[:namespace] || nil

  @initial_state = options[:initial]

  @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

#skip_transition_trackingObject (readonly)

Returns the value of attribute skip_transition_tracking.



11
12
13
# File 'lib/stator/machine.rb', line 11

def skip_transition_tracking
  @skip_transition_tracking
end

#skip_validationsObject (readonly)

Returns the value of attribute skip_validations.



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

def skip_validations
  @skip_validations
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
# File 'lib/stator/machine.rb', line 69

def conditional(*states, &block)
  klass.instance_exec("#{states.map(&:to_s).inspect}.include?(self._stator(#{@namespace.inspect}).integration(self).state)", &block)
end

#evaluateObject



79
80
81
82
83
# File 'lib/stator/machine.rb', line 79

def evaluate
  @transitions.each(&:evaluate)
  @aliases.each(&:evaluate)
  generate_methods
end

#get_transition(name) ⇒ Object



36
37
38
# File 'lib/stator/machine.rb', line 36

def get_transition(name)
  @transitions.detect{|t| t.name.to_s == name.to_s}
end

#integration(record) ⇒ Object



32
33
34
# File 'lib/stator/machine.rb', line 32

def integration(record)
  ::Stator::Integration.new(self, record)
end

#klassObject



85
86
87
# File 'lib/stator/machine.rb', line 85

def klass
  @class_name.constantize
end

#matching_transition(from, to) ⇒ Object



73
74
75
76
77
# File 'lib/stator/machine.rb', line 73

def matching_transition(from, to)
  @transitions.detect do |transition|
    transition.valid?(from, to)
  end
end

#state(name, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/stator/machine.rb', line 61

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



54
55
56
57
58
59
# File 'lib/stator/machine.rb', line 54

def state_alias(name, options = {}, &block)
  a = ::Stator::Alias.new(self, name, options)
  a.instance_eval(&block) if block_given?
  @aliases << a
  a
end

#transition(name, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stator/machine.rb', line 40

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

#without_transition_trackingObject



97
98
99
100
101
102
103
# File 'lib/stator/machine.rb', line 97

def without_transition_tracking
  was = @skip_transition_tracking
  @skip_transition_tracking = true
  yield
ensure
  @skip_transition_tracking = was
end

#without_validationObject



89
90
91
92
93
94
95
# File 'lib/stator/machine.rb', line 89

def without_validation
  was = @skip_validations
  @skip_validations = true
  yield
ensure
  @skip_validations = was
end