Class: Lev::Error

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Error

Returns a new instance of Error.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/lev/error.rb', line 13

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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



5
6
7
# File 'lib/lev/error.rb', line 5

def code
  @code
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/lev/error.rb', line 6

def data
  @data
end

#kindObject

Returns the value of attribute kind.



7
8
9
# File 'lib/lev/error.rb', line 7

def kind
  @kind
end

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/lev/error.rb', line 8

def message
  @message
end

#offending_inputsObject

The inputs related to this error



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

def offending_inputs
  @offending_inputs
end

Instance Method Details

#full_messageObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lev/error.rb', line 31

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

#to_sObject



27
28
29
# File 'lib/lev/error.rb', line 27

def to_s
  inspect
end

#translateObject



23
24
25
# File 'lib/lev/error.rb', line 23

def translate
  ErrorTranslator.translate(self)
end