Sha256: a9b6c76f7e416b8f3add83a433d281497efb9a180b6fa574bd8c96ff55afbe23

Contents?: true

Size: 1.04 KB

Versions: 14

Compression:

Stored size: 1.04 KB

Contents

module Lev

  class Error

    attr_accessor :code
    attr_accessor :data
    attr_accessor :kind
    attr_accessor :message

    # The inputs related to this error
    attr_accessor :offending_inputs

    def initialize(args={})
      raise ArgumentError, "must supply a :code" if args[:code].blank?

      self.code = args[:code]
      self.data = args[:data]
      self.kind = args[:kind]
      self.message = args[:message]
      self.offending_inputs = args[:offending_inputs]
    end

    def translate
      ErrorTranslator.translate(self)
    end

    def to_s
      inspect
    end

    def full_message
      attribute = data[:attribute] if data.present?
      return message.humanize if attribute.nil?

      attr_name = attribute.to_s.gsub('.', '_').humanize

      model = data[:model]
      attr_name = model.class.human_attribute_name(attribute, default: attr_name) if model.present?

      I18n.t(:"errors.format", {
        default:   "%{attribute} %{message}",
        attribute: attr_name,
        message:   message
      })
    end

  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lev-12.1.0 lib/lev/error.rb
lev-12.0.0 lib/lev/error.rb
lev-11.0.0 lib/lev/error.rb
lev-10.1.0 lib/lev/error.rb
lev-10.0.0 lib/lev/error.rb
lev-9.0.4 lib/lev/error.rb
lev-9.0.3 lib/lev/error.rb
lev-9.0.2 lib/lev/error.rb
lev-9.0.1 lib/lev/error.rb
lev-9.0.0 lib/lev/error.rb
lev-8.1.0 lib/lev/error.rb
lev-8.0.0 lib/lev/error.rb
lev-7.1.0 lib/lev/error.rb
lev-7.0.3 lib/lev/error.rb