Class: Comparison::Comparator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/comparison/comparator.rb

Overview

The Comparator object compares two numbers to each other and exposes the raw and percentage differences.

Direct Known Subclasses

Presenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, other) ⇒ Comparator

Instantiates a new Comparator to compare two numbers, value and other.

Both numbers will be converted to instances of ‘BigDecimal`.



19
20
21
22
# File 'lib/comparison/comparator.rb', line 19

def initialize(value, other)
  @value = value.to_d
  @other = other.to_d
end

Instance Attribute Details

#otherObject (readonly)

Returns the value of attribute other.



13
14
15
# File 'lib/comparison/comparator.rb', line 13

def other
  @other
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/comparison/comparator.rb', line 13

def value
  @value
end

Instance Method Details

#absoluteObject



33
34
35
36
# File 'lib/comparison/comparator.rb', line 33

def absolute
  Kernel.warn "DEPRECATION WARNING: use #difference instead of #absolute (called from #{caller(1..1).first})"
  difference
end

#changeObject

Returns the relative change of @value from @other.



40
41
42
# File 'lib/comparison/comparator.rb', line 40

def change
  difference / other.abs
end

#differenceObject

Returns the difference between @value and @other.



29
30
31
# File 'lib/comparison/comparator.rb', line 29

def difference
  value - other
end

#mObject



55
56
57
58
# File 'lib/comparison/comparator.rb', line 55

def m
  Kernel.warn "DEPRECATION WARNING: use #value instead of #m (called from #{caller(1..1).first})"
  value
end

#nObject



60
61
62
63
# File 'lib/comparison/comparator.rb', line 60

def n
  Kernel.warn "DEPRECATION WARNING: use #other instead of #n (called from #{caller(1..1).first})"
  other
end

#percentageObject

Returns the relative change of @value from @other as a percentage.



46
47
48
# File 'lib/comparison/comparator.rb', line 46

def percentage
  change * 100
end

#relativeObject



50
51
52
53
# File 'lib/comparison/comparator.rb', line 50

def relative
  Kernel.warn "DEPRECATION WARNING: use #percentage instead of #relative (called from #{caller(1..1).first})"
  change
end