Class: AASM::Core::Transition

Inherits:
Object
  • Object
show all
Includes:
DslHelper
Defined in:
lib/aasm/core/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DslHelper

#add_options_from_dsl

Constructor Details

#initialize(event, opts, &block) ⇒ Transition

Returns a new instance of Transition.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aasm/core/transition.rb', line 10

def initialize(event, opts, &block)
  add_options_from_dsl(opts, [:on_transition, :guard, :after, :success], &block) if block

  @event = event
  @from = opts[:from]
  @to = opts[:to]
  @guards = Array(opts[:guards]) + Array(opts[:guard]) + Array(opts[:if])
  @unless = Array(opts[:unless]) #TODO: This could use a better name
  @failures = []

  if opts[:on_transition]
    warn '[DEPRECATION] :on_transition is deprecated, use :after instead'
    opts[:after] = Array(opts[:after]) + Array(opts[:on_transition])
  end
  @after = Array(opts[:after])
  @after = @after[0] if @after.size == 1

  @success = Array(opts[:success])
  @success = @success[0] if @success.size == 1

  @opts = opts
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event


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

def event
  @event
end

#failuresObject (readonly)

Returns the value of attribute failures


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

def failures
  @failures
end

#fromObject (readonly)

Returns the value of attribute from


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

def from
  @from
end

#optsObject (readonly) Also known as: options

Returns the value of attribute opts


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

def opts
  @opts
end

#toObject (readonly)

Returns the value of attribute to


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

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object


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

def ==(obj)
  @from == obj.from && @to == obj.to
end

#allowed?(obj, *args) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
45
# File 'lib/aasm/core/transition.rb', line 42

def allowed?(obj, *args)
  invoke_callbacks_compatible_with_guard(@guards, obj, args, :guard => true) &&
  invoke_callbacks_compatible_with_guard(@unless, obj, args, :unless => true)
end

#execute(obj, *args) ⇒ Object


47
48
49
50
# File 'lib/aasm/core/transition.rb', line 47

def execute(obj, *args)
  invoke_callbacks_compatible_with_guard(event.state_machine.global_callbacks[:after_all_transitions], obj, args)
  invoke_callbacks_compatible_with_guard(@after, obj, args)
end

#from?(value) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/aasm/core/transition.rb', line 56

def from?(value)
  @from == value
end

#initialize_copy(orig) ⇒ Object

called internally by Ruby 1.9 after clone()


34
35
36
37
38
39
40
# File 'lib/aasm/core/transition.rb', line 34

def initialize_copy(orig)
  super
  @guards = @guards.dup
  @unless = @unless.dup
  @opts   = {}
  orig.opts.each_pair { |name, setting| @opts[name] = setting.is_a?(Hash) || setting.is_a?(Array) ? setting.dup : setting }
end

#invoke_success_callbacks(obj, *args) ⇒ Object


60
61
62
# File 'lib/aasm/core/transition.rb', line 60

def invoke_success_callbacks(obj, *args)
  _fire_callbacks(@success, obj, args)
end