class Shioconv::Unit

Constants

CONVERTABLE_UNITS
UNIT_TYPES

Attributes

name[R]
type[R]
value[R]

Public Class Methods

convertable?(unit) click to toggle source
# File lib/shioconv/unit.rb, line 34
def self.convertable?(unit)
  CONVERTABLE_UNITS.include?(unit)
end
find_by(unit_name) click to toggle source
# File lib/shioconv/unit.rb, line 24
def self.find_by(unit_name)
  UNIT_TYPES.each do |unit_type, units|
    if units.has_key?(unit_name)
      return self.new(type: unit_type, name: unit_name, value: units[unit_name])
    end
  end

  raise ArgumentError.new("unit [#{unit_name}] does not found.") unless @name
end
list() click to toggle source
# File lib/shioconv/unit.rb, line 38
def self.list
  UNIT_TYPES.map do |type, units|
    (base_unit, _) = units.dup.shift
    "#{type}: #{base_unit}, " << units.map { |unit, value| "#{unit}(#{value}#{base_unit})" }.join(', ')
  end
end
new(type: type(), name: name(), value: value()) click to toggle source
# File lib/shioconv/unit.rb, line 45
def initialize(type: type(), name: name(), value: value())
  @type  = type
  @name  = name
  @value = value
end

Public Instance Methods

convert(condiment, quantity, dst_unit) click to toggle source
# File lib/shioconv/unit.rb, line 51
def convert(condiment, quantity, dst_unit)
  return quantity if name == dst_unit
  dst_unit = self.class.find_by(dst_unit)

  current_vavlue = quantity * value / dst_unit.value
  if type == :weight && dst_unit.type == :volume
    current_vavlue /= condiment.specific_gravity
  elsif type == :volume && dst_unit.type == :weight
    current_vavlue *= condiment.specific_gravity
  end

  current_vavlue
end