From 5a65a8659e09700609b60056e90dcbbdcb517576 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 9 Feb 2016 16:04:43 -0500 Subject: [PATCH] Adds ability for concentrations to convert to a scalar denom uom --- lib/unit/concentration.rb | 7 ++++++- test/concentration_test.rb | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/unit/concentration.rb b/lib/unit/concentration.rb index ec69cd5..1388173 100644 --- a/lib/unit/concentration.rb +++ b/lib/unit/concentration.rb @@ -37,7 +37,12 @@ def convert_to(uom) if uoms.size <= 1 raise IncompatibleUnitsError end - Concentration.new(numerator.convert_to(uoms[0]), denominator.convert_to(uoms[1])) + tokens = Lexer.new.tokenize("1 #{uom}") + destination_conc = Parser.new.parse(tokens) + Concentration.new( + numerator.convert_to(destination_conc.numerator.uom).scale(destination_conc.scalar), + denominator.convert_to(destination_conc.denominator.uom) + ) end alias_method :>>, :convert_to diff --git a/test/concentration_test.rb b/test/concentration_test.rb index 04134da..0bd46ec 100644 --- a/test/concentration_test.rb +++ b/test/concentration_test.rb @@ -237,6 +237,12 @@ class ConcentrationTest < Minitest::Test assert_equal 2000, new_con.scalar assert_equal 'mcg/ml', new_con.uom end + + should "convert to unit with scalar in denomatinor" do + new_con = @con >> 'mg/2ml' + assert_equal 1, new_con.scalar + assert_equal 'mg/ml', new_con.uom + end end context "abs" do