lib/rcap/validation.rb in rcap-2.7.0 vs lib/rcap/validation.rb in rcap-2.7.3

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + # This file is ripped directly from the no-longer-maintained assistance gem. Other code in that gem # was causing issues, so I'm putting the needed code directly in rcap as a workaround. # See https://github.com/farrel/RCAP/issues/4 @@ -60,22 +62,22 @@ # Returns the errors for the given attribute. def on(att) @errors[att] end - alias_method :[], :on + alias [] on # Adds an error for the given attribute. def add(att, msg) @errors[att] << msg end # Returns an array of fully-formatted error messages. def full_messages - @errors.reduce([]) do |m, kv| att, errors = *kv - errors.each { |e| m << "#{att} #{e}" } - m + @errors.each_with_object([]) do |kv, m| + att, errors = *kv + errors.each { |e| m << "#{att} #{e}" } end end end # The Generator class is used to generate validation definitions using @@ -167,11 +169,11 @@ end # Validates confirmation of an attribute. def validates_confirmation_of(*atts) opts = { - message: 'is not confirmed', + message: 'is not confirmed' }.merge!(atts.extract_options!) validates_each(*atts) do |o, a, v| next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank]) c = o.send(:"#{a}_confirmation") @@ -180,15 +182,15 @@ end # Validates the format of an attribute. def validates_format_of(*atts) opts = { - message: 'is invalid', + message: 'is invalid' }.merge!(atts.extract_options!) unless opts[:with].is_a?(Regexp) - fail ArgumentError, 'A regular expression must be supplied as the :with option of the options hash' + raise ArgumentError, 'A regular expression must be supplied as the :with option of the options hash' end validates_each(*atts) do |o, a, v| next if (v.nil? && opts[:allow_nil]) || (v.blank? && opts[:allow_blank]) o.errors[a] << opts[:message] unless v.to_s =~ opts[:with] @@ -224,11 +226,11 @@ INTEGER_RE = /\A[+-]?\d+\Z/ # Validates whether an attribute is a number. def validates_numericality_of(*atts) opts = { - message: 'is not a number', + message: 'is not a number' }.merge!(atts.extract_options!) re = opts[:only_integer] ? INTEGER_RE : NUMBER_RE validates_each(*atts) do |o, a, v| @@ -238,10 +240,10 @@ end # Validates the presence of an attribute. def validates_presence_of(*atts) opts = { - message: 'is not present', + message: 'is not present' }.merge!(atts.extract_options!) validates_each(*atts) do |o, a, v| o.errors[a] << opts[:message] unless v && !v.blank? end