class LazilyEvaluated

Public Class Methods

new(c) click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 5
def initialize(c)
  if c.is_a?(Fixnum)
    @operand1 = c
    @operator = 'constant'
  elsif c.is_a?(Class)
    @operand1 = c
    @operator = 'model'
  elsif c.is_a?(Array)
    @operand1, @operand2, @operator = c
  else
    raise
  end
end

Public Instance Methods

*(other) click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 27
def *(other)
  return LazilyEvaluated.new([self, other, '*'])
end
+(other) click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 19
def +(other)
  return LazilyEvaluated.new([self, other, '+'])
end
-(other) click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 23
def -(other)
  return LazilyEvaluated.new([self, other, '-'])
end
/(other) click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 31
def /(other)
  return LazilyEvaluated.new([self, other, '/'])
end
evaluate() click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 35
def evaluate
  # don't do anything in prod-like environments
  return 0 unless PerformancePromise.configuration.allowed_environments.include?(Rails.env)

  case @operator
  when 'constant'
    return @operand1
  when 'model'
    return @operand1.count
  when '+'
    return @operand1.evaluate + @operand2.evaluate
  when '-'
    return @operand1.evaluate - @operand2.evaluate
  when '*'
    return @operand1.evaluate * @operand2.evaluate
  when '/'
    return @operand1.evaluate / @operand2.evaluate
  else
    raise
  end
end
queries() click to toggle source
# File lib/performance_promise/lazily_evaluated.rb, line 57
def queries
  # syntactic sugar
  self
end