From feaaa7576a0774471e36dc59730a886f623712e6 Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Fri, 19 Apr 2019 13:49:56 +0900 Subject: Refactor after the most recent code review --- activesupport/lib/active_support/actionable_error.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'activesupport/lib/active_support/actionable_error.rb') diff --git a/activesupport/lib/active_support/actionable_error.rb b/activesupport/lib/active_support/actionable_error.rb index 8adf40cc3d..7db14cd178 100644 --- a/activesupport/lib/active_support/actionable_error.rb +++ b/activesupport/lib/active_support/actionable_error.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "active_support/concern" - module ActiveSupport # Actionable errors let's you define actions to resolve an error. # @@ -11,14 +9,10 @@ module ActiveSupport module ActionableError extend Concern - NonActionable = Class.new(StandardError) - - NoActions = Hash.new do |_, name| # :nodoc: - raise NonActionable, "Cannot find action \"#{name}\"" - end + class NonActionable < StandardError; end included do - class_attribute :_actions, default: NoActions.dup + class_attribute :_actions, default: {} end def self.actions(error) # :nodoc: @@ -26,12 +20,14 @@ module ActiveSupport when ActionableError, -> it { Class === it && it < ActionableError } error._actions else - NoActions + {} end end def self.dispatch(error, name) # :nodoc: - actions(error.is_a?(String) ? error.constantize : error)[name].call + actions(error).fetch(name).call + rescue KeyError + raise NonActionable, "Cannot find action \"#{name}\"" end module ClassMethods -- cgit v1.2.3