module NutritionCalculator::CachedOutputsWithRecalculation::InstanceMethods

Private Instance Methods

cache(name, &block) click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 123
def cache(name, &block)
  cached_values.fetch(name) do
    cached_values[name] = block.call
  end
end
cache_and_debug(name, &block) click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 135
def cache_and_debug(name, &block)
  cache(name) do
    run_and_debug(name, &block)
  end
end
cached_values() click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 107
def cached_values
  @cached_values ||= {}
end
recalculate!() click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 118
def recalculate!
  logger.debug "Input received; clearing cached calculations."
  @cached_values = {}
end
require_input(name) click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 129
def require_input(name)
  unless instance_variable_defined?("@#{name}")
    raise RuntimeError, "Required input missing: `#{name}`."
  end
end
run_and_debug(name, &block) click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 141
def run_and_debug(name, &block)
  debug_value(name) { instance_eval &block }
end
validate_input!(name, value, validator) click to toggle source
# File lib/nutrition_calculator/cached_outputs_with_recalculation.rb, line 111
def validate_input!(name, value, validator)
  success = validator.(value)
  if !success
    raise InvalidInputError, "#{value.inspect} is not a valid input value for '##{name}'."
  end
end