Class: ErrorBuilder::Error

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Error

Returns a new instance of Error.



7
8
9
10
11
# File 'lib/error_builder/error.rb', line 7

def initialize(key)
  @key     = key
  @format  = ErrorBuilder.configuration.message_format
  @message = initialize_message(format)
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

Instance Method Details

#add_message(message) ⇒ Object



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

def add_message(message)
  case format
  when :array
    @message << message
  when :string
    @message = message
  else
    raise ArgumentError, "Unsupported message format: #{format}"
  end
end

#keysObject



31
32
33
34
35
# File 'lib/error_builder/error.rb', line 31

def keys
  return [key] unless key.to_s.include?(".")

  deflat_key
end

#to_hObject



24
25
26
27
28
29
# File 'lib/error_builder/error.rb', line 24

def to_h
  {
    key: @key,
    message: @message
  }
end