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.



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

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.



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

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.



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

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.



190
191
192
# File 'lib/fast_serializer/serializer.rb', line 190

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.



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

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)


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

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

#new(object, options = nil) ⇒ Object

:nodoc:



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

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.



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

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.



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

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.



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
135
136
137
138
139
140
141
142
143
# File 'lib/fast_serializer/serializer.rb', line 105

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?("?".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