Class: Xing::Services::ErrorConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/services/error_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(am_object) ⇒ ErrorConverter

Returns a new instance of ErrorConverter.



6
7
8
# File 'lib/xing/services/error_converter.rb', line 6

def initialize(am_object)
  @am_object = am_object
end

Instance Attribute Details

#am_objectObject (readonly)

Returns the value of attribute am_object.



9
10
11
# File 'lib/xing/services/error_converter.rb', line 9

def am_object
  @am_object
end

Instance Method Details

#convertObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/xing/services/error_converter.rb', line 33

def convert
  final_errors = {}
  json_errors.each_key do |key|
    final_errors[key] = {
      :type => json_errors[key][0],
      :message => regular_errors[key][0]
    }
  end
  final_errors
end

#json_errorsObject

This is a terrible hack to preserve the semantic meaning of different error types – neccesary because ActiveModel::Errors frustratingly translates semantic error messages through i18n as soon as it gets them



15
16
17
18
19
20
21
22
23
24
# File 'lib/xing/services/error_converter.rb', line 15

def json_errors
  @json_errors ||= begin
                     old_locale = I18n.locale
                     I18n.locale = "json"
                     am_object.valid?
                     error_hash = am_object.errors.to_hash.deep_dup
                     I18n.locale = old_locale
                     error_hash
                   end
end

#regular_errorsObject



26
27
28
29
30
31
# File 'lib/xing/services/error_converter.rb', line 26

def regular_errors
  @regular_errors ||= begin
                        am_object.valid?
                        am_object.errors.to_hash.deep_dup
                      end
end