Class: Height::Units::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable, Math
Defined in:
lib/height/units/base.rb

Direct Known Subclasses

Centimeters, Feet, Inches, Meters, Millimeters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Base

Returns a new instance of Base.



16
17
18
# File 'lib/height/units/base.rb', line 16

def initialize(value)
  @value = self.class.round_value(value)
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/height/units/base.rb', line 6

def value
  @value
end

Class Method Details

.round_value(value) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/height/units/base.rb', line 8

def self.round_value(value)
  key = self.name.split('::').last.downcase.to_sym

  precision = ::Height.units_precision[key]

  value.round(precision)
end

Instance Method Details

#*(other) ⇒ Object



35
36
37
38
# File 'lib/height/units/base.rb', line 35

def *(other)
  result = value * other_value(other)
  self.class.new(result)
end

#+(other) ⇒ Object



24
25
26
27
# File 'lib/height/units/base.rb', line 24

def +(other)
  result = value + other_value(other)
  self.class.new(result)
end

#-(other) ⇒ Object



29
30
31
32
33
# File 'lib/height/units/base.rb', line 29

def -(other)
  result = value - other_value(other)

  self.class.new(result)
end

#/(other) ⇒ Object



40
41
42
43
# File 'lib/height/units/base.rb', line 40

def /(other)
  result = value / other_value(other)
  self.class.new(result)
end

#<=>(other) ⇒ Object



20
21
22
# File 'lib/height/units/base.rb', line 20

def <=>(other)
  value <=> other_value(other)
end

#to_sObject



51
52
53
# File 'lib/height/units/base.rb', line 51

def to_s
  "%g" % ("%0.2f" % value)
end

#to_unit(klass) ⇒ Object



45
46
47
48
49
# File 'lib/height/units/base.rb', line 45

def to_unit(klass)
  class_name = klass.name.split('::').last.downcase
  method = "to_#{class_name}"
  send(method)
end