Class: LazilyEvaluated

Inherits:
Object
  • Object
show all
Defined in:
lib/performance_promise/lazily_evaluated.rb

Instance Method Summary collapse

Constructor Details

#initialize(c) ⇒ LazilyEvaluated

Returns a new instance of LazilyEvaluated.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#*(other) ⇒ Object



27
28
29
# File 'lib/performance_promise/lazily_evaluated.rb', line 27

def *(other)
  return LazilyEvaluated.new([self, other, '*'])
end

#+(other) ⇒ Object



19
20
21
# File 'lib/performance_promise/lazily_evaluated.rb', line 19

def +(other)
  return LazilyEvaluated.new([self, other, '+'])
end

#-(other) ⇒ Object



23
24
25
# File 'lib/performance_promise/lazily_evaluated.rb', line 23

def -(other)
  return LazilyEvaluated.new([self, other, '-'])
end

#/(other) ⇒ Object



31
32
33
# File 'lib/performance_promise/lazily_evaluated.rb', line 31

def /(other)
  return LazilyEvaluated.new([self, other, '/'])
end

#evaluateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#queriesObject



57
58
59
60
# File 'lib/performance_promise/lazily_evaluated.rb', line 57

def queries
  # syntactic sugar
  self
end