Class: Exchequer::ErrorMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/exchequer/error_mapper.rb

Constant Summary collapse

MAPPINGS =
{
  'Credit card expiration year'  => :expiration_year,
  'Credit card expiration month' => :expiration_month,
  'Credit card number'           => :full_number,
  'First name'                   => :first_name,
  'Last name'                    => :last_name
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination) ⇒ ErrorMapper

Returns a new instance of ErrorMapper.



14
15
16
# File 'lib/exchequer/error_mapper.rb', line 14

def initialize(source, destination)
  @source, @destination = source, destination
end

Class Method Details

.map(source, arguments) ⇒ Object



10
11
12
# File 'lib/exchequer/error_mapper.rb', line 10

def self.map(source, arguments)
  new(source, arguments[:to]).map_errors
end

Instance Method Details

#map_errorsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/exchequer/error_mapper.rb', line 18

def map_errors
  source.errors[:base].dup.each do |error|
    label = MAPPINGS.keys.detect { |key| error[/^#{key}:/] }

    if label
      destination.errors[MAPPINGS[label]] << error.gsub(/^#{label}:\s+/, '')
    else
      destination.errors[:base] << error
    end
  end
end