Class: Code42::AttributeSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/code42/attribute_serializer.rb

Instance Method Summary collapse

Instance Method Details

#<<(attribute) ⇒ Object



11
12
13
14
# File 'lib/code42/attribute_serializer.rb', line 11

def <<(attribute)
  add_exception attribute.from, attribute.to
  attributes << attribute
end

#add_exception(from, to) ⇒ Object



3
4
5
# File 'lib/code42/attribute_serializer.rb', line 3

def add_exception(from, to)
  exceptions << AttributeSerializerException.new(from, to)
end

#attribute_for(key) ⇒ Object



84
85
86
87
# File 'lib/code42/attribute_serializer.rb', line 84

def attribute_for(key)
  key = key.to_s
  attributes.detect { |a| a.to.to_s == key.to_s || a.from.to_s == key.to_s }
end

#attributesObject



7
8
9
# File 'lib/code42/attribute_serializer.rb', line 7

def attributes
  @attributes ||= []
end

#deserialize(key, value) ⇒ Object



42
43
44
# File 'lib/code42/attribute_serializer.rb', line 42

def deserialize(key, value)
  Hash[deserialize_key(key), deserialize_value(key, value)]
end

#deserialize_key(key) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/code42/attribute_serializer.rb', line 46

def deserialize_key(key)
  if exception = exception_from(key)
    exception.to
  else
    key.underscore.to_sym
  end
end

#deserialize_value(key, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/code42/attribute_serializer.rb', line 54

def deserialize_value(key, value)
  if klass = attribute_for(key).try(:as)
    if attribute_for(key).try(:collection)
      klass.collection_from_response(value)
    elsif klass.respond_to?(:from_response)
      klass.from_response(value)
    elsif klass.respond_to?(:parse)
      klass.parse(value)
    else
      value
    end
  else
    if value.is_a?(Hash)
      value.inject({}) do |h,a|
        h.merge! deserialize(a[0], a[1])
      end
    else
      value
    end
  end
end

#exception_from(key) ⇒ Object



93
94
95
# File 'lib/code42/attribute_serializer.rb', line 93

def exception_from(key)
  exceptions.from(key)
end

#exception_to(key) ⇒ Object



89
90
91
# File 'lib/code42/attribute_serializer.rb', line 89

def exception_to(key)
  exceptions.to(key)
end

#exceptionsObject



16
17
18
# File 'lib/code42/attribute_serializer.rb', line 16

def exceptions
  @exceptions ||= AttributeSerializerExceptions.new
end

#serialize(key, value) ⇒ Object



20
21
22
# File 'lib/code42/attribute_serializer.rb', line 20

def serialize(key, value)
  Hash[serialize_key(key), serialize_value(value)]
end

#serialize_key(key) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/code42/attribute_serializer.rb', line 76

def serialize_key(key)
  if exception = exception_to(key)
    exception.from
  else
    key.to_s.camelize(:lower)
  end
end

#serialize_value(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/code42/attribute_serializer.rb', line 24

def serialize_value(value)
  if value.respond_to? :serialize
    value.serialize
  elsif value.is_a? DateTime
    value.to_s
  elsif value.is_a? Hash
    value.inject({}) do |h,a|
      h.merge! serialize(a[0], a[1])
    end
  elsif value.is_a? Array
    value.map do |item|
      serialize_value(item)
    end
  else
    value
  end
end