Module: FastSerializer::Serializer

Included in:
ArraySerializer
Defined in:
lib/fast_serializer/serializer.rb

Overview

Models can include this module to define themselves as serializers. A serializer is used to wrap an object and output a hash version of that object suitable for serialization to JSON or other formats.

To define what fields to serialize on the wrapped object, the serializer class must call the serialize class method:

class PersonSerializer
  include FastSerializer::Serializer
  serialize :id, :name
end

This sample serializer will output an object as a hash with keys :name. The values for each field is gotten by calling the corresponding method on the serializer object. By default, each serialized field will automatically define a method that simply delegates to the wrapped object. So if you need provide special handling for a field or serialize a virtual field that doesn’t exist on the parent object, you just need to implement the method on the serializer.

class PersonSerializer
  include FastSerializer::Serializer
  serialize :id, :name

  def name
    "#{object.first_name} #{object.last_name}"
  end
end

Serializers can implement their own options for controlling details about how to serialize the object.

class PersonSerializer
  include FastSerializer::Serializer
  serialize :id, :name

  def name
    if option(:last_first)
      "#{object.last_name}, #{object.first_name}"
    else
      "#{object.first_name} #{object.last_name}"
    end
  end
end

serializer = PersonSerializer.new(person, :last_first => true)

All serializers will honor options for :include (include optional fields), :exclude (exclude fields).

Serializer can also be specified as cacheable. Cacheable serializer will store and fetch the serialized value from a cache. In order to user caching you must set the cache implementation. This can either be done on a global level (FastSerializer.cache) class level, or instance level (:cache option). Then you can specify serializers to be cacheable. This can be done on a class level with the cacheable directive or on an instance level with the :cacheable option. A time to live can also be set at the same levels using cache_ttl.

Serializers are designed to be reusable and must never have any internal state associated with them. Calling as_json on a serializer multiple times must always return the same value.

Serializing a nil object will result in nil rather than an empty hash.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly)

Return the wrapped object that is being serialized.



64
65
66
# File 'lib/fast_serializer/serializer.rb', line 64

def object
  @object
end

#optionsObject (readonly)

Return the options hash (if any) that specified optional details about how to serialize the object.



67
68
69
# File 'lib/fast_serializer/serializer.rb', line 67

def options
  @options
end

Class Method Details

.included(base) ⇒ Object



59
60
61
# File 'lib/fast_serializer/serializer.rb', line 59

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

:nodoc:



297
298
299
# File 'lib/fast_serializer/serializer.rb', line 297

def ==(other)
  other.instance_of?(self.class) && @object == other.object && @options == other.options
end

#as_json(*args) ⇒ Object Also known as: to_hash, to_h

Serialize the wrapped object into a format suitable for passing to a JSON parser.



249
250
251
252
253
254
255
# File 'lib/fast_serializer/serializer.rb', line 249

def as_json(*args)
  return nil unless object
  unless @_serialized
    @_serialized = (cacheable? ? load_from_cache : load_hash).freeze
  end
  @_serialized
end

#cacheObject

Return the cache implementation where this serializer can be stored.



280
281
282
# File 'lib/fast_serializer/serializer.rb', line 280

def cache
  option(:cache) || self.class.cache
end

#cache_keyObject

Returns a array of the elements that make this serializer unique. The key is an array made up of the serializer class name, wrapped object, and serialization options hash.



292
293
294
# File 'lib/fast_serializer/serializer.rb', line 292

def cache_key
  [self.class.name, object, options]
end

#cache_ttlObject

Return the time to live in seconds this serializer can be cached for.



285
286
287
# File 'lib/fast_serializer/serializer.rb', line 285

def cache_ttl
  option(:cache_ttl) || self.class.cache_ttl
end

#cacheable?Boolean

Return true if this serializer is cacheable.

Returns:

  • (Boolean)


275
276
277
# File 'lib/fast_serializer/serializer.rb', line 275

def cacheable?
  option(:cacheable) || self.class.cacheable?
end

#initialize(object, options = nil) ⇒ Object

Create a new serializer for the specified object.

Options can be passed in to control how the object is serialized. Options supported by all Serializers:

  • :include - Field or array of optional field names that should be included in the serialized object.

  • :exclude - Field or array of field names that should be excluded from the serialized object.

  • :cacheable - Override the cacheable behavior set on the class.

  • :cache_ttl - Override the cache ttl set on the class.

  • :cache - Override the cache implementation set on the class.



242
243
244
245
246
# File 'lib/fast_serializer/serializer.rb', line 242

def initialize(object, options = nil)
  @object = object
  @options = options
  @_serialized = nil
end

#option(name) ⇒ Object

Fetch the specified option from the options hash.



270
271
272
# File 'lib/fast_serializer/serializer.rb', line 270

def option(name)
  @options[name] if @options
end

#to_json(options = nil) ⇒ Object

Convert the wrapped object to JSON format.



261
262
263
264
265
266
267
# File 'lib/fast_serializer/serializer.rb', line 261

def to_json(options = nil)
  if defined?(MultiJson)
    MultiJson.dump(as_json)
  else
    JSON.dump(as_json)
  end
end