lib/quirky-api/serializers/quirky_serializer.rb
# This allows you to cache certain fields within a serializer.  The issue
# with caching an entire serialized object is that of persistence --
# a lot of things change, all the time, making it very difficult to cache
# an entire object and ensure the correct responses.
#
# quirky-api takes a per-field approach to caching.  When an object is
# serialized, every field is configured to be cached will be cached
# invididually, allowing for more fine-grained caching abilities.  Each
# field has a cache key like so:
#
#    [object_id].[updated_at]/[field_name]
#
# Therefore, making changes to the overlying object will automatically
# bust the cache for the serialized object and return the new information.
#
# +caches+ allows multiple arguments, and may be passed any or all of the
# following:
#
#  * +:all+ will cache every field on the object, including attributes,
#    optional fields and associations.
#  * +:associations+ will cache only the associations on an object.  This
#    is marginally more tricky since associations tend to be an entirely
#    different object on their own.  In order to properly bust caches on
#    cached associations, be sure to touch the cached object.
#  * +:optional_fields+ will cache all optional fields.
#  * +:fields+ will cache all attributes.
#  * +:field_name+ will cache the field of name +field_name+, regardless
#    of what type that field is.  This manes that if the field is an
#    association, it will be cached.  Likewise, if it is a 'regular'
#    attribute, it will be cached.
#
#  @example
#    class UserSerializer < QuirkySerializer
#      attributes :first, :last, :email
#      caches :first, :last
#    end
#
#  @example
#    class UserSerializer < QuirkySerializer
#      attributes :first, :last, :email
#      optional :bio
#      caches :optional_fields
#    end
#
#  @example
#    class UserSerializer < QuirkySerializer
#      attributes :first, :last, :email
#      optional :bio
#      associations :profile
#      caches :all
#    end
#
#  @param [*args]  A list of cacheable attributes, per the notes above.
#
This allows you to cache certain fields within a serializer.  The issue
with caching an entire serialized object is that of persistence
a lot of things change, all the time, making it very difficult to cache
an entire object and ensure the correct responses.

quirky-api takes a per-field approach to caching.  When an object is
serialized, every field is configured to be cached will be cached
invididually, allowing for more fine-grained caching abilities.  Each
field has a cache key like so.



Therefore, making changes to the overlying object will automatically
bust the cache for the serialized object and return the new information.

+caches+ allows multiple arguments, and may be passed any or all of the
following.

+:all+ will cache every field on the object, including attributes,
optional fields and associations.
+:associations+ will cache only the associations on an object.  This
is marginally more tricky since associations tend to be an entirely
different object on their own.  In order to properly bust caches on
cached associations, be sure to touch the cached object.
+:optional_fields+ will cache all optional fields.
+:fields+ will cache all attributes.
+:field_name+ will cache the field of name +field_name+, regardless
of what type that field is.  This manes that if the field is an
association, it will be cached.  Likewise, if it is a 'regular'
attribute, it will be cached.







@param [*args]  A list of cacheable attributes, per the notes above.
This allows you to cache certain fields within a serializer . The issue with caching an entire serialized object is that of persistence a lot of things change all the time making it very difficult to cache an entire object and ensure the correct responses . quirky - api takes a per - field approach to caching . When an object is serialized every field is configured to be cached will be cached invididually allowing for more fine - grained caching abilities . Each field has a cache key like so . Therefore making changes to the overlying object will automatically bust the cache for the serialized object and return the new information . + caches + allows multiple arguments and may be passed any or all of the following . + : all + will cache every field on the object including attributes optional fields and associations . + : associations + will cache only the associations on an object . This is marginally more tricky since associations tend to be an entirely different object on their own . In order to properly bust caches on cached associations be sure to touch the cached object . + : optional_fields + will cache all optional fields . + : fields + will cache all attributes . + : field_name + will cache the field of name + field_name + regardless of what type that field is . This manes that if the field is an association it will be cached . Likewise if it is a ' regular ' attribute it will be cached . @param [ * args ] A list of cacheable attributes per the notes above .
def caches(*attrs)
      fields = []
      if attrs.include? :all
        fields.concat [*self._attributes.keys].map(&:to_sym) +
                      [*self._optional_fields].map(&:to_sym) +
                      [*self._associations].map(&:to_sym)
        attrs.delete(:all)
      end
      if attrs.include? :associations
        fields.concat self._associations
        attrs.delete(:associations)
      end
      if attrs.include? :optional_fields
        fields.concat self._optional_fields
        attrs.delete(:optional_fields)
      end
      if attrs.include? :fields
        fields.concat self._attributes
        attrs.delete(:fields)
      end
      self._cacheable_fields = fields.concat(attrs)
    end
def caches ( * attrs ) fields = [ ] if attrs . include? :all fields . concat [ * self . _attributes . keys ] . map ( & :to_sym ) + [ * self . _optional_fields ] . map ( & :to_sym ) + [ * self . _associations ] . map ( & :to_sym ) attrs . delete ( :all ) end if attrs . include? :associations fields . concat self . _associations attrs . delete ( :associations ) end if attrs . include? :optional_fields fields . concat self . _optional_fields attrs . delete ( :optional_fields ) end if attrs . include? :fields fields . concat self . _attributes attrs . delete ( :fields ) end self . _cacheable_fields = fields . concat ( attrs ) end
This allows you to cache certain fields within a serializer.
This allows you to cache certain fields within a serializer .
attrs