Class: PlainSerializer::Base

Inherits:
Object
  • Object
show all
Includes:
Configurable, Modifiable, Serializable
Defined in:
lib/plain_serializer/base.rb

Overview

The base class

Instance Method Summary collapse

Methods included from Configurable

included

Methods included from Serializable

included, #serializer, #serializers, #setup_serializer

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/plain_serializer/base.rb', line 11

def initialize(*args)
  Helpers.extract_options!(args)

  @attributes = args
end

Instance Method Details

#serialize(entity) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/plain_serializer/base.rb', line 17

def serialize(entity)
  return if entity.nil?

  @attributes.each_with_object({}) do |attribute, result|
    result[attribute] = send(attribute, entity)
  end
end

#serialize_collection(entities) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/plain_serializer/base.rb', line 25

def serialize_collection(entities)
  return if entities.nil?

  entities.each_with_object([]) do |entity, result|
    result << serialize(entity)
  end
end