Class: CacheCrispies::Condition

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

Overview

Represents an instance of a conditional built by a Base.show_if call

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Condition

Returns a new instance of Condition

Parameters:

  • block (Proc)

    a block containing the logic for the condition



10
11
12
# File 'lib/cache_crispies/condition.rb', line 10

def initialize(block)
  @block = block
end

Instance Method Details

#true_for?(model, options = {}) ⇒ Boolean

Test the truthiness of the condition against a model and options

Parameters:

  • model (Object)

    typically ActiveRecord::Base, but could be anything

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

    any optional values from the serializer instance

Returns:

  • (Boolean)

    the condition’s truthiness



27
28
29
30
31
32
33
34
35
36
# File 'lib/cache_crispies/condition.rb', line 27

def true_for?(model, options = {})
  !!case block.arity
    when 0
      block.call
    when 1
      block.call(model)
    else
      block.call(model, options)
    end
end

#uidObject

A system-wide unique ID used for memoizaiton



17
18
19
20
# File 'lib/cache_crispies/condition.rb', line 17

def uid
  # Just reusing the block's object_id seems to make sense
  block.object_id
end