Class: Transitions::StateTransition

Inherits:
Object
  • Object
show all
Defined in:
lib/transitions/state_transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ StateTransition

Returns a new instance of StateTransition.


27
28
29
30
# File 'lib/transitions/state_transition.rb', line 27

def initialize(opts)
  @from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
  @options = opts
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from


25
26
27
# File 'lib/transitions/state_transition.rb', line 25

def from
  @from
end

#optionsObject (readonly)

Returns the value of attribute options


25
26
27
# File 'lib/transitions/state_transition.rb', line 25

def options
  @options
end

#toObject (readonly)

Returns the value of attribute to


25
26
27
# File 'lib/transitions/state_transition.rb', line 25

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object


61
62
63
# File 'lib/transitions/state_transition.rb', line 61

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

#execute(obj, *args) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/transitions/state_transition.rb', line 43

def execute(obj, *args)
  case @on_transition
  when Symbol, String
    obj.send(@on_transition, *args)
  when Proc
    @on_transition.call(obj, *args)
  when Array
    @on_transition.each do |callback|
      # Yes, we're passing always the same parameters for each callback in here.
      # We should probably drop args altogether in case we get an array.
      obj.send(callback, *args)
    end
  else
    # TODO We probably should check for this in the constructor and not that late.
    raise ArgumentError, "You can only pass a Symbol, a String, a Proc or an Array to 'on_transition' - got #{@on_transition.class}." unless @on_transition.nil?
  end
end

#from?(value) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/transitions/state_transition.rb', line 65

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

#perform(obj) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'lib/transitions/state_transition.rb', line 32

def perform(obj)
  case @guard
  when Symbol, String
    obj.send(@guard)
  when Proc
    @guard.call(obj)
  else
    true
  end
end