lib/fast_serializer/serializer.rb in fast_serializer-1.1.2 vs lib/fast_serializer/serializer.rb in fast_serializer-1.1.3
- old
+ new
@@ -55,11 +55,10 @@
# Serializers are designed to be reusable and must never have any internal state associated with them. Calling
# +as_json+ on a serializer multiple times must always return the same value.
#
# Serializing a nil object will result in nil rather than an empty hash.
module Serializer
-
def self.included(base)
base.extend(ClassMethods)
base.extend(ArrayHelper) unless base.is_a?(FastSerializer::ArraySerializer)
end
@@ -100,10 +99,14 @@
# * 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.
+ #
+ # @param 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.
+ # @return [void]
def serialize(*fields)
options = {}
if fields.size > 1 && fields.last.is_a?(Hash)
fields.last.each do |key, value|
options[key.to_sym] = value
@@ -117,21 +120,21 @@
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(', ')}")
+ 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)
+ 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)
@@ -142,10 +145,12 @@
end
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.
+ #
+ # @param fields [Array<Symbol>] the fields to remove
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)
@@ -159,41 +164,52 @@
# cacheable state of their parent class, so if you have non-cacheable serializer subclassing a
# cacheable parent class, you can call <tt>cacheable false</tt> 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.
+ #
+ # @param cacheable [Boolean] pass false if the serializer is not cacheable
+ # @param ttl [Numeric] the time to live in seconds for a cacheable serializer
+ # @param cache [FastSerializer::Cache] the cache implementation to use for a cacheable serializer
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.
+ #
+ # @return [Boolean]
def cacheable?
unless defined?(@cacheable)
@cacheable = superclass.cacheable? if superclass.respond_to?(:cacheable?)
end
!!@cacheable
end
# Return the time to live in seconds for a cacheable serializer.
+ #
+ # @return [Numeric]
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.
+ #
+ # @param value [Numeric] the time to live in seconds
+ # @return [void]
def cache_ttl=(value)
@cache_ttl = value
end
# Get the cache implemtation used to store cacheable serializers.
+ #
+ # @return [FastSerializer::Cache]
def cache
if defined?(@cache)
@cache
elsif superclass.respond_to?(:cache)
superclass.cache
@@ -201,10 +217,13 @@
FastSerializer.cache
end
end
# Set the cache implementation used to store cacheable serializers.
+ #
+ # @param cache [FastSerializer::Cache]
+ # @return [void]
def cache=(cache)
if defined?(ActiveSupport::Cache::Store) && cache.is_a?(ActiveSupport::Cache::Store)
cache = Cache::ActiveSupportCache.new(cache)
end
@cache = cache
@@ -220,10 +239,12 @@
super
end
end
# Return a list of the SerializedFields defined for the class.
+ #
+ # @return [Array<FastSerializer::SerializedField>]
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
@@ -247,30 +268,30 @@
# Add the field to the frozen list of fields.
field_list = []
added = false
serializable_fields.each do |existing_field|
- if existing_field.name == name
- field_list << field
+ field_list << if existing_field.name == name
+ field
else
- field_list << existing_field
+ existing_field
end
end
field_list << field unless added
@serializable_fields = field_list.freeze
end
# Define a delegate method name +attribute+ that invokes the +field+ method on the wrapped object.
def define_delegate(attribute, field)
- define_method(attribute){ object.send(field) }
+ define_method(attribute) { object.send(field) }
end
end
module ArrayHelper
# Helper method to serialize an array of values using this serializer.
def array(values, options = nil)
- options = (options ? options.merge(:serializer => self) : {:serializer => self})
+ options = (options ? options.merge(serializer: self) : {serializer: self})
FastSerializer::ArraySerializer.new(values, options)
end
end
# Create a new serializer for the specified object.
@@ -293,18 +314,16 @@
end
# Serialize the wrapped object into a format suitable for passing to a JSON parser.
def as_json(*args)
return nil unless object
- unless @_serialized
- @_serialized = (cacheable? ? load_from_cache : load_hash).freeze
- end
+ @_serialized ||= (cacheable? ? load_from_cache : load_hash).freeze
@_serialized
end
- alias :to_hash :as_json
- alias :to_h :as_json
+ alias_method :to_hash, :as_json
+ alias_method :to_h, :as_json
# Convert the wrapped object to JSON format.
def to_json(options = {})
if defined?(MultiJson)
MultiJson.dump(as_json, options)
@@ -361,11 +380,11 @@
SerializationContext.use do
self.class.serializable_fields.each do |field|
name = field.name
if field.optional?
- next unless include_fields && include_fields.include?(name)
+ next unless include_fields&.include?(name)
end
next if excluded_fields && excluded_fields[name] == true
condition = field.condition
next if condition && !send(condition)
@@ -417,10 +436,10 @@
options.each do |key, value|
hash_key[key] = options_cache_key(value)
end
hash_key
elsif options.is_a?(Enumerable)
- options.collect{|option| options_cache_key(option)}
+ options.collect { |option| options_cache_key(option) }
else
options
end
end