Module: Angus::Marshalling

Defined in:
lib/angus/marshallings/exceptions.rb,
lib/angus/marshallings/marshalling.rb

Defined Under Namespace

Classes: InvalidGetterError

Class Method Summary collapse

Class Method Details

.marshal_object(object, getters) ⇒ Object

Marshal a complex object.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/angus/marshallings/marshalling.rb', line 12

def self.marshal_object(object, getters)
  result = {}
  getters.each do |getter|
    if getter.is_a?(Hash)
      key = getter.keys[0]
      value = get_value(object, key)

      #HACK to support ActiveRecord::Relation
      if defined?(ActiveRecord::Relation) && value.is_a?(ActiveRecord::Relation)
        value = value.to_a
      end

      if value.is_a?(Array)
        result[key] = value.map { |object| marshal_object(object, getter.values[0]) }
      else
        result[key] = value.nil? ? nil : marshal_object(value, getter.values[0])
      end
    else
      value = get_value(object, getter)
      result[getter] = marshal_scalar(value)
    end
  end
  return result
end