Class: CacheCrispies::Base
- Inherits:
-
Object
- Object
- CacheCrispies::Base
- Defined in:
- lib/cache_crispies/base.rb
Overview
The Base class that all serializer classes should inherit from
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.cache_key_addons(_options = {}) ⇒ Array<String>
An array of strings that should be added to the cache key for an instance of this serializer.
-
.cache_key_base ⇒ String
Return a cache key string for the serializer class to be included in the cache key for the instances.
-
.collection_key ⇒ Symbol
A JSON key to use as a root key on a collection-type serializable.
-
.do_caching? ⇒ Boolean
Whether or not this serializer class should allow caching of results.
-
.file_hashes ⇒ Array<String>
Return an array of cache key string for this serializer and all nested and deeply nested serializers.
-
.inherited(other) ⇒ void
Define class-level instance variables and their default values when the class is inherited by another class.
-
.key ⇒ Symbol
A JSON key to use as a root key on a non-collection serializable.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Renders the serializer instance to a JSON-ready Hash.
-
#initialize(model, options = {}) ⇒ Base
constructor
Initializes a new instance of CacheCrispies::Base, or really, it should always be a subclass of CacheCrispies::Base.
Constructor Details
#initialize(model, options = {}) ⇒ Base
Initializes a new instance of CacheCrispies::Base, or really, it should always be a subclass of CacheCrispies::Base.
34 35 36 37 |
# File 'lib/cache_crispies/base.rb', line 34 def initialize(model, = {}) @model = model @options = end |
Class Attribute Details
.attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
24 25 26 |
# File 'lib/cache_crispies/base.rb', line 24 def attributes @attributes end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/cache_crispies/base.rb', line 9 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/cache_crispies/base.rb', line 9 def @options end |
Class Method Details
.cache_key_addons(_options = {}) ⇒ Array<String>
An array of strings that should be added to the cache key for an instance of this serializer. Typically you’d add in the #cache_key or string value for any extra models or data passed in through the options hash here. But it could also contain any custom logic about how to construct a cache key. This method is meant to be overridden in subclasses.
passed in here as well so you can refernce it if needed.
93 94 95 |
# File 'lib/cache_crispies/base.rb', line 93 def self.cache_key_addons( = {}) [] end |
.cache_key_base ⇒ String
Return a cache key string for the serializer class to be included in the cache key for the instances. The key includes the name of the class, and a digest of the contents of the main class file.
102 103 104 |
# File 'lib/cache_crispies/base.rb', line 102 def self.cache_key_base @cache_key_base ||= "#{self}-#{file_hashes.join(CACHE_KEY_SEPARATOR)}" end |
.collection_key ⇒ Symbol
A JSON key to use as a root key on a collection-type serializable. By deafult it’s the plural version of .key, but it can be overridden in a subclass to be anything.
68 69 70 71 72 |
# File 'lib/cache_crispies/base.rb', line 68 def self.collection_key return nil unless key key.to_s.pluralize.to_sym end |
.do_caching? ⇒ Boolean
Whether or not this serializer class should allow caching of results. It is set to false by default, but can be overridden in child classes.
50 51 52 |
# File 'lib/cache_crispies/base.rb', line 50 def self.do_caching? false end |
.file_hashes ⇒ Array<String>
Return an array of cache key string for this serializer and all nested and deeply nested serializers. The purpose of grabbing all this data is to be able to construct a cache key that will be busted if any of the nested serializers, no matter how deep, change at all.
112 113 114 115 116 |
# File 'lib/cache_crispies/base.rb', line 112 def self.file_hashes @file_hashes ||= ( [file_hash] + nested_serializers.flat_map(&:file_hashes) ).uniq.sort end |
.inherited(other) ⇒ void
This method returns an undefined value.
Define class-level instance variables and their default values when the class is inherited by another class. This is not meant to be called directly. It is called internally by Ruby.
17 18 19 20 21 |
# File 'lib/cache_crispies/base.rb', line 17 def self.inherited(other) other.instance_variable_set(:@attributes, []) other.instance_variable_set(:@nesting, []) other.instance_variable_set(:@conditions, []) end |
.key ⇒ Symbol
A JSON key to use as a root key on a non-collection serializable. by default it’s the name of the class without the “Serializer” part. But it can be overridden in a subclass to be anything.
59 60 61 |
# File 'lib/cache_crispies/base.rb', line 59 def self.key to_s.demodulize.chomp('Serializer').underscore.to_sym end |
Instance Method Details
#as_json ⇒ Hash
Renders the serializer instance to a JSON-ready Hash
42 43 44 |
# File 'lib/cache_crispies/base.rb', line 42 def as_json HashBuilder.new(self).call end |