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.



5
6
7
8
# File 'lib/transitions/state_transition.rb', line 5

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.



3
4
5
# File 'lib/transitions/state_transition.rb', line 3

def from
  @from
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/transitions/state_transition.rb', line 3

def options
  @options
end

#toObject (readonly)

Returns the value of attribute to.



3
4
5
# File 'lib/transitions/state_transition.rb', line 3

def to
  @to
end

Instance Method Details

#==(obj) ⇒ Object



32
33
34
# File 'lib/transitions/state_transition.rb', line 32

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

#executable?(obj, *args) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/transitions/state_transition.rb', line 10

def executable?(obj, *args)
  [@guard].flatten.all? { |g| perform_guard(obj, g, *args) }
end

#execute(obj, *args) ⇒ Object



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

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.
    fail 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)


36
37
38
# File 'lib/transitions/state_transition.rb', line 36

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