class PackList::Weight

Constants

WEIGHT_CONVERSIONS
ZERO

Attributes

quantity[R]
units[R]

Public Class Methods

new(quantity, units) click to toggle source
# File lib/packlist/model.rb, line 16
def initialize(quantity, units)
  raise ArgumentError, "Invalid units: #{units}" unless WEIGHT_CONVERSIONS.key?(units)
  @quantity, @units = quantity, units
end

Public Instance Methods

*(x) click to toggle source
# File lib/packlist/model.rb, line 41
def *(x)
  Weight.new x * @quantity, @units
end
+(w) click to toggle source
# File lib/packlist/model.rb, line 27
def +(w)
  # avoid changing units when either weight is zero
  case
  when w.quantity == 0
    self
  when self.quantity == 0
    w
  when w.units == @units
    Weight.new w.quantity + @quantity, @units
  else
    self + w.to_units(@units)
  end
end
in_units(new_units) click to toggle source

private

# File lib/packlist/model.rb, line 48
def in_units(new_units)
  quantity * WEIGHT_CONVERSIONS[units] / WEIGHT_CONVERSIONS[new_units]
end
to_s() click to toggle source
# File lib/packlist/model.rb, line 56
def to_s
  "#{'%.1f' % quantity} #{units}"
end
to_units(new_units) click to toggle source
# File lib/packlist/model.rb, line 52
def to_units(new_units)
  Weight.new in_units(new_units), new_units
end