Class: CacheCrispies::Optional

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

Overview

Represents an optional condition

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Optional

Returns a new instance of Optional

Parameters:

  • block (Proc)

    the key of the attribute to include


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

def initialize(key)
  @key = key
end

Instance Method Details

#true_for?(serializer) ⇒ Boolean

Test the truthiness of the optional condition against a model and options

Parameters:

  • model (Object)

    typically ActiveRecord::Base, but could be anything

  • options (Hash)

    any optional values from the serializer instance

Returns:

  • (Boolean)

    the condition's truthiness


26
27
28
29
30
# File 'lib/cache_crispies/optional.rb', line 26

def true_for?(serializer)
  included = Array(serializer.options.fetch(:include, [])).map(&:to_sym)

  included.include?(key) || included.include?(:*)
end

#uidObject

A system-wide unique ID used for memoizaiton


16
17
18
19
# File 'lib/cache_crispies/optional.rb', line 16

def uid
  # Just reusing the key seems to make sense
  key
end