Class: Stator::Transition

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

Constant Summary collapse

ANY =
'__any__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, name, namespace = nil) ⇒ Transition

Returns a new instance of Transition.



9
10
11
12
13
14
15
16
17
# File 'lib/stator/transition.rb', line 9

def initialize(class_name, name, namespace = nil)
  @class_name = class_name
  @name       = name
  @namespace  = namespace
  @full_name  = [@namespace, @name].compact.join('_') if @name
  @froms      = []
  @to         = nil
  @callbacks  = {}
end

Instance Attribute Details

#full_nameObject (readonly)

Returns the value of attribute full_name.



7
8
9
# File 'lib/stator/transition.rb', line 7

def full_name
  @full_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#anyObject



48
49
50
# File 'lib/stator/transition.rb', line 48

def any
  ANY
end

#can?(current_state) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/stator/transition.rb', line 35

def can?(current_state)
  @froms.include?(current_state) || @froms.include?(ANY) || current_state == ANY
end

#conditional(options = {}, &block) ⇒ Object



44
45
46
# File 'lib/stator/transition.rb', line 44

def conditional(options = {}, &block)
  klass.instance_exec(conditional_block(options), &block)
end

#evaluateObject



52
53
54
# File 'lib/stator/transition.rb', line 52

def evaluate
  generate_methods unless @full_name.blank?
end

#from(*froms) ⇒ Object



19
20
21
# File 'lib/stator/transition.rb', line 19

def from(*froms)
  @froms |= froms.map{|f| f.try(:to_s) } # nils are ok
end

#from_statesObject



31
32
33
# File 'lib/stator/transition.rb', line 31

def from_states
  @froms
end

#to(to) ⇒ Object



23
24
25
# File 'lib/stator/transition.rb', line 23

def to(to)
  @to = to.to_s
end

#to_stateObject



27
28
29
# File 'lib/stator/transition.rb', line 27

def to_state
  @to
end

#valid?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/stator/transition.rb', line 39

def valid?(from, to)
  can?(from) &&
  (@to == to || @to == ANY || to == ANY)
end