Class: CacheCrispies::Collection

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

Overview

Handles rendering and possibly caching a collection of models using a

Serializer

Instance Method Summary collapse

Constructor Details

#initialize(collection, serializer, options = {}) ⇒ Collection

Initializes a new instance of CacheCrispies::Collection

Parameters:

  • colleciton (Object)

    typically an enumerable containing instances of ActiveRecord::Base, but could be any enumerable

  • serializer (CacheCrispies::Base)

    a class inheriting from CacheCrispies::Base

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

    any optional values from the serializer instance



14
15
16
17
18
# File 'lib/cache_crispies/collection.rb', line 14

def initialize(collection, serializer, options = {})
  @collection = collection
  @serializer = serializer
  @options = options
end

Instance Method Details

#as_jsonHash

Renders the collection to a JSON-ready Hash trying to cache the hash

along the way

Returns:

  • (Hash)

    the JSON-ready Hash



24
25
26
27
28
29
30
# File 'lib/cache_crispies/collection.rb', line 24

def as_json
  if serializer.do_caching? && collection.respond_to?(:cache_key)
    cached_json
  else
    uncached_json
  end
end