Class: GroupSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/serializer/group_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(objects, serializer: nil) ⇒ GroupSerializer

Returns a new instance of GroupSerializer.

Raises:

  • (ArgumentError)


2
3
4
5
6
7
# File 'lib/serializer/group_serializer.rb', line 2

def initialize(objects, serializer: nil)
  raise ArgumentError, 'serializer needs to be specified' if serializer.nil?

  @objects = objects
  @serializer = serializer
end

Instance Method Details

#to_jsonObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/serializer/group_serializer.rb', line 9

def to_json(*)
  Appsignal.instrument(
    'json.serialize',
    'Group serializer',
    @serializer.to_s
  ) do
    result = @objects.map { |object| @serializer.new(object).to_h }

    Oj.dump(result, mode: :json)
  end
end