Class: Errorio::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/errorio/error.rb

Overview

Error object for errors collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, attribute, type, options) ⇒ Error

Returns a new instance of Error.



4
5
6
7
8
9
# File 'lib/errorio/error.rb', line 4

def initialize(base, attribute, type, options)
  @base = base
  @attribute = attribute
  @type = type
  @options = options.symbolize_keys
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



12
13
14
# File 'lib/errorio/error.rb', line 12

def attribute
  @attribute
end

#baseObject (readonly)

Returns the value of attribute base.



11
12
13
# File 'lib/errorio/error.rb', line 11

def base
  @base
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/errorio/error.rb', line 14

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/errorio/error.rb', line 13

def type
  @type
end

Instance Method Details

#full_message(attribute, message) ⇒ Object

Returns a full message for a given attribute. person.errors.full_message(:name, ‘is invalid’) # => “Name is invalid”



22
23
24
25
26
27
28
# File 'lib/errorio/error.rb', line 22

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

#messageObject



16
17
18
# File 'lib/errorio/error.rb', line 16

def message
  options[:message]
end