simple-measures 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.rdoc +30 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/simple-measures.rb +30 -0
- data/lib/simple-measures/core_ext/numeric.rb +8 -0
- data/lib/simple-measures/core_ext/symbol.rb +8 -0
- data/lib/simple-measures/registry.rb +65 -0
- data/lib/simple-measures/registry/unit_entry.rb +1 -0
- data/lib/simple-measures/systems/imperial.rb +31 -0
- data/lib/simple-measures/systems/si.rb +29 -0
- data/lib/simple-measures/unit.rb +112 -0
- data/spec/simple-measures/registry_spec.rb +68 -0
- data/spec/simple-measures/unit_spec.rb +154 -0
- data/spec/spec_helper.rb +2 -0
- metadata +75 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Colin Harris
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
= Simple Measures
|
2
|
+
|
3
|
+
all the units you need to describe and convert recipes.
|
4
|
+
|
5
|
+
==== volume
|
6
|
+
gallons, quarts, pints, cups, floz
|
7
|
+
liters
|
8
|
+
|
9
|
+
==== weight
|
10
|
+
pounds, ounces
|
11
|
+
grams
|
12
|
+
|
13
|
+
== Examples
|
14
|
+
|
15
|
+
require 'simple-measures'
|
16
|
+
|
17
|
+
SimpleMeasures.use_imperial_units!
|
18
|
+
SimpleMeasures.use_si_units!
|
19
|
+
SimpleMeasures.use_numeric_sugar!
|
20
|
+
|
21
|
+
( 3.pounds + 2.oz ).to_grams
|
22
|
+
=> #<SimpleMeasures::Unit:0x00000101108e20 @value=1417.4761562, @unit=:grams>
|
23
|
+
|
24
|
+
1.liter.to_quarts
|
25
|
+
=> #<SimpleMeasures::Unit:0x00000101115568 @value=1.05668820943259, @unit=:quarts>
|
26
|
+
|
27
|
+
|
28
|
+
== Copyright
|
29
|
+
|
30
|
+
Copyright (c) 2010 Colin Harris. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
task :default => :spec
|
4
|
+
|
5
|
+
Spec::Rake::SpecTask.new do |t|
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.warning = false
|
8
|
+
t.rcov = false
|
9
|
+
t.spec_opts = ["--colour"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
Rake::RDocTask.new do |rdoc|
|
14
|
+
if File.exist?('VERSION')
|
15
|
+
version = File.read('VERSION')
|
16
|
+
else
|
17
|
+
version = ""
|
18
|
+
end
|
19
|
+
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = "Measurement #{version}"
|
22
|
+
rdoc.rdoc_files.include('README*')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'rake/packagetask'
|
27
|
+
require 'rake/gempackagetask'
|
28
|
+
|
29
|
+
### Task: gem
|
30
|
+
gemspec = Gem::Specification.new do |gem|
|
31
|
+
gem.name = "simple-measures"
|
32
|
+
gem.version = File.read('VERSION')
|
33
|
+
|
34
|
+
gem.summary = "SimpleMeasures - simple unit conversions for everyday things"
|
35
|
+
gem.description = "SimpleMeasures is an easy and extensible measurement conversion library"
|
36
|
+
|
37
|
+
gem.authors = "Colin Harris"
|
38
|
+
gem.email = "qzzzq1@gmail.com"
|
39
|
+
gem.homepage = "http://github.com/aberant/simple-measures"
|
40
|
+
|
41
|
+
gem.has_rdoc = true
|
42
|
+
|
43
|
+
gem.files = FileList['Rakefile', 'README.rdoc', 'VERSION', 'LICENSE', 'lib/**/*'].to_a
|
44
|
+
gem.test_files = FileList['spec/**/*.rb']
|
45
|
+
end
|
46
|
+
|
47
|
+
Rake::GemPackageTask.new( gemspec ) do |task|
|
48
|
+
task.gem_spec = gemspec
|
49
|
+
task.need_tar = false
|
50
|
+
task.need_tar_gz = true
|
51
|
+
task.need_tar_bz2 = true
|
52
|
+
task.need_zip = true
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Measurement, all the units you need to describe and convert recipes.
|
2
|
+
#
|
3
|
+
# == Authors
|
4
|
+
#
|
5
|
+
# * Colin Harris (@aberant on twitter)
|
6
|
+
#
|
7
|
+
# == Copyright
|
8
|
+
#
|
9
|
+
# Copyright (c) 2010 Colin Harris
|
10
|
+
#
|
11
|
+
# This code released under the terms of the MIT license.
|
12
|
+
|
13
|
+
require 'simple-measures/registry'
|
14
|
+
require 'simple-measures/registry/unit_entry'
|
15
|
+
require 'simple-measures/unit'
|
16
|
+
require 'simple-measures/core_ext/symbol'
|
17
|
+
|
18
|
+
module SimpleMeasures
|
19
|
+
VERSION = File.read( File.join( File.dirname( __FILE__ ), "..", "VERSION"))
|
20
|
+
|
21
|
+
class InvalidUnitError < StandardError ; end
|
22
|
+
class InvalidAliasError < StandardError ; end
|
23
|
+
class ConversionError < StandardError ; end
|
24
|
+
|
25
|
+
def self.use_si_units!() require 'simple-measures/systems/si'; end
|
26
|
+
def self.use_imperial_units!() require 'simple-measures/systems/imperial'; end
|
27
|
+
def self.use_numeric_sugar!() require 'simple-measures/core_ext/numeric'; end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#= SimpleMeasures::Registry
|
2
|
+
# the central location where all units and aliases are stored
|
3
|
+
module SimpleMeasures
|
4
|
+
class Registry
|
5
|
+
@units = {}
|
6
|
+
@aliases = {}
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_reader :units
|
10
|
+
attr_reader :aliases
|
11
|
+
|
12
|
+
def add_unit( unit, value, type )
|
13
|
+
@units[unit] = UnitEntry.new( unit, value, type )
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_alias( unit, unit_alias )
|
17
|
+
raise InvalidAliasError unless valid_unit?( unit )
|
18
|
+
@aliases[unit_alias] = unit
|
19
|
+
end
|
20
|
+
|
21
|
+
def unit_conversion_value( name )
|
22
|
+
unit( name ).value
|
23
|
+
end
|
24
|
+
|
25
|
+
def unit_type( name )
|
26
|
+
unit( name ).type
|
27
|
+
end
|
28
|
+
|
29
|
+
def normalized_unit_name( name )
|
30
|
+
aliased_name = @aliases[name]
|
31
|
+
|
32
|
+
return aliased_name if aliased_name
|
33
|
+
return name if @units.keys.include?( name )
|
34
|
+
|
35
|
+
raise InvalidUnitError
|
36
|
+
end
|
37
|
+
|
38
|
+
def compatible_types?( type_a, type_b )
|
39
|
+
unit_type( type_a ) == unit_type( type_b )
|
40
|
+
end
|
41
|
+
|
42
|
+
def clear!
|
43
|
+
@units = {}
|
44
|
+
@alises = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid_unit?( unit )
|
48
|
+
@units.keys.include?( unit ) || @aliases.keys.include?( unit )
|
49
|
+
end
|
50
|
+
|
51
|
+
def smallest_unit_for_type( type )
|
52
|
+
units_filtered_by_type = units.find_all{ |unit| unit[1].type == type }
|
53
|
+
result = units_filtered_by_type.sort{ |a, b| a[1].value <=> b[1].value }
|
54
|
+
|
55
|
+
# only interested in key, not associated struct
|
56
|
+
result[0][0]
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def unit( name )
|
61
|
+
@units[normalized_unit_name( name )]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
UnitEntry = Struct.new( :unit, :value, :type )
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# ==========
|
2
|
+
# = Weight =
|
3
|
+
# ==========
|
4
|
+
SimpleMeasures::Registry.add_unit( :pound, 453_592.37, :weight )
|
5
|
+
SimpleMeasures::Registry.add_unit( :ounce, 28_349.5231, :weight )
|
6
|
+
|
7
|
+
|
8
|
+
SimpleMeasures::Registry.add_alias( :pound, :pounds )
|
9
|
+
SimpleMeasures::Registry.add_alias( :pound, :lb )
|
10
|
+
SimpleMeasures::Registry.add_alias( :pound, :lbs )
|
11
|
+
|
12
|
+
SimpleMeasures::Registry.add_alias( :ounce, :ounces )
|
13
|
+
SimpleMeasures::Registry.add_alias( :ounce, :oz )
|
14
|
+
|
15
|
+
# ==========
|
16
|
+
# = Volume =
|
17
|
+
# ==========
|
18
|
+
SimpleMeasures::Registry.add_unit( :fluid_ounce, 29.5735296, :volume )
|
19
|
+
SimpleMeasures::Registry.add_unit( :cup, 236.588237, :volume )
|
20
|
+
SimpleMeasures::Registry.add_unit( :pint, 473.176473, :volume )
|
21
|
+
SimpleMeasures::Registry.add_unit( :quart, 946.352946, :volume )
|
22
|
+
SimpleMeasures::Registry.add_unit( :gallon, 3_785.41178, :volume )
|
23
|
+
|
24
|
+
SimpleMeasures::Registry.add_alias( :fluid_ounce, :floz )
|
25
|
+
SimpleMeasures::Registry.add_alias( :fluid_ounce, :ozfl )
|
26
|
+
SimpleMeasures::Registry.add_alias( :cup, :cups )
|
27
|
+
SimpleMeasures::Registry.add_alias( :pint, :pints )
|
28
|
+
SimpleMeasures::Registry.add_alias( :quart, :quarts )
|
29
|
+
SimpleMeasures::Registry.add_alias( :quarts, :qt )
|
30
|
+
SimpleMeasures::Registry.add_alias( :gallon, :gallons )
|
31
|
+
SimpleMeasures::Registry.add_alias( :gallon, :gal )
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# ==========
|
2
|
+
# = Weight =
|
3
|
+
# ==========
|
4
|
+
SimpleMeasures::Registry.add_unit( :milligram, 1, :weight )
|
5
|
+
SimpleMeasures::Registry.add_unit( :gram, 10 ** 3, :weight )
|
6
|
+
SimpleMeasures::Registry.add_unit( :kilogram, 10 ** 6, :weight )
|
7
|
+
|
8
|
+
|
9
|
+
SimpleMeasures::Registry.add_alias( :milligram, :milligrams )
|
10
|
+
SimpleMeasures::Registry.add_alias( :milligram, :mg )
|
11
|
+
|
12
|
+
SimpleMeasures::Registry.add_alias( :gram, :grams )
|
13
|
+
SimpleMeasures::Registry.add_alias( :gram, :g )
|
14
|
+
|
15
|
+
SimpleMeasures::Registry.add_alias( :kilogram, :kilograms )
|
16
|
+
SimpleMeasures::Registry.add_alias( :kilogram, :kg )
|
17
|
+
|
18
|
+
# ==========
|
19
|
+
# = Volume =
|
20
|
+
# ==========
|
21
|
+
SimpleMeasures::Registry.add_unit( :milliliter, 1, :volume )
|
22
|
+
SimpleMeasures::Registry.add_unit( :liter, 10 ** 3, :volume )
|
23
|
+
|
24
|
+
|
25
|
+
SimpleMeasures::Registry.add_alias( :milliliter, :milliliters )
|
26
|
+
SimpleMeasures::Registry.add_alias( :milliliter, :ml )
|
27
|
+
|
28
|
+
SimpleMeasures::Registry.add_alias( :liter, :liters )
|
29
|
+
SimpleMeasures::Registry.add_alias( :liter, :l )
|
@@ -0,0 +1,112 @@
|
|
1
|
+
module SimpleMeasures
|
2
|
+
class Unit
|
3
|
+
include Comparable
|
4
|
+
|
5
|
+
attr_reader :unit, :value
|
6
|
+
|
7
|
+
def initialize( value, unit )
|
8
|
+
raise InvalidUnitError, "Invalid Unit" unless Registry.valid_unit?( unit )
|
9
|
+
@value = value
|
10
|
+
@unit = unit
|
11
|
+
end
|
12
|
+
|
13
|
+
def <=>( other )
|
14
|
+
raise ArgumentError, "Cannot compare #{unit} to #{other.unit}" unless Registry.compatible_types?( unit, other.unit)
|
15
|
+
# we have to make some compromises here
|
16
|
+
# sometimes floats appear equal, but a bagillionth decimal place later
|
17
|
+
# there is some mystery number that screws this up. thus we chop it off after the thousandths place.
|
18
|
+
|
19
|
+
# please enquire about the "enterprise" license which will give you precision to the ten thousandths place
|
20
|
+
other_value = ( other.convert_to( smallest_common_unit ).value * 1000 ).round
|
21
|
+
self_value = ( convert_to( smallest_common_unit ).value * 1000 ).round
|
22
|
+
|
23
|
+
other_value <=> self_value
|
24
|
+
end
|
25
|
+
|
26
|
+
def *( multiplier )
|
27
|
+
case multiplier
|
28
|
+
when Numeric
|
29
|
+
Unit.new( @value * multiplier, @unit )
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Only able to acecpt Numeric types for multiplication"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def /( divisor )
|
36
|
+
case divisor
|
37
|
+
when Numeric
|
38
|
+
Unit.new( @value / divisor, @unit )
|
39
|
+
else
|
40
|
+
raise ArgumentError, "Only able to acecpt Numeric types for division"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def +( other )
|
45
|
+
raise ArgumentError, "Only able to add Measurement::Unit objects" unless other.class == Unit
|
46
|
+
raise ArgumentError, "Cannot add #{unit} to #{other.unit}" unless Registry.compatible_types?( unit, other.unit )
|
47
|
+
|
48
|
+
converted_self = convert_to( smallest_common_unit ).value
|
49
|
+
converted_other = other.convert_to( smallest_common_unit ).value
|
50
|
+
|
51
|
+
Unit.new( converted_self + converted_other, smallest_common_unit )
|
52
|
+
end
|
53
|
+
|
54
|
+
def -( other )
|
55
|
+
raise ArgumentError, "Only able to subtract Measurement::Unit objects" unless other.class == Unit
|
56
|
+
raise ArgumentError, "Cannot add #{unit} to #{other.unit}" unless Registry.compatible_types?( unit, other.unit )
|
57
|
+
|
58
|
+
converted_self = convert_to( smallest_common_unit ).value
|
59
|
+
converted_other = other.convert_to( smallest_common_unit ).value
|
60
|
+
|
61
|
+
Unit.new( converted_self - converted_other, smallest_common_unit )
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
def coerce( other )
|
66
|
+
return self, other
|
67
|
+
end
|
68
|
+
|
69
|
+
def convert_to( new_unit )
|
70
|
+
raise ConversionError, "Cannot convert #{unit} to #{new_unit}" unless Registry.compatible_types?( unit, new_unit )
|
71
|
+
|
72
|
+
old_unit = @unit
|
73
|
+
|
74
|
+
new_conversion = Registry.unit_conversion_value( new_unit )
|
75
|
+
old_conversion = Registry.unit_conversion_value( old_unit )
|
76
|
+
|
77
|
+
old_base = @value * old_conversion.to_f
|
78
|
+
new_value = old_base / new_conversion.to_f
|
79
|
+
|
80
|
+
Unit.new( new_value, new_unit )
|
81
|
+
end
|
82
|
+
|
83
|
+
def method_missing( meth, *args )
|
84
|
+
super unless meth.match( /^to_/ )
|
85
|
+
|
86
|
+
new_unit = meth.to_s.gsub( 'to_', '' ).to_sym
|
87
|
+
super unless Registry.valid_unit?( new_unit )
|
88
|
+
|
89
|
+
convert_to( new_unit )
|
90
|
+
end
|
91
|
+
|
92
|
+
def hash
|
93
|
+
unit.hash ^
|
94
|
+
value.hash
|
95
|
+
end
|
96
|
+
|
97
|
+
def eql?( other )
|
98
|
+
self.class.equal?( other.class ) &&
|
99
|
+
unit == other.unit &&
|
100
|
+
value == other.value
|
101
|
+
end
|
102
|
+
|
103
|
+
def to_s
|
104
|
+
"#{value} #{unit}"
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
def smallest_common_unit
|
109
|
+
Registry.smallest_unit_for_type( Registry.unit_type( unit) )
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__) , '..', 'spec_helper' )
|
2
|
+
|
3
|
+
describe SimpleMeasures::Registry do
|
4
|
+
before :each do
|
5
|
+
SimpleMeasures::Registry.clear!
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should be able to add units" do
|
9
|
+
SimpleMeasures::Registry.add_unit( :milligram, 1, :weight )
|
10
|
+
SimpleMeasures::Registry.units.should have_key( :milligram )
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be able to add an alias" do
|
14
|
+
SimpleMeasures::Registry.add_unit( :milligram, 1, :weight )
|
15
|
+
SimpleMeasures::Registry.add_alias( :milligram, :milligrams )
|
16
|
+
|
17
|
+
SimpleMeasures::Registry.aliases.should have_key( :milligrams )
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not be able to add an alias if there is no coresponding unit" do
|
21
|
+
lambda{
|
22
|
+
SimpleMeasures::Registry.add_alias( :milligram, :milligrams )
|
23
|
+
}.should raise_error( SimpleMeasures::InvalidAliasError )
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return the normalized unit name for a given alias" do
|
27
|
+
SimpleMeasures::Registry.add_unit( :milligram, 1, :weight )
|
28
|
+
SimpleMeasures::Registry.add_alias( :milligram, :bob )
|
29
|
+
|
30
|
+
|
31
|
+
SimpleMeasures::Registry.normalized_unit_name( :bob ).should == :milligram
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return the unit conversion value" do
|
35
|
+
SimpleMeasures::Registry.add_unit( :gram, 1000, :weight )
|
36
|
+
|
37
|
+
SimpleMeasures::Registry.unit_conversion_value( :gram ).should == 1000
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return the unit conversion value for an alias" do
|
41
|
+
SimpleMeasures::Registry.add_unit( :gram, 1000, :weight )
|
42
|
+
SimpleMeasures::Registry.add_alias( :gram, :grams )
|
43
|
+
|
44
|
+
SimpleMeasures::Registry.unit_conversion_value( :grams ).should == 1000
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return the unit type" do
|
48
|
+
SimpleMeasures::Registry.add_unit( :gram, 1000, :weight )
|
49
|
+
|
50
|
+
SimpleMeasures::Registry.unit_type( :gram ).should == :weight
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return the unit type for an alias" do
|
54
|
+
SimpleMeasures::Registry.add_unit( :gram, 1000, :weight )
|
55
|
+
SimpleMeasures::Registry.add_alias( :gram, :grams )
|
56
|
+
|
57
|
+
SimpleMeasures::Registry.unit_type( :grams ).should == :weight
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should be able to return the smallest unit for a type" do
|
61
|
+
SimpleMeasures::Registry.add_unit( :gram, 1000, :weight )
|
62
|
+
SimpleMeasures::Registry.add_unit( :milligram, 1, :weight )
|
63
|
+
SimpleMeasures::Registry.add_unit :pound, 453_592.37, :weight
|
64
|
+
SimpleMeasures::Registry.add_unit( :fluid_ounces, 1, :volume )
|
65
|
+
|
66
|
+
SimpleMeasures::Registry.smallest_unit_for_type( :weight ).should == :milligram
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__) , '..', 'spec_helper' )
|
2
|
+
|
3
|
+
describe SimpleMeasures::Unit do
|
4
|
+
before :each do
|
5
|
+
SimpleMeasures::Registry.clear!
|
6
|
+
SimpleMeasures::Registry.add_unit :gram, 1000, :weight
|
7
|
+
SimpleMeasures::Registry.add_alias :gram, :grams
|
8
|
+
|
9
|
+
SimpleMeasures::Registry.add_unit :milligram, 1, :weight
|
10
|
+
SimpleMeasures::Registry.add_alias :milligram, :milligrams
|
11
|
+
|
12
|
+
SimpleMeasures::Registry.add_unit :pound, 453_592.37, :weight
|
13
|
+
SimpleMeasures::Registry.add_alias :pound, :pounds
|
14
|
+
|
15
|
+
SimpleMeasures::Registry.add_unit :milliliter, 1, :volume
|
16
|
+
SimpleMeasures::Registry.add_alias :milliliter, :milliliters
|
17
|
+
|
18
|
+
SimpleMeasures::Registry.add_unit :fluid_ounce, 28.413, :volume
|
19
|
+
SimpleMeasures::Registry.add_alias :fluid_ounce, :fl_oz
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "basics" do
|
23
|
+
it "does not accept units it doesn't know about" do
|
24
|
+
lambda {
|
25
|
+
SimpleMeasures::Unit.new( 42, :blarghs )
|
26
|
+
}.should raise_error( SimpleMeasures::InvalidUnitError )
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should know aliases for the same unit" do
|
30
|
+
SimpleMeasures::Unit.new( 2, :grams ).should == SimpleMeasures::Unit.new( 2, :gram )
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "equality" do
|
35
|
+
it "should know basic equality" do
|
36
|
+
weight1 = SimpleMeasures::Unit.new( 42, :grams )
|
37
|
+
weight2 = SimpleMeasures::Unit.new( 42, :grams )
|
38
|
+
|
39
|
+
weight1.should == weight2
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should know equality between convertable units" do
|
43
|
+
weight1 = SimpleMeasures::Unit.new( 4, :grams )
|
44
|
+
weight2 = SimpleMeasures::Unit.new( 4000, :milligrams )
|
45
|
+
|
46
|
+
weight1.should == weight2
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "conversion" do
|
51
|
+
it "should convert from grams to pounds" do
|
52
|
+
weight = SimpleMeasures::Unit.new( 907.18474, :grams )
|
53
|
+
|
54
|
+
weight.convert_to( :pounds ).should == SimpleMeasures::Unit.new( 2, :pounds )
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should convert from grams to milligrams" do
|
58
|
+
weight = SimpleMeasures::Unit.new( 2, :grams )
|
59
|
+
weight.convert_to( :milligrams ).should == SimpleMeasures::Unit.new( 2000, :milligrams )
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should have convience methods for the unit" do
|
63
|
+
weight = SimpleMeasures::Unit.new( 2, :grams )
|
64
|
+
weight.to_milligram.should == SimpleMeasures::Unit.new( 2000, :milligrams )
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should have convience methods for the alias" do
|
68
|
+
weight = SimpleMeasures::Unit.new( 2, :grams )
|
69
|
+
weight.to_milligrams.should == SimpleMeasures::Unit.new( 2000, :milligrams )
|
70
|
+
end
|
71
|
+
|
72
|
+
it "blows up if there is not a unit for a convience method" do
|
73
|
+
weight = SimpleMeasures::Unit.new( 2, :grams )
|
74
|
+
lambda{
|
75
|
+
weight.to_blarghs
|
76
|
+
}.should raise_error( NoMethodError )
|
77
|
+
end
|
78
|
+
|
79
|
+
it "blows up if you try to convert between incompatible types" do
|
80
|
+
weight = SimpleMeasures::Unit.new( 2, :grams )
|
81
|
+
lambda{
|
82
|
+
weight.convert_to(:fl_oz)
|
83
|
+
}.should raise_error( SimpleMeasures::ConversionError )
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "using coersion" do
|
88
|
+
it "is able to multiply" do
|
89
|
+
result = 2 * SimpleMeasures::Unit.new( 2,:pounds )
|
90
|
+
result.should == SimpleMeasures::Unit.new( 4, :pounds )
|
91
|
+
end
|
92
|
+
|
93
|
+
it "is able to divide" do
|
94
|
+
result = SimpleMeasures::Unit.new( 8,:pounds ) / 2
|
95
|
+
result.should == SimpleMeasures::Unit.new( 4, :pounds )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "addition" do
|
100
|
+
it "should be able to add two items of the exact same units" do
|
101
|
+
( SimpleMeasures::Unit.new( 1, :gram ) +
|
102
|
+
SimpleMeasures::Unit.new( 1, :gram ) ).should == SimpleMeasures::Unit.new( 2, :grams )
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should be able to add two items of the same units with one using an alias" do
|
106
|
+
( SimpleMeasures::Unit.new( 1, :gram ) +
|
107
|
+
SimpleMeasures::Unit.new( 2, :grams ) ).should == SimpleMeasures::Unit.new( 3, :grams )
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be able to add two items of different but compatible units" do
|
111
|
+
( SimpleMeasures::Unit.new( 1, :milligram ) +
|
112
|
+
SimpleMeasures::Unit.new( 1, :gram ) ).should == SimpleMeasures::Unit.new( 1.001, :grams )
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should not be able to add items of different types" do
|
116
|
+
lambda {
|
117
|
+
SimpleMeasures::Unit.new( 1, :gram ) + SimpleMeasures::Unit.new( 1, :milliliter)
|
118
|
+
}.should raise_error(ArgumentError)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "subtraction" do
|
123
|
+
it "should be able to subtract two items of the exact same units" do
|
124
|
+
( SimpleMeasures::Unit.new( 5, :gram ) -
|
125
|
+
SimpleMeasures::Unit.new( 1, :gram ) ).should == SimpleMeasures::Unit.new( 4, :grams )
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should be able to subtract two items of the same units with one using an alias" do
|
129
|
+
( SimpleMeasures::Unit.new( 6, :gram ) -
|
130
|
+
SimpleMeasures::Unit.new( 3, :grams ) ).should == SimpleMeasures::Unit.new( 3, :grams )
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should be able to subtract two items of different but compatible units" do
|
134
|
+
( SimpleMeasures::Unit.new( 1, :gram ) -
|
135
|
+
SimpleMeasures::Unit.new( 1, :milligram ) ).should == SimpleMeasures::Unit.new( 999, :milligrams )
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should not be able to subtract items of different types" do
|
139
|
+
lambda {
|
140
|
+
SimpleMeasures::Unit.new( 1, :gram ) - SimpleMeasures::Unit.new( 1, :milliliter)
|
141
|
+
}.should raise_error(ArgumentError)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "hashing" do
|
146
|
+
it "is correct so it can be used as a key" do
|
147
|
+
five_grams = SimpleMeasures::Unit.new( 5, :gram )
|
148
|
+
another_five_grams = SimpleMeasures::Unit.new( 5, :gram )
|
149
|
+
|
150
|
+
hash = {five_grams => 1 }.merge( another_five_grams => 2 )
|
151
|
+
hash.keys.size.should == 1
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-measures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Colin Harris
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-15 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: SimpleMeasures is an easy and extensible measurement conversion library
|
22
|
+
email: qzzzq1@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- Rakefile
|
31
|
+
- README.rdoc
|
32
|
+
- VERSION
|
33
|
+
- LICENSE
|
34
|
+
- lib/simple-measures/core_ext/numeric.rb
|
35
|
+
- lib/simple-measures/core_ext/symbol.rb
|
36
|
+
- lib/simple-measures/registry/unit_entry.rb
|
37
|
+
- lib/simple-measures/registry.rb
|
38
|
+
- lib/simple-measures/systems/imperial.rb
|
39
|
+
- lib/simple-measures/systems/si.rb
|
40
|
+
- lib/simple-measures/unit.rb
|
41
|
+
- lib/simple-measures.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/aberant/simple-measures
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: SimpleMeasures - simple unit conversions for everyday things
|
72
|
+
test_files:
|
73
|
+
- spec/simple-measures/registry_spec.rb
|
74
|
+
- spec/simple-measures/unit_spec.rb
|
75
|
+
- spec/spec_helper.rb
|