Class: Convertable::Units::ComposedUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/convertable/units.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units) ⇒ ComposedUnit

Returns a new instance of ComposedUnit.



85
86
87
# File 'lib/convertable/units.rb', line 85

def initialize(units)
  @multiplying_unit, *@dividing_units = units.split("/").map { |unit| Convertable::Units.find(unit) }
end

Instance Attribute Details

#dividing_unitsObject (readonly)

Returns the value of attribute dividing_units.



83
84
85
# File 'lib/convertable/units.rb', line 83

def dividing_units
  @dividing_units
end

#multiplying_unitObject (readonly)

Returns the value of attribute multiplying_unit.



83
84
85
# File 'lib/convertable/units.rb', line 83

def multiplying_unit
  @multiplying_unit
end

Instance Method Details

#==(other_composed_unit) ⇒ Object



95
96
97
98
# File 'lib/convertable/units.rb', line 95

def ==(other_composed_unit)
  multiplying_unit == other_composed_unit.multiplying_unit &&
    dividing_units == other_composed_unit.dividing_units
end

#convert_to(magnitude, new_composed_unit) ⇒ Object



89
90
91
92
93
# File 'lib/convertable/units.rb', line 89

def convert_to(magnitude, new_composed_unit)
  raise UnsupportedConversion unless new_composed_unit.is_a?(ComposedUnit)

  magnitude * conversion_factor_to(new_composed_unit)
end

#to_jsonObject



100
101
102
# File 'lib/convertable/units.rb', line 100

def to_json
  name
end