Class: Statum::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/statum/event.rb

Overview

Class for storing event info

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, options = {}) ⇒ Event

Creates an event class

Parameters:

  • from (Symbol, Array<Symbol>)

    From state name

  • to (Symbol)

    To state name

  • options (Hash) (defaults to: {})

    Options for event


16
17
18
19
20
21
# File 'lib/statum/event.rb', line 16

def initialize(from, to, options = {})
  @from   = from.is_a?(Array) ? from.map(&:to_sym) : from.to_sym
  @to     = to.to_sym
  @before = Statum::Hook.new(options.fetch(:before, nil))
  @after = Statum::Hook.new(options.fetch(:after, nil))
end

Instance Attribute Details

#afterStatum::Hook

After hook object

Returns:


8
9
10
# File 'lib/statum/event.rb', line 8

def after
  @after
end

#beforeStatum::Hook

Before hook object

Returns:


8
9
10
# File 'lib/statum/event.rb', line 8

def before
  @before
end

#fromSymbol, Array

From state name (or names)

Returns:

  • (Symbol, Array)

    the current value of from


8
9
10
# File 'lib/statum/event.rb', line 8

def from
  @from
end

#toSymbol

To state name

Returns:

  • (Symbol)

    the current value of to


8
9
10
# File 'lib/statum/event.rb', line 8

def to
  @to
end

Instance Method Details

#can_fire?(current_state) ⇒ Boolean

Returns true if event can be fired from current state

Parameters:

  • current_state (Symbol)

    Current state

Returns:

  • (Boolean)

28
29
30
31
32
33
34
35
36
# File 'lib/statum/event.rb', line 28

def can_fire?(current_state)
  if from.is_a?(Array)
    from.include?(current_state.to_sym)
  elsif from == Statum::ANY_STATE_NAME
    true
  else
    from == current_state.to_sym
  end
end