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.



186
187
188
189
190
191
192
193
194
# File 'lib/fast_serializer/serializer.rb', line 186

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.



197
198
199
200
201
202
# File 'lib/fast_serializer/serializer.rb', line 197

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.



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

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.



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

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.



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

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)


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

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

#new(object, options = nil) ⇒ Object

:nodoc:



205
206
207
208
209
210
211
212
213
# File 'lib/fast_serializer/serializer.rb', line 205

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.



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

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.



216
217
218
219
220
221
222
223
# File 'lib/fast_serializer/serializer.rb', line 216

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

  • 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.



102
103
104
105
106
107
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
# File 'lib/fast_serializer/serializer.rb', line 102

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

  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, condition: condition)

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