lib/finite_machine/safety.rb in finite_machine-0.11.3 vs lib/finite_machine/safety.rb in finite_machine-0.12.0
- old
+ new
@@ -1,9 +1,11 @@
-# encoding: utf-8
+# frozen_string_literal: true
+require_relative 'hook_event'
+
module FiniteMachine
- # Module for responsible for safety checks against known methods
+ # Module responsible for safety checks against known methods
module Safety
EVENT_CONFLICT_MESSAGE = \
"You tried to define an event named \"%{name}\", however this would " \
"generate \"%{type}\" method \"%{method}\", which is already defined " \
"by %{source}"
@@ -31,11 +33,11 @@
# @return [nil]
#
# @api public
def detect_event_conflict!(event_name, method_name = event_name)
if method_already_implemented?(method_name)
- fail FiniteMachine::AlreadyDefinedError, EVENT_CONFLICT_MESSAGE % {
+ raise FiniteMachine::AlreadyDefinedError, EVENT_CONFLICT_MESSAGE % {
name: event_name,
type: :instance,
method: method_name,
source: 'FiniteMachine'
}
@@ -53,27 +55,27 @@
#
# @api public
def ensure_valid_callback_name!(event_type, name)
message = if wrong_event_name?(name, event_type)
EVENT_CALLBACK_CONFLICT_MESSAGE % {
- type: "on_#{event_type.to_s}",
+ type: "on_#{event_type}",
name: name
}
elsif wrong_state_name?(name, event_type)
STATE_CALLBACK_CONFLICT_MESSAGE % {
- type: "on_#{event_type.to_s}",
+ 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 && fail_invalid_callback_error(message)
+ message && raise_invalid_callback_error(message)
end
private
# Check if event name exists
@@ -85,11 +87,11 @@
# @return [Boolean]
#
# @api private
def wrong_event_name?(name, event_type)
machine.states.include?(name) &&
- !machine.event_names.include?(name) &&
+ !machine.events.include?(name) &&
event_type < HookEvent::Anyaction
end
# Check if state name exists
#
@@ -99,17 +101,17 @@
#
# @return [Boolean]
#
# @api private
def wrong_state_name?(name, event_type)
- machine.event_names.include?(name) &&
+ machine.events.include?(name) &&
!machine.states.include?(name) &&
event_type < HookEvent::Anystate
end
- def fail_invalid_callback_error(message)
+ def raise_invalid_callback_error(message)
exception = InvalidCallbackNameError
- machine.catch_error(exception) || fail(exception, message)
+ machine.catch_error(exception) || raise(exception, message)
end
# Check if method is already implemented inside StateMachine
#
# @param [String] name