Sha256: 8d0b02554e1587672612b3993fe8bd8260b5e0bbdb03723b4f0bff0fcbc5fa49

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require "active_model"

# ApiClient::Errors provide extra functionality to ActiveModel::Errors.
class ApiClient::Errors < ActiveModel::Errors
  # Add serveral errors from a hash to the object.
  #
  # @return [ApiClient::Errors] The Error object.
  def add_errors(errors = {})
    errors.each do |key, value|
      self.set(key, value)
    end
  end

  # Returns a unique message for each array of error messages in a hash.
  #
  #   class Person
  #     validates_presence_of :name, :address, :email
  #     validates_length_of :name, in: 5..30
  #   end
  #
  #   person = Person.create(address: '123 First St.')
  #   person.errors.unique_messages
  #   # => { :name => "is too short (minimum is 5 characters) and can't be blank", :address => nil, :email => "can't be blank" }
  def unique_messages
    errors = {}
    map { |attribute, messages| errors[attribute] = unique_message(attribute) }
    errors
  end

  # Returns a unique message for a given attribute.
  #
  #   class Person
  #     validates_presence_of :name, :address, :email
  #     validates_length_of :name, in: 5..30
  #   end
  #
  #   person = Person.create(address: '123 First St.')
  #   person.errors.unique_message(:name) # => "is too short (minimum is 5 characters) and can't be blank"
  #   person.errors.unique_message(:address) # => nil
  def unique_message(attribute)
    return '' if messages[attribute].blank?
    messages[attribute].to_sentence
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api-client-1.5.3 lib/api-client/errors.rb