class CacheCrispies::Condition

Represents an instance of a conditional built by a {CacheCrispies::Base.show_if} call

Attributes

block[R]

Public Class Methods

new(block) click to toggle source

Returns a new instance of Condition

@param block [Proc] a block containing the logic for the condition

# File lib/cache_crispies/condition.rb, line 10
def initialize(block)
  @block = block
end

Public Instance Methods

true_for?(serializer) click to toggle source

Test the truthiness of the condition against the serializer instance

@param serializer [Object] CacheCrispies::Base serializer instance @return [Boolean] the condition's truthiness

# File lib/cache_crispies/condition.rb, line 26
def true_for?(serializer)
  return !!serializer.public_send(block) if block.is_a?(Symbol)

  !!execute_block(serializer.model, serializer.options)
end
uid() click to toggle source

A system-wide unique ID used for memoizaiton

@eturn [Integer] the unique ID for this condition

# 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

Private Instance Methods

execute_block(model, options) click to toggle source
# File lib/cache_crispies/condition.rb, line 36
def execute_block(model, options)
  case block.arity
  when 0
    block.call
  when 1
    block.call(model)
  else
    block.call(model, options)
  end
end