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
- .attributes_by_nesting ⇒ Object
-
.cache_key_addons(options = {}) {|options| ... } ⇒ Array<String>
Call with a block returning 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(*key) ⇒ Symbol?
Get or set a JSON key to use as a root key on a collection-type serializable.
-
.dependency_key(key = nil) ⇒ String
Get or set a cache key that can be changed whenever an outside dependency of any kind changes in any way that could change the output of your serializer.
-
.do_caching(value = nil) ⇒ Boolean
(also: do_caching?)
Get or set whether or not this serializer class should allow caching of results.
-
.engine(value = nil) ⇒ Object
Get or set root path of Rails application or engine.
-
.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(*key) ⇒ Symbol?
Get or set 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.
-
#write_to_json(json_writer = nil) ⇒ Oj::StringWriter
Renders the serializer instance to an Oj::StringWriter instance.
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
.attributes_by_nesting ⇒ Object
208 209 210 211 212 |
# File 'lib/cache_crispies/base.rb', line 208 def self.attributes_by_nesting @attributes_by_nesting ||= ( attributes.sort_by(&:nesting).group_by(&:nesting) ) end |
.cache_key_addons(options = {}) {|options| ... } ⇒ Array<String>
Call with a block returning an array of strings that should be added to the cache key for an instance of this serializer. Typically you’d add in string values to uniquely represent the values you’re passing to the serializer so that they are cached separately. But it could also contain any custom logic about how to construct a cache key. Call without a block to act as a getter and return the value.
passed in here as well so you can refernce it if needed. should return an array of strings to use in the cache key
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/cache_crispies/base.rb', line 158 def self.cache_key_addons( = {}, &block) @cache_key_addons ||= nil if block_given? @cache_key_addons = block nil else Array(@cache_key_addons&.call()) end 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.
192 193 194 |
# File 'lib/cache_crispies/base.rb', line 192 def self.cache_key_base @cache_key_base ||= "#{self}-#{file_hashes.join(CACHE_KEY_SEPARATOR)}" end |
.collection_key(*key) ⇒ Symbol?
Get or set 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. Calling the method with a key will set the key, calling it without any arguments will get the key.
Hash, or nil for no key or nil for no key
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cache_crispies/base.rb', line 124 def self.collection_key(*key) @default_collection_key ||= self.key.to_s.pluralize.to_sym # method called with no args so act as a getter if key.empty? if defined? @collection_key return @collection_key else return @default_collection_key end end # method called with args so act as a setter @collection_key = key.first&.to_sym end |
.dependency_key(key = nil) ⇒ String
Get or set a cache key that can be changed whenever an outside dependency of any kind changes in any way that could change the output of your serializer. For instance, if a mixin is changed. Or maybe an object you’re serializing has changed it’s #to_json method. This key should be changed accordingly, to bust the cache so that you’re not serving stale data.
177 178 179 180 181 182 183 184 185 |
# File 'lib/cache_crispies/base.rb', line 177 def self.dependency_key(key = nil) @dependency_key ||= nil # method called with no args so act as a getter return @dependency_key unless key # method called with args so act as a setter @dependency_key = key.to_s end |
.do_caching(value = nil) ⇒ Boolean Also known as: do_caching?
Get or set whether or not this serializer class should allow caching of results. It returns false by default, but can be overridden in child classes. Calling the method with an argument will set the value, calling it without any arguments will get the value.
65 66 67 68 69 70 71 72 73 |
# File 'lib/cache_crispies/base.rb', line 65 def self.do_caching(value = nil) @do_caching ||= false # method called with no args so act as a getter return @do_caching if value.nil? # method called with args so act as a setter @do_caching = !!value end |
.engine(value = nil) ⇒ Object
Get or set root path of Rails application or engine. It uses Rails by default, but can be overriden with your Rails Engine class. Calling the method with an argument will set the value, calling it without any arguments will get the value.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cache_crispies/base.rb', line 83 def self.engine(value = nil) @engine ||= superclass.try(:engine) @engine ||= Rails # method called with no args so act as a getter return @engine if value.nil? # method called with args so act as a setter @engine = value 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.
202 203 204 205 206 |
# File 'lib/cache_crispies/base.rb', line 202 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(*key) ⇒ Symbol?
Get or set 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. Calling the method with a key will set the key, calling it without any arguments will get the key.
Hash, or nil for no key or nil for no key
104 105 106 107 108 109 110 111 112 |
# File 'lib/cache_crispies/base.rb', line 104 def self.key(*key) @default_key ||= to_s.demodulize.chomp('Serializer').underscore.to_sym # method called with no args so act as a getter return defined?(@key) ? @key : @default_key if key.empty? # method called with args so act as a setter @key = key.first&.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 |
#write_to_json(json_writer = nil) ⇒ Oj::StringWriter
Renders the serializer instance to an Oj::StringWriter instance
50 51 52 53 54 55 56 |
# File 'lib/cache_crispies/base.rb', line 50 def write_to_json(json_writer = nil) json_writer ||= Oj::StringWriter.new(mode: :rails) JsonBuilder.new(self).call(json_writer) json_writer end |