lib/finite_machine/catchable.rb in finite_machine-0.13.0 vs lib/finite_machine/catchable.rb in finite_machine-0.14.0
- old
+ new
@@ -31,11 +31,11 @@
unless options.key?(:with)
if block_given?
options[:with] = block
else
- raise ArgumentError, 'Need to provide error handler.'
+ raise ArgumentError, "Need to provide error handler."
end
end
evaluate_exceptions(exceptions, options)
end
@@ -69,11 +69,11 @@
#
# @param [String] class_name
#
# @api private
def extract_const(class_name)
- class_name.split('::').reduce(FiniteMachine) do |constant, part|
+ class_name.split("::").reduce(FiniteMachine) do |constant, part|
constant.const_get(part)
end
end
# Executes given handler
@@ -83,13 +83,13 @@
case handler
when Symbol
target.method(handler)
when Proc
if handler.arity.zero?
- proc { instance_exec(&handler) }
+ -> { instance_exec(&handler) }
else
- proc { |_exception| instance_exec(_exception, &handler) }
+ ->(exception) { instance_exec(exception, &handler) }
end
end
end
# Check if exception inherits from Exception class and add to error handlers
@@ -99,18 +99,26 @@
# @param [Hash] options
#
# @api private
def evaluate_exceptions(exceptions, options)
exceptions.each do |exception|
- key = if exception.is_a?(Class) && exception <= Exception
- exception.name
- elsif exception.is_a?(String)
- exception
- else
- raise ArgumentError, "#{exception} isn't an Exception"
- end
-
+ key = extract_exception_name(exception)
error_handlers << [key, options[:with]]
+ end
+ end
+
+ # Extract exception name
+ #
+ # @param [Class,Exception,String] exception
+ #
+ # @api private
+ def extract_exception_name(exception)
+ if exception.is_a?(Class) && exception <= Exception
+ exception.name
+ elsif exception.is_a?(String)
+ exception
+ else
+ raise ArgumentError, "#{exception} isn't an Exception"
end
end
end # Catchable
end # FiniteMachine