module FastSerializer::Serializer::ClassMethods
Public Instance Methods
Get the cache implemtation used to store cacheable serializers.
# 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
Set the cache implementation used to store cacheable serializers.
# 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
Return the time to live in seconds for a cacheable serializer.
# 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
Set the time to live on a cacheable serializer.
# File lib/fast_serializer/serializer.rb, line 190 def cache_ttl=(value) @cache_ttl = value end
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.
# 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
Return true if the serializer class is cacheable.
# File lib/fast_serializer/serializer.rb, line 171 def cacheable? unless defined?(@cacheable) @cacheable = superclass.cacheable? if superclass.respond_to?(:cacheable?) end !!@cacheable end
Remove a field from being serialized. This can be useful in subclasses if they need to remove a field defined by the parent class.
# 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
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.
# 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