Skip to content

Commit

Permalink
Reconfigured DSL a bit, updated us/si systems
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuga committed Jan 4, 2010
1 parent aa7bd90 commit 01f2ff2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
11 changes: 4 additions & 7 deletions lib/quantity/systems/si.rb
Expand Up @@ -24,7 +24,7 @@
# @see http://physics.nist.gov/cuu/Units/prefixes.html
class Quantity
class Unit
module SI

prefixes = {}
units = {}
aliases = {}
Expand Down Expand Up @@ -68,7 +68,7 @@ module SI

units.each do | unit, dimension |
prefixes.each do | prefix, value |
add_unit dimension, "#{prefix + unit}".to_sym, value, "#{prefix + unit}s".to_sym
add_unit "#{prefix + unit}".to_sym, dimension, value, "#{prefix + unit}s".to_sym
if aliases[unit]
aliases[unit].each do | unit_alias |
add_alias "#{prefix + unit}".to_sym, "#{prefix + unit_alias}".to_sym
Expand All @@ -81,24 +81,21 @@ module SI
add_alias :centimeter, :cm
add_alias :meter, :m
add_alias :nanometer, :nm
add_unit :length, :angstrom, 10 ** -7, :angstroms
add_unit :angstrom, :length, 10 ** -7, :angstroms

add_alias :kilogram, :kg
add_alias :gram, :g
add_alias :milligram, :mg
add_alias :megagram, :tonne, :tonnes


#l = Unit.for(:length)
Quantity::Dimension::Compound.name_compound self.for(:meter).dimension**3, :volume
prefixes.each do | prefix, value |
add_unit :volume, "#{prefix}liter".to_sym, value * 1000, "#{prefix}liters".to_sym
add_unit "#{prefix}liter".to_sym, :volume, value * 1000, "#{prefix}liters".to_sym
(aliases['liter']).each do | unit_alias |
add_alias "#{prefix}liter".to_sym, "#{prefix + unit_alias}".to_sym
end
end
add_alias :liter, :l

end
end
end
22 changes: 11 additions & 11 deletions lib/quantity/systems/us.rb
Expand Up @@ -3,17 +3,17 @@
# fluid pint and a dry pint, pleases see the documentation
# for the quantity/systems/us modules.
class Quantity::Unit
add_unit :length, :foot, 304.8, :ft, :feet
add_unit :width, :inch, 25.4, :in, :inches
add_unit :length, :yard, 914.4, :yd, :yards
add_unit :length, :mile, 1_609_344, :miles
add_unit :foot, :length, 304.8, :ft, :feet
add_unit :inch, :width, 25.4, :in, :inches
add_unit :yard, :length, 914.4, :yd, :yards
add_unit :mile, :length, 1_609_344, :miles

add_unit :mass, :pound, 453592.37, :pounds, :lb, :lbs
add_unit :mass, :ounce, 28349.5231, :ounces, :oz
add_unit :mass, :ton, 907184740, :tons
add_unit :pound, :mass, 453592.37, :pounds, :lb, :lbs
add_unit :ounce, :mass, 28349.5231, :ounces, :oz
add_unit :ton, :mass, 907184740, :tons

#add_unit :fluid_ounce, 29.57, :floz, :ozfl
#add_unit :pint, 473.18, :pint, :pints
#add_unit :quart, 946.35, :qt, :quarts
#add_unit :gallon, 3785.41, :gallons, :gal
add_unit :fluid_ounce, :volume, 29.57, :floz, :ozfl
add_unit :pint, :volume, 473.18, :pint, :pints
add_unit :quart, :volume, 946.35, :qt, :quarts
add_unit :gallon, :volume, 3785.41, :gallons, :gal
end
4 changes: 2 additions & 2 deletions lib/quantity/unit.rb
Expand Up @@ -58,7 +58,7 @@ def self.add_alias(unit,*names)
# @param [Numeric] value
# @param [[String Symbol]] *aliases
def self.add_unit(name,dimension,value,*names)
new_unit = Unit.new({ :name => name,:dimension => dimension,:value => value})
new_unit = Unit.new({ :name => name,:dimension => Quantity::Dimension.for(dimension),:value => value})
names.each do | name |
add_alias new_unit, name
end
Expand Down Expand Up @@ -242,7 +242,7 @@ def self.from_string_form(to)
def initialize(opts)
@units = opts[:units]
@dimension = opts[:dimension]
@value = @dimension.is_base? ? opts[:value] : calculate_value
@value = opts[:value] || calculate_value
if @dimension.nil?
raise ArgumentError, "Adding invalid unit with nil dimension (#{name} - #{dimension})"
end
Expand Down
4 changes: 4 additions & 0 deletions spec/unit.spec
Expand Up @@ -43,10 +43,14 @@ describe Quantity::Unit do
Quantity::Unit.add_unit :gram, @mass, 1000, :grams
Quantity::Unit.add_unit :nanosecond, @time, 10**-6, :nanoseconds
Quantity::Unit.add_unit :picogram, @mass, 10**-9, :picograms
Quantity::Unit.add_unit :mps, @accel, 10**12, :meterspersecond
meters = Quantity::Unit.for :meter
meters.dimension.should == @length
meters.name.should == :meter
meters.value.should == 1000
mps = Quantity::Unit.for :mps
mps.name.should == :mps
mps.value.should == 10**12
end

it "should know its aliases" do
Expand Down

0 comments on commit 01f2ff2

Please sign in to comment.