Module: Angus::Marshalling

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

Class Method Summary collapse

Class Method Details

.marshal(object) ⇒ Array

Marshal an object

This method is intended for scalar objects and arrays of scalar objects.

For more complex objects: If object is an array, this method returns an array with all the elements marshalled Else returns a marshalled representation of the object.

Parameters:

  • object

    The object to be marshalled

Returns:

  • (Array)

    An object suitable for be converted easily to JSON notation

See Also:



19
20
21
22
23
24
25
# File 'lib/angus/marshallings/marshalling.rb', line 19

def self.marshal(object)
  if object.is_a?(Array)
    object.map { |element| marshal(element) }
  else
    marshal_scalar(object)
  end
end

.marshal_object(object, getters) ⇒ Object

Marshal a complex object.

Parameters:

  • object

    The object to be marshalled

  • getters

    An array of getters / hashes that will be used to obtain the information from the object.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/angus/marshallings/marshalling.rb', line 32

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)

      #TODO Consider adding ActiveRecord::Relation support
      #ex: "if value.is_a?(Array) || value.is_a?(ActiveRecord::Relation)"
      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