class Errorio::Errors
Collection of error objects
Attributes
errors[R]
objects[R]
Public Class Methods
new(base = nil)
click to toggle source
# File lib/errorio/errors.rb, line 10 def initialize(base = nil) @base = base @errors = [] end
Public Instance Methods
add(attribute, type = :invalid, options = {})
click to toggle source
Adds a new error to errors collection
@param [Symbol] attribute @param [Symbol] type @param [Hash] options
# File lib/errorio/errors.rb, line 20 def add(attribute, type = :invalid, options = {}) error = Error.new(@base, attribute, type, options) @errors.append(error) error end
attribute_names()
click to toggle source
Returns all error attribute names
person.errors.messages # => {:name=>["cannot be nil", "must be specified"]} person.errors.attribute_names # => [:name]
# File lib/errorio/errors.rb, line 46 def attribute_names @errors.map(&:attribute).uniq.freeze end
Also aliased as: keys
copy(other)
click to toggle source
Copy errors from another errors object
@param [Errorio::Errors,ActiveModel::Errors] other
# File lib/errorio/errors.rb, line 31 def copy(other) other.each do |err| options = err.options # ActiveModel::Error object has own way to generate message attribute, options[:message] = err.message if err.is_a?(ActiveModel::Error) add err.attribute, err.type, options end end
each(&block)
click to toggle source
# File lib/errorio/errors.rb, line 61 def each(&block) @errors.each(&block) end
full_message(attribute, message)
click to toggle source
Returns a full message for a given attribute. person.errors.full_message(:name, 'is invalid') # => “Name is invalid”
# File lib/errorio/errors.rb, line 53 def full_message(attribute, message) attr_name = attribute.to_s.tr('.', '_').humanize return "#{attr_name} #{message}" if @base.nil? attr_name = @base.class.human_attribute_name(attribute, default: attr_name) I18n.t(:"errors.format", default: '%{attribute} %{message}', attribute: attr_name, message: message) end