lib/unit_measurements/unit.rb in unit_measurements-3.8.0 vs lib/unit_measurements/unit.rb in unit_measurements-4.0.0
- old
+ new
@@ -4,24 +4,26 @@
require "set"
module UnitMeasurements
class Unit
- attr_reader :name, :value, :aliases, :unit_group
+ attr_reader :name, :value, :aliases, :system, :unit_group
- def initialize(name, value:, aliases:, unit_group: nil)
+ def initialize(name, value:, aliases:, system:, unit_group: nil)
@name = name.to_s.freeze
@value = value
- @aliases = Set.new(aliases.sort.map(&:to_s).map(&:freeze)).freeze
+ @aliases = Set.new(aliases.map(&:to_s).sort.map(&:freeze)).freeze
+ @system = system
@unit_group = unit_group
end
- def with(name: nil, value: nil, aliases: nil, unit_group: nil)
+ def with(name: nil, value: nil, aliases: nil, system: nil, unit_group: nil)
self.class.new(
(name || self.name),
value: (value || self.value),
aliases: (aliases || self.aliases),
+ system: (system || self.system),
unit_group: (unit_group || self.unit_group)
)
end
def names
@@ -40,15 +42,16 @@
def conversion_factor
return value if value.is_a?(Numeric)
measurement_value, measurement_unit = parse_value(value)
conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor
+
conversion_factor * measurement_value
end
private
- SI_PREFIXES = [
+ SI_DECIMAL_PREFIXES = [
["q", %w(quecto), 1e-30],
["r", %w(ronto), 1e-27],
["y", %w(yocto), 1e-24],
["z", %w(zepto), 1e-21],
["a", %w(atto), 1e-18],