Module: FiniteMachine::Safety

Included in:
DSL, Observer
Defined in:
lib/finite_machine/safety.rb

Overview

Module responsible for safety checks against known methods

Constant Summary collapse

EVENT_CONFLICT_MESSAGE =
"You tried to define an event named \"%{name}\", however this would " \
"generate \"%{type}\" method \"%{method}\", which is already defined " \
"by %{source}"
STATE_CALLBACK_CONFLICT_MESSAGE =
"\"%{type}\" callback is a state listener and cannot be used " \
"with \"%{name}\" event name. Please use on_before or on_after instead."
EVENT_CALLBACK_CONFLICT_MESSAGE =
"\"%{type}\" callback is an event listener and cannot be used " \
"with \"%{name}\" state name. Please use on_enter, on_transition or " \
"on_exit instead."
CALLBACK_INVALID_MESSAGE =
"\"%{name}\" is not a valid callback name. " \
"Valid callback names are \"%{callbacks}"

Instance Method Summary collapse

Instance Method Details

#detect_event_conflict!(event_name, method_name = event_name) ⇒ nil

Raise error when the method is already defined

Examples:

detect_event_conflict!(:test, "test=")

Returns:

  • (nil)

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/finite_machine/safety.rb', line 36

def detect_event_conflict!(event_name, method_name = event_name)
  if method_already_implemented?(method_name)
    raise FiniteMachine::AlreadyDefinedError, EVENT_CONFLICT_MESSAGE % {
      name: event_name,
      type: :instance,
      method: method_name,
      source: "FiniteMachine"
    }
  end
end

#ensure_valid_callback_name!(event_type, name) ⇒ nil

Raise error when the callback name is not valid

Examples:

ensure_valid_callback_name!(HookEvent::Enter, ":state_name")

Returns:

  • (nil)

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/finite_machine/safety.rb', line 57

def ensure_valid_callback_name!(event_type, name)
  message = if wrong_event_name?(name, event_type)
    EVENT_CALLBACK_CONFLICT_MESSAGE % {
      type: "on_#{event_type}",
      name: name
    }
  elsif wrong_state_name?(name, event_type)
    STATE_CALLBACK_CONFLICT_MESSAGE % {
      type: "on_#{event_type}",
      name: name
    }
  elsif !callback_names.include?(name)
    CALLBACK_INVALID_MESSAGE % {
      name: name,
      callbacks: callback_names.to_a.inspect
    }
  else
    nil
  end
  message && raise_invalid_callback_error(message)
end