Class: RDStation::Error::Formatter

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

Instance Method Summary collapse

Constructor Details

#initialize(errors) ⇒ Formatter

Returns a new instance of Formatter.



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

def initialize(errors)
  @errors = errors
end

Instance Method Details

#error_format ⇒ Object



35
36
37
# File 'lib/rdstation/error/formatter.rb', line 35

def error_format
  @error_format ||= RDStation::Error::Format.new(@errors)
end

#from_flat_hash ⇒ Object



21
22
23
# File 'lib/rdstation/error/formatter.rb', line 21

def from_flat_hash
  [@errors]
end

#from_hash_of_arrays ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rdstation/error/formatter.rb', line 25

def from_hash_of_arrays
  @errors.each_with_object([]) do |errors, array_of_errors|
    attribute_name = errors.first
    attribute_errors = errors.last
    path = { 'path' => "body.#{attribute_name}" }
    errors = attribute_errors.map { |error| error.merge(path) }
    array_of_errors.push(*errors)
  end
end

#to_array ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/rdstation/error/formatter.rb', line 10

def to_array
  case error_format.format
  when RDStation::Error::Format::FLAT_HASH
    from_flat_hash
  when RDStation::Error::Format::HASH_OF_ARRAYS
    from_hash_of_arrays
  else
    @errors
  end
end