Class: Height

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/height.rb,
lib/height/parser.rb,
lib/height/version.rb,
lib/height/units/base.rb,
lib/height/units/feet.rb,
lib/height/parsers/base.rb,
lib/height/units/inches.rb,
lib/height/units/meters.rb,
lib/height/parsers/metric.rb,
lib/height/formatters/base.rb,
lib/height/parsers/imperial.rb,
lib/height/formatters/metric.rb,
lib/height/units/centimeters.rb,
lib/height/units/millimeters.rb,
lib/height/formatters/imperial.rb

Defined Under Namespace

Modules: Formatters, Parsers, Units Classes: Parser

Constant Summary collapse

MILLIMETERS_IN_CENTIMETER =
10
CENTIMETERS_IN_METER =
100
MILLIMETERS_IN_METER =
MILLIMETERS_IN_CENTIMETER * CENTIMETERS_IN_METER
MILLIMETERS_IN_INCH =
25.4
INCHES_IN_FOOT =
12
VERSION =
"0.0.1"

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Height

Returns a new instance of Height.



39
40
41
42
43
44
45
# File 'lib/height.rb', line 39

def initialize(input)
  if input.respond_to? :to_millimeters
    @millimeters = input.to_millimeters
  else
    @millimeters = Parser.new(input).millimeters
  end
end

Class Attribute Details

.system_of_unitsObject

Returns the value of attribute system_of_units.



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

def system_of_units
  @system_of_units
end

.units_precisionObject

Returns the value of attribute units_precision.



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

def units_precision
  @units_precision
end

Instance Method Details

#+(other) ⇒ Object



67
68
69
70
# File 'lib/height.rb', line 67

def +(other)
  centimeters = (millimeters + other.millimeters).to_centimeters
  self.class.new(centimeters)
end

#-(other) ⇒ Object



72
73
74
75
# File 'lib/height.rb', line 72

def -(other)
  centimeters = (millimeters - other.millimeters).to_centimeters
  self.class.new(centimeters)
end

#<=>(other) ⇒ Object



77
78
79
# File 'lib/height.rb', line 77

def <=>(other)
  millimeters <=> other.millimeters
end

#centimetersObject



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

def centimeters
  millimeters.to_centimeters
end

#feetObject



63
64
65
# File 'lib/height.rb', line 63

def feet
  millimeters.to_feet
end

#inchesObject



59
60
61
# File 'lib/height.rb', line 59

def inches
  millimeters.to_inches
end

#metersObject



55
56
57
# File 'lib/height.rb', line 55

def meters
  millimeters.to_meters
end

#millimetersObject



47
48
49
# File 'lib/height.rb', line 47

def millimeters
  @millimeters
end

#to_s(format = :default, system_of_units = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/height.rb', line 81

def to_s(format = :default, system_of_units = nil)
  system_of_units ||= self.class.system_of_units

  case system_of_units
  when :metric
    Formatters::Metric.new(millimeters).format(format)
  when :imperial
    Formatters::Imperial.new(millimeters).format(format)
  else
    raise ::ArgumentError.new('Invalid system of units provided, use either :metric or :imperial')
  end
end