提交 963fef7b 编写于 作者: G Genadi Samokovarov

Simplify the ActionableError.{dispatch,action} boundries

上级 a3110fe2
......@@ -15,8 +15,8 @@ module ActionableError
NonActionable = Class.new(StandardError)
NoActions = Hash.new do |_, label| # :nodoc:
raise NonActionable, "Cannot find action \"#{label}\" for #{self}"
NoActions = Hash.new do |_, name| # :nodoc:
raise NonActionable, "Cannot find action \"#{name}\""
end
included do
......@@ -25,19 +25,15 @@ module ActionableError
def self.actions(error) # :nodoc:
case error
when String
actions(error.constantize)
when ActionableError, -> it { Class === it && it < ActionableError }
error._actions
when Exception
NoActions
else
raise NonActionable, "#{error} is non-actionable"
NoActions
end
end
def self.dispatch(error, label) # :nodoc:
actions(error)[label].call
def self.dispatch(error, name) # :nodoc:
actions(error.is_a?(String) ? error.constantize : error)[name].call
end
module ClassMethods
......@@ -50,8 +46,8 @@ module ClassMethods
# ActiveRecord::Tasks::DatabaseTasks.migrate
# end
# end
def action(label, &block)
_actions[label] = block
def action(name, &block)
_actions[name] = block
end
end
end
......
......@@ -21,44 +21,33 @@ class DispatchableError < StandardError
end
end
test "lists all action of an actionable error" do
test "returns all action of an actionable error" do
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError).keys
assert_equal ["Flip 1", "Flip 2"], ActiveSupport::ActionableError.actions(DispatchableError.new).keys
end
test "raises an error when trying to get actions from non-actionable error classes" do
assert_raises ActiveSupport::ActionableError::NonActionable do
ActiveSupport::ActionableError.actions(NonActionableError)
end
assert_raises ActiveSupport::ActionableError::NonActionable do
ActiveSupport::ActionableError.actions(NonActionableError.name)
end
end
test "returns no actions from non-actionable exception instances" do
test "returns no actions for non-actionable errors" do
assert ActiveSupport::ActionableError.actions(Exception).empty?
assert ActiveSupport::ActionableError.actions(Exception.new).empty?
end
test "dispatches actions from class and a label" do
test "dispatches actions from error and name" do
assert_changes "DispatchableError.flip1", from: false, to: true do
ActiveSupport::ActionableError.dispatch DispatchableError, "Flip 1"
end
end
test "dispatches actions from class name and a label" do
test "dispatches actions from error class as string and name" do
assert_changes "DispatchableError.flip2", from: false, to: true do
ActiveSupport::ActionableError.dispatch DispatchableError.name, "Flip 2"
end
end
test "cannot dispatch errors that do not include ActiveSupport::ActionableError" do
test "cannot dispatch missing actions" do
err = assert_raises ActiveSupport::ActionableError::NonActionable do
ActiveSupport::ActionableError.dispatch NonActionableError, "action"
end
assert_equal <<~EXPECTED.chop, err.to_s
ActionableErrorTest::NonActionableError is non-actionable
EXPECTED
assert_equal 'Cannot find action "action"', err.to_s
end
end
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册