Module: Pliny::Helpers::Serialize

Defined in:
lib/pliny/helpers/serialize.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/pliny/helpers/serialize.rb', line 3

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#serialize(data, structure = :default) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pliny/helpers/serialize.rb', line 7

def serialize(data, structure = :default)
  serializer_class = self.class.serializer_class

  if serializer_class.nil?
    raise <<-eos.strip
No serializer has been specified for this endpoint. Please specify one with
`serializer Serializers::ModelName` in the endpoint.
    eos
  end

  env['pliny.serializer_arity'] = data.respond_to?(:size) ? data.size : 1

  start = Time.now
  serializer_class.new(structure).serialize(data).tap do
    env['pliny.serializer_timing'] = (Time.now - start).to_f
  end
end