Module: FastSerializer::Serializer::ClassMethods

Defined in:
lib/fast_serializer/serializer.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject

Get the cache implemtation used to store cacheable serializers.



170
171
172
173
174
175
176
177
178
# File 'lib/fast_serializer/serializer.rb', line 170

def cache
  if defined?(@cache)
    @cache
  elsif superclass.respond_to?(:cache)
    superclass.cache
  else
    FastSerializer.cache
  end
end

#cache=(cache) ⇒ Object

Set the cache implementation used to store cacheable serializers.



181
182
183
184
185
186
# File 'lib/fast_serializer/serializer.rb', line 181

def cache=(cache)
  if defined?(ActiveSupport::Cache::Store) && cache.is_a?(ActiveSupport::Cache::Store)
    cache = Cache::ActiveSupportCache.new(cache)
  end
  @cache = cache
end

#cache_ttlObject

Return the time to live in seconds for a cacheable serializer.



154
155
156
157
158
159
160
161
162
# File 'lib/fast_serializer/serializer.rb', line 154

def cache_ttl
  if defined?(@cache_ttl)
    @cache_ttl
  elsif superclass.respond_to?(:cache_ttl)
    superclass.cache_ttl
  else
    nil
  end
end

#cache_ttl=(value) ⇒ Object

Set the time to live on a cacheable serializer.



165
166
167
# File 'lib/fast_serializer/serializer.rb', line 165

def cache_ttl=(value)
  @cache_ttl = value
end

#cacheable(cacheable = true, ttl: nil, cache: nil) ⇒ Object

Specify the cacheability of the serializer.

You can specify the cacheable state (defaults to true) of the class. Subclasses will inherit the cacheable state of their parent class, so if you have non-cacheable serializer subclassing a cacheable parent class, you can call cacheable false to override the parent behavior.

You can also specify the cache time to live (ttl) in seconds and the cache implementation to use. Both of these values are inherited on subclasses.



139
140
141
142
143
# File 'lib/fast_serializer/serializer.rb', line 139

def cacheable(cacheable = true, ttl: nil, cache: nil)
  @cacheable = cacheable
  self.cache_ttl = ttl if ttl
  self.cache = cache if cache
end

#cacheable?Boolean

Return true if the serializer class is cacheable.

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/fast_serializer/serializer.rb', line 146

def cacheable?
  unless defined?(@cacheable)
    @cacheable = superclass.cacheable? if superclass.respond_to?(:cacheable?)
  end
  !!@cacheable
end

#new(object, options = nil) ⇒ Object

:nodoc:



189
190
191
192
193
194
195
196
197
# File 'lib/fast_serializer/serializer.rb', line 189

def new(object, options = nil)
  context = SerializationContext.current
  if context
    # If there's a context in scope this will load duplicate entries from the context rather than creating new instances.
    context.load(self, object, options)
  else
    super
  end
end

#remove(*fields) ⇒ Object

Remove a field from being serialized. This can be useful in subclasses if they need to remove a field defined by the parent class.



122
123
124
125
126
127
128
129
# File 'lib/fast_serializer/serializer.rb', line 122

def remove(*fields)
  remove_fields = fields.collect(&:to_sym)
  field_list = []
  serializable_fields.each do |existing_field|
    field_list << existing_field unless remove_fields.include?(existing_field.name)
  end
  @serializable_fields = field_list.freeze
end

#serializable_fieldsObject

Return a list of the SerializedFields defined for the class.



200
201
202
203
204
205
206
207
# File 'lib/fast_serializer/serializer.rb', line 200

def serializable_fields
  unless defined?(@serializable_fields) && @serializable_fields
    fields = superclass.send(:serializable_fields).dup if superclass.respond_to?(:serializable_fields)
    fields ||= []
    @serializable_fields = fields.freeze
  end
  @serializable_fields
end

#serialize(*fields, as: nil, optional: false, delegate: true, serializer: nil, serializer_options: nil, enumerable: false) ⇒ Object

Define one or more fields to include in the serialized object. Field values will be gotten by calling the method of the same name on class including this module.

Several options can be specified to control how the field is serialized.

  • as: Name to call the field in the serialized hash. Defaults to the same as the field name (withany ? stripped off the end for boolean fields). This option can only be specified for a single field.

  • optional: Boolean flag indicating if the field is optional in the serialized value (defaults to false). Optional fields are only included if the :include option to the as_json method includes the field name.

  • delegate: Boolean flag indicating if the field call should be delegated to the wrapped object (defaults to true). When this is supplied, a method will be automatically defined on the serializer with the name of the field that simply then calls the same method on the wrapped object.

  • serializer: Class that should be used to serialize the field. If this option is specified, the field value will be serialized using the specified serializer class which should include this module. Otherwise, the as_json method will be called on the field class.

  • serializer_options: Options that should be used for serializing the field for when the :serializer option has been specified.

  • enumerable: Boolean flag indicating if the field is enumerable (defaults to false). This option is only used if the :serializer option has been set. If the field is marked as enumerable, then the value will be serialized as an array with each element wrapped in the specified serializer.

Subclasses will inherit all of their parent classes serialized fields. Subclasses can override fields defined on the parent class by simply defining them again.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fast_serializer/serializer.rb', line 99

def serialize(*fields, as: nil, optional: false, delegate: true, serializer: nil, serializer_options: nil, enumerable: false)
  if as && fields.size > 1
    raise ArgumentError.new("Cannot specify :as argument with multiple fields to serialize")
  end

  fields.each do |field|
    name = as
    if name.nil? && field.to_s.end_with?("?".freeze)
      name = field.to_s.chomp("?".freeze)
    end

    field = field.to_sym
    attribute = (name || field).to_sym
    add_field(attribute, optional: optional, serializer: serializer, serializer_options: serializer_options, enumerable: enumerable)

    if delegate && !method_defined?(attribute)
      define_delegate(attribute, field)
    end
  end
end