lib/metaractor/errors.rb in metaractor-3.3.0 vs lib/metaractor/errors.rb in metaractor-3.3.2

- old
+ new

@@ -1,7 +1,7 @@ -require 'sycamore' -require 'forwardable' +require "sycamore" +require "forwardable" module Metaractor class Errors extend Forwardable class Error @@ -19,19 +19,19 @@ if object.class.respond_to?(:i18n_parent_names) && !object.class.i18n_parent_names.empty? names = object.class.i18n_parent_names until names.empty? - defaults << ['errors', names.join('.'), 'parameters', path_elements.join('.'), @value.to_s].reject do |item| - item.nil? || item == '' - end.join('.').to_sym + defaults << ["errors", names.join("."), "parameters", path_elements.join("."), @value.to_s].reject do |item| + item.nil? || item == "" + end.join(".").to_sym names.pop end end unless path_elements.empty? - defaults << :"errors.parameters.#{path_elements.join('.')}.#{@value}" + defaults << :"errors.parameters.#{path_elements.join(".")}.#{@value}" end defaults << :"errors.parameters.#{@value}" key = defaults.shift I18n.translate( @@ -39,22 +39,22 @@ default: defaults, error_key: @value, parameter: path_elements.last ) else - "#{path_elements.join('.')} #{@value}".lstrip + "#{path_elements.join(".")} #{@value}".lstrip end end def ==(other) - if other.is_a?(self.class) - @value == other.value + @value == if other.is_a?(self.class) + other.value else - @value == other + other end end - alias eql? == + alias_method :eql?, :== def hash @value.hash end @@ -71,14 +71,14 @@ def add(error: {}, errors: {}, object: nil) trees = [] [error, errors].each do |h| tree = nil - if h.is_a? Metaractor::Errors - tree = Sycamore::Tree.from(h.instance_variable_get(:@tree)) + tree = if h.is_a? Metaractor::Errors + Sycamore::Tree.from(h.instance_variable_get(:@tree)) else - tree = Sycamore::Tree.from(normalize_error_hash(h)) + Sycamore::Tree.from(normalize_error_hash(h)) end unless tree.empty? if tree.nodes.any? { |node| tree.strict_leaf?(node) } raise ArgumentError, "Invalid hash!" @@ -110,11 +110,11 @@ messages << message_from_path(path) end messages end - alias to_a full_messages + alias_method :to_a, :full_messages def full_messages_for(*path) child_tree = @tree.fetch_path(path) if child_tree.strict_leaves? @@ -131,23 +131,21 @@ unwrapped_enum(result.nodes) else unwrapped_tree(result).to_h end end - alias [] dig + alias_method :[], :dig def include?(*elements) if elements.size == 1 && elements.first.is_a?(Hash) unwrapped_tree.include?(*elements) + elsif elements.all? { |e| e.is_a? String } + full_messages.include?(*elements) else - if elements.all? {|e| e.is_a? String } - full_messages.include?(*elements) - else - elements.all? do |element| - @tree.include_path?(element) - end + elements.all? do |element| + @tree.include_path?(element) end end end def slice(*paths) @@ -167,15 +165,15 @@ unwrapped_tree.to_h else @tree.to_h end end - alias to_hash to_h + alias_method :to_hash, :to_h def inspect str = "<##{self.class.name}: " - if !self.empty? + if !empty? str << "Errors:\n" str << Metaractor.format_hash(to_h(unwrap: false)) str << "\n" end