Module: FastSerializer::Serializer::ClassMethods

Defined in:
lib/fast_serializer/serializer.rb

Instance Method Summary collapse

Instance Method Details

#cacheFastSerializer::Cache

Get the cache implemtation used to store cacheable serializers.



211
212
213
214
215
216
217
218
219
# File 'lib/fast_serializer/serializer.rb', line 211

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

#cache=(cache) ⇒ void

This method returns an undefined value.

Set the cache implementation used to store cacheable serializers.

Parameters:



225
226
227
228
229
230
# File 'lib/fast_serializer/serializer.rb', line 225

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

#cache_ttlNumeric

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

Returns:

  • (Numeric)


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

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

#cache_ttl=(value) ⇒ void

This method returns an undefined value.

Set the time to live on a cacheable serializer.

Parameters:

  • value (Numeric)

    the time to live in seconds



204
205
206
# File 'lib/fast_serializer/serializer.rb', line 204

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.

Parameters:

  • cacheable (Boolean) (defaults to: true)

    pass false if the serializer is not cacheable

  • ttl (Numeric) (defaults to: nil)

    the time to live in seconds for a cacheable serializer

  • cache (FastSerializer::Cache) (defaults to: nil)

    the cache implementation to use for a cacheable serializer



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

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)


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

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

#new(object, options = nil) ⇒ Object

:nodoc:



233
234
235
236
237
238
239
240
241
# File 'lib/fast_serializer/serializer.rb', line 233

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.

Parameters:

  • fields (Array<Symbol>)

    the fields to remove



152
153
154
155
156
157
158
159
# File 'lib/fast_serializer/serializer.rb', line 152

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_fieldsArray<FastSerializer::SerializedField>

Return a list of the SerializedFields defined for the class.



246
247
248
249
250
251
252
253
# File 'lib/fast_serializer/serializer.rb', line 246

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) ⇒ void

This method returns an undefined value.

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.

  • condition: Block or method name that will be called at runtime bound to the serializer that will determine if the attribute will be included or not.

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

Parameters:

  • fields (Array<Symbol, Hash>)

    the fields to serialize. If the last argument is a hash, it will be treated as options for the serialized fields.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fast_serializer/serializer.rb', line 108

def serialize(*fields)
  options = {}
  if fields.size > 1 && fields.last.is_a?(Hash)
    fields.last.each do |key, value|
      options[key.to_sym] = value
    end
    fields = fields[0, fields.size - 1]
  end
  as = options.delete(:as)
  optional = options.delete(:optional) || false
  delegate = options.delete(:delegate) || true
  enumerable = options.delete(:enumerable) || false
  serializer = options.delete(:serializer)
  serializer_options = options.delete(:serializer_options)
  condition = options.delete(:if)

  unless options.empty?
    raise ArgumentError.new("Unsupported serialize options: #{options.keys.join(", ")}")
  end

  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?("?")
      name = field.to_s.chomp("?")
    end

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

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