class FiniteMachine::HookEvent

A class responsible for event notification

Constants

EVENTS
MESSAGE

Attributes

event_name[R]

The event name triggering this hook event

from[R]

The from state this hook has been fired

name[R]

HookEvent state or action name

type[R]

HookEvent type

Public Class Methods

any_state_or_event(event_type) click to toggle source

Choose any state or event name based on even type

@param [HookEvent] event_type

@return [Symbol]

out of :any or :any_event

@api public

# File lib/finite_machine/hook_event.rb, line 52
def self.any_state_or_event(event_type)
  event_type < Anyaction ? ANY_EVENT : ANY_STATE
end
build(state, event_name, from) click to toggle source

Build event hook

@param [Symbol] :state

The state or action name.

@param [Symbol] :event_name

The event name associted with this hook.

@return [self]

@api public

# File lib/finite_machine/hook_event.rb, line 67
def self.build(state, event_name, from)
  state_or_action = self < Anystate ? state : event_name
  new(state_or_action, event_name, from)
end
event_name() click to toggle source

Extract event name

@return [String] the event name

@api public

# File lib/finite_machine/hook_event.rb, line 31
def self.event_name
  name.split("::").last.downcase.to_sym
end
new(name, event_name, from) click to toggle source

Instantiate a new HookEvent object

@param [Symbol] name

The action or state name

@param [Symbol] event_name

The event name associated with this hook event.

@example

HookEvent.new(:green, :move, :green)

@return [self]

@api public

# File lib/finite_machine/hook_event.rb, line 104
def initialize(name, event_name, from)
  @name       = name
  @type       = self.class
  @event_name = event_name
  @from       = from
  freeze
end
to_s() click to toggle source

String representation

@return [String] the event name

@api public

# File lib/finite_machine/hook_event.rb, line 40
def self.to_s
  event_name.to_s
end

Public Instance Methods

<=>(other) click to toggle source

Compare whether the instance is greater, less then or equal to other

@return [-1 0 1]

@api public

# File lib/finite_machine/hook_event.rb, line 134
def <=>(other)
  other.is_a?(type) &&
    [name, from, event_name] <=> [other.name, other.from, other.event_name]
end
notify(subscriber, *data) click to toggle source

Notify subscriber about this event

@param [Observer] subscriber

the object subscribed to be notified about this event

@param [Array] data

the data associated with the triggered event

@return [nil]

@api public

# File lib/finite_machine/hook_event.rb, line 123
def notify(subscriber, *data)
  return unless subscriber.respond_to?(MESSAGE)

  subscriber.public_send(MESSAGE, self, *data)
end