Class: FiniteMachine::HookEvent

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/finite_machine/hook_event.rb

Overview

A class responsible for event notification

Direct Known Subclasses

Anyaction, Anystate

Defined Under Namespace

Classes: After, Anyaction, Anystate, Before, Enter, Exit, Transition

Constant Summary collapse

EVENTS =
Anystate, Enter, Transition, Exit, Anyaction, Before, After
MESSAGE =
:emit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, event_name, from) ⇒ self

Instantiate a new HookEvent object

Examples:

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

Parameters:

  • name (Symbol)

    The action or state name

  • event_name (Symbol)

    The event name associated with this hook event.



104
105
106
107
108
109
110
# 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

Instance Attribute Details

#event_nameObject (readonly)

The event name triggering this hook event



88
89
90
# File 'lib/finite_machine/hook_event.rb', line 88

def event_name
  @event_name
end

#fromObject (readonly)

The from state this hook has been fired



85
86
87
# File 'lib/finite_machine/hook_event.rb', line 85

def from
  @from
end

#nameObject (readonly)

HookEvent state or action name



79
80
81
# File 'lib/finite_machine/hook_event.rb', line 79

def name
  @name
end

#typeObject (readonly)

HookEvent type



82
83
84
# File 'lib/finite_machine/hook_event.rb', line 82

def type
  @type
end

Class Method Details

.any_state_or_event(event_type) ⇒ Symbol

Choose any state or event name based on even type

Parameters:

Returns:

  • (Symbol)

    out of :any or :any_event



52
53
54
# 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) ⇒ self

Build event hook

Parameters:

  • :state (Symbol)

    The state or action name.

  • :event_name (Symbol)

    The event name associted with this hook.

Returns:

  • (self)


67
68
69
70
# 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_nameString

Extract event name

Returns:

  • (String)

    the event name



31
32
33
# File 'lib/finite_machine/hook_event.rb', line 31

def self.event_name
  name.split("::").last.downcase.to_sym
end

.to_sString

String representation

Returns:

  • (String)

    the event name



40
41
42
# File 'lib/finite_machine/hook_event.rb', line 40

def self.to_s
  event_name.to_s
end

Instance Method Details

#<=>(other) ⇒ -1 0 1

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

Returns:

  • (-1 0 1)


134
135
136
137
# 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) ⇒ nil

Notify subscriber about this event

Parameters:

  • subscriber (Observer)

    the object subscribed to be notified about this event

  • data (Array)

    the data associated with the triggered event

Returns:

  • (nil)


123
124
125
126
127
# 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