Class: USerializer::CompositeSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/userializer/composite_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(objs, opts = {}) ⇒ CompositeSerializer

Returns a new instance of CompositeSerializer.



32
33
34
35
# File 'lib/userializer/composite_serializer.rb', line 32

def initialize(objs, opts = {})
  @opts = opts
  @objs = compose_objs(objs)
end

Instance Method Details

#merge_root(res, opts) ⇒ Object



37
38
39
40
41
# File 'lib/userializer/composite_serializer.rb', line 37

def merge_root(res, opts)
  @objs.each do |obj|
    obj.merge_root(res, opts)
  end
end

#serialize(*_args) ⇒ Object



50
51
52
# File 'lib/userializer/composite_serializer.rb', line 50

def serialize(*_args)
  to_hash
end

#to_hashObject



43
44
45
46
47
48
# File 'lib/userializer/composite_serializer.rb', line 43

def to_hash
  res = {}

  merge_root(res, @opts)
  res
end

#to_json(*_args) ⇒ Object



54
55
56
# File 'lib/userializer/composite_serializer.rb', line 54

def to_json(*_args)
  Oj.dump(to_hash, mode: :compat)
end