Class: GroupSerializer
- Inherits:
-
Object
- Object
- GroupSerializer
- Defined in:
- lib/serializer/group_serializer.rb
Instance Method Summary collapse
-
#initialize(objects, serializer: nil, scope: {}) ⇒ GroupSerializer
constructor
A new instance of GroupSerializer.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(objects, serializer: nil, scope: {}) ⇒ GroupSerializer
Returns a new instance of GroupSerializer.
2 3 4 5 6 7 8 |
# File 'lib/serializer/group_serializer.rb', line 2 def initialize(objects, serializer: nil, scope: {}) raise ArgumentError, 'serializer needs to be specified' if serializer.nil? @objects = objects @serializer = serializer @scope = scope end |
Instance Method Details
#to_h ⇒ Object
20 21 22 23 24 |
# File 'lib/serializer/group_serializer.rb', line 20 def to_h @objects .map { |object| @serializer.new(object, scope: @scope) } .map(&:to_h) end |
#to_json ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/serializer/group_serializer.rb', line 10 def to_json(*) Appsignal.instrument( 'json.serialize', 'Group serializer', @serializer.to_s ) do Oj.dump(to_h, mode: :json) end end |