Initialization by hash #39

Closed
wants to merge 1 commit into
from
@@ -162,6 +162,14 @@ def self.definition(_unit)
return @@definitions[unit]
end
+ # return the unit definition for a unit, searching aliases too
+ # throw error if not found
+ def self.definition_or_alias(_unit)
+ return if !_unit
+ self.definition(_unit) || self.definition(@@UNIT_MAP[_unit]) || raise(ArgumentError, "'#{_unit}' Unit not recognized")
+ end
+
+
# return a list of all defined units
# @return [Array]
def self.definitions
@@ -343,6 +351,19 @@ def initialize(*options)
@numerator = options[0][:numerator] || UNITY_ARRAY
@denominator = options[0][:denominator] || UNITY_ARRAY
@signature = options[0][:signature]
+
+ # numerator needs to be an array, 'pounds' => ['pounds']
+ @numerator = [@numerator] unless @numerator.is_a?(Array)
+
+ # numerator needs to be based on a definition
+ @numerator = @numerator.collect{|n| Unit.definition_or_alias(n).name}.flatten.compact
+
+ # denominator needs to be an array, 'pounds' => ['pounds']
+ @denominator = [@denominator] unless @denominator.kind_of?(Array)
+
+ # denominator needs to be based on a definition
+ @denominator = @denominator.collect{|n| Unit.definition_or_alias(n).name}.flatten.compact
+
when Array
initialize(*options[0])
return
@@ -508,6 +508,44 @@
its(:units) {should == "m/s"}
end
+ #hash
+ describe Unit(:scalar => 1.345, :numerator => 'oz') do
+ its(:kind) {should == :mass}
+ its(:units) {should == 'oz'}
+ its(:scalar) {should == 1.345}
+ its(:numerator) {should == ["<ounce>"]}
+ end
+
+ # hash with speed
+ describe Unit(:scalar => 62.5, :numerator => 'feet', :denominator => 's') do
+ its(:kind) {should == :speed}
+ its(:units) {should == 'ft/s'}
+ its(:scalar) {should == 62.5}
+ its(:numerator) {should == ["<foot>"]}
+ its(:denominator) {should == ["<second>"]}
+ end
+
+ # hash with array numerator
+ describe Unit(:scalar => 199.9, :numerator => ['inches', 'inch'], :denominator => 'pounds') do
+ its(:kind) {should == nil}
+ its(:units) {should == 'in^2/lbs'}
+ its(:scalar) {should == 199.9}
+ its(:numerator) {should == ["<inch>", "<inch>"] }
+ its(:denominator) {should == ["<pound>"]}
+ end
+
+ # hash - handle zero-decimal floats properly
+ describe Unit(:scalar => 97.00000, :numerator => 'kelvin') do
+ its(:kind) {should == :temperature}
+ its(:units) {should == 'degK'}
+ its(:scalar) {should == 97}
+ its(:scalar) {should be_a(Float)}
+ its(:numerator) {should == ["<kelvin>"]}
+ end
+
+
+ # tbd: test setting signature manually in a hash?
+
end
describe "Unit handles attempts to create bad units" do
@@ -553,6 +591,8 @@
specify "no undefined units" do
expect {Unit("1 mFoo")}.to raise_error(ArgumentError,"'1 mFoo' Unit not recognized")
+ expect {Unit(:scalar => 2.444, :numerator => "mFoo")}.to raise_error(ArgumentError,"'mFoo' Unit not recognized")
+ expect {Unit(:scalar => 2.444, :numerator => "m", :denominator => "mFoo")}.to raise_error(ArgumentError,"'mFoo' Unit not recognized")
end
specify "no units with powers greater than 19" do