Class: CacheCrispies::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_crispies/base.rb

Overview

The Base class that all serializer classes should inherit from

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options = {}) ⇒ Base

Initializes a new instance of CacheCrispies::Base, or really, it should always be a subclass of CacheCrispies::Base.

Parameters:

  • model (Object)

    typically ActiveRecord::Base, but could be anything

  • options (Hash) (defaults to: {})

    any optional custom values you want to be accessible in your subclass.



34
35
36
37
# File 'lib/cache_crispies/base.rb', line 34

def initialize(model, options = {})
  @model = model
  @options = options
end

Class Attribute Details

.attributesObject (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

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/cache_crispies/base.rb', line 9

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/cache_crispies/base.rb', line 9

def options
  @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.

Examples:

cache based off models provided in options

def self.cache_key_addons(options)
  [options[:current_user].cache_key]
end

time-based caching

def self.cache_key_addons(_options)
  [Date.today.to_s]
end

Parameters:

  • options (Hash)

    the options hash passed to the serializer, will be

Returns:

  • (Array<String>)


93
94
95
# File 'lib/cache_crispies/base.rb', line 93

def self.cache_key_addons(_options = {})
  []
end

.cache_key_baseString

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.

Returns:

  • (String)

    a cache key for the class



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_keySymbol

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.

Returns:

  • (Symbol)

    a symbol to be used as a key for a JSON-ready Hash



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.

Returns:

  • (Boolean)


50
51
52
# File 'lib/cache_crispies/base.rb', line 50

def self.do_caching?
  false
end

.file_hashesArray<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.

Returns:

  • (Array<String>)

    an array of uniq, sorted serializer file hashes



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.

Parameters:

  • other (Class)

    the inheriting child class



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

.keySymbol

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.

Returns:

  • (Symbol)

    a symbol to be used as a key for a JSON-ready Hash



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_jsonHash

Renders the serializer instance to a JSON-ready Hash

Returns:

  • (Hash)

    a JSON-ready hash



42
43
44
# File 'lib/cache_crispies/base.rb', line 42

def as_json
  HashBuilder.new(self).call
end