lib/unit_measurements/measurement.rb in unit_measurements-4.12.0 vs lib/unit_measurements/measurement.rb in unit_measurements-5.0.0
- old
+ new
@@ -99,20 +99,29 @@
#
# @example
# UnitMeasurements::Length.new(1, "m").convert_to("cm")
# => 100.0 cm
#
- # @param [String|Symbol] target_unit The target unit for conversion.
+ # UnitMeasurements::Length.new(1, "cm").convert_to("primitive")
+ # => 0.01 m
#
+ # @param [String|Symbol] target_unit
+ # The target unit for conversion. Specifing +primitive+ will convert the
+ # measurement to a primitive unit of the unit group.
+ #
# @return [Measurement]
# A new +Measurement+ instance with the converted +quantity+ and
# +target unit+.
#
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
# @since 1.0.0
def convert_to(target_unit)
- target_unit = unit_from_unit_or_name!(target_unit)
+ target_unit = if target_unit.to_s.eql?("primitive")
+ self.class.unit_group.primitive
+ else
+ unit_from_unit_or_name!(target_unit)
+ end
return self if target_unit == unit
conversion_factor = (unit.conversion_factor / target_unit.conversion_factor)
@@ -186,10 +195,10 @@
extend Forwardable
# Methods delegated from the unit group.
def_delegators :unit_group, :primitive, :units, :unit_names, :unit_with_name_and_aliases,
:unit_names_with_aliases, :unit_for, :unit_for!, :defined?,
- :unit_or_alias?, :[]
+ :unit_or_alias?, :[], :units_for, :units_for!
# Parses an input string and returns a +Measurement+ instance depending on
# the input string. This method first normalizes the +input+ internally,
# using the +Normalizer+ before parsing it using the +Parser+.
#