cooking 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cooking.rb +170 -1
- data/lib/cooking/version.rb +1 -1
- metadata +6 -27
- data/.rvmrc +0 -1
- data/README.rdoc +0 -30
- data/lib/cooking/base.rb +0 -173
- data/lib/cooking/cook.rb +0 -154
- data/spec/cooking_spec.rb +0 -11
- data/test/cooking/base_test.rb +0 -70
- data/test/cooking/cooking_test.rb +0 -31
- data/test/test_helper.rb +0 -4
data/lib/cooking.rb
CHANGED
@@ -1 +1,170 @@
|
|
1
|
-
|
1
|
+
module Cooking
|
2
|
+
attr_reader :unit, :kind
|
3
|
+
alias :units :unit
|
4
|
+
|
5
|
+
# The heart of unit conversion, it will handle methods like: to_seconds and cast the number accordingly.
|
6
|
+
def method_missing(symbol, *args)
|
7
|
+
symbol = symbol.to_s.sub(/^to_/,'').intern if symbol.to_s =~ /^to_.+/
|
8
|
+
|
9
|
+
to_kind, to_unit = lookup_unit(symbol)
|
10
|
+
|
11
|
+
# If there is a kind and this is conversion and the aliases include the provided symbol, convert
|
12
|
+
if to_kind && to_unit && self.class.all_unit_aliases(to_kind).include?(symbol)
|
13
|
+
from_unit = (@unit || to_unit)
|
14
|
+
from_kind = lookup_unit(from_unit).first
|
15
|
+
|
16
|
+
if from_kind != to_kind
|
17
|
+
raise UnitsError, "invalid conversion, cannot convert #{from_unit} (a #{from_kind}) to #{to_unit} (a #{to_kind})"
|
18
|
+
|
19
|
+
else
|
20
|
+
# The reason these numbers have to be floats instead of integers is that all similar integers are the same ones
|
21
|
+
# in memory, so that @kind and @unit couldn't be different for different numbers
|
22
|
+
case self.class.unit_conversions[to_kind]
|
23
|
+
when Hash
|
24
|
+
result = Float( self * self.class.unit_conversions[from_kind][from_unit] / self.class.unit_conversions[to_kind][to_unit] )
|
25
|
+
when Symbol
|
26
|
+
result = Float( self * send(self.class.unit_conversions[to_kind], from_unit, to_unit) )
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
result.instance_eval do
|
31
|
+
@unit = to_unit
|
32
|
+
@kind = to_kind
|
33
|
+
end
|
34
|
+
|
35
|
+
return result
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# lookup a kind and base unit (like [:volume, :liters]) when you input :liter
|
44
|
+
def lookup_unit(symbol)
|
45
|
+
self.class.unit_conversions.keys.each do |kind|
|
46
|
+
if Symbol === self.class.unit_conversions[kind]
|
47
|
+
return kind, symbol if send(:"#{ self.class.unit_conversions[kind] }_include?", symbol)
|
48
|
+
else
|
49
|
+
if self.class.unit_conversions[kind].include? symbol
|
50
|
+
return kind, symbol
|
51
|
+
else
|
52
|
+
s = self.class.unit_aliases[kind].find { |k,v| v.include? symbol }
|
53
|
+
return kind, s[0] if s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
return nil, nil
|
58
|
+
end
|
59
|
+
|
60
|
+
module ClassMethods #:nodoc:
|
61
|
+
def unit_conversions() @@unit_conversions end
|
62
|
+
def unit_aliases() @@unit_aliases end
|
63
|
+
def add_unit_conversions(hash={}) unit_conversions.update(hash) end
|
64
|
+
def add_unit_aliases(hash={}) unit_aliases.update(hash) end
|
65
|
+
|
66
|
+
def init_units
|
67
|
+
@@unit_conversions = Hash.new
|
68
|
+
@@unit_aliases = Hash.new
|
69
|
+
end
|
70
|
+
|
71
|
+
def all_unit_aliases(kind)
|
72
|
+
results = Array.new
|
73
|
+
results += @@unit_conversions[kind].keys rescue nil
|
74
|
+
results += @@unit_aliases[kind].to_a.flatten rescue nil
|
75
|
+
|
76
|
+
return results.uniq
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.append_features(base) #:nodoc:
|
81
|
+
super
|
82
|
+
base.extend ClassMethods
|
83
|
+
base.init_units
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class Numeric
|
88
|
+
include Cooking
|
89
|
+
end
|
90
|
+
|
91
|
+
class Float
|
92
|
+
alias :add :+
|
93
|
+
# Add only numbers that both have units or both don't have units
|
94
|
+
def +(other)
|
95
|
+
if Float === other && kind && other.kind
|
96
|
+
add_with_units( unit == other.unit ? other : other.send(unit) )
|
97
|
+
|
98
|
+
elsif Numeric === other && (kind || other.kind) && (kind.nil? || other.kind.nil?)
|
99
|
+
raise UnitsError, "cannot add a number without units to one with units"
|
100
|
+
|
101
|
+
else
|
102
|
+
add other
|
103
|
+
end
|
104
|
+
end
|
105
|
+
def add_with_units(other)
|
106
|
+
add(other).send(unit)
|
107
|
+
end
|
108
|
+
|
109
|
+
alias :multiply :*
|
110
|
+
# CURRENTLY: Scalar multiplication (a number with a unit to a number without a unit)
|
111
|
+
# TO COME: Non-scalar multiplication
|
112
|
+
# This will require keeping track of the exponents, like:
|
113
|
+
# meters is [:meters, 1], square inches is [:inches, 2], cubic mililiters is [:milileters, 3]
|
114
|
+
# And then we can get rid of the silly :m3's in the volume conversion as well
|
115
|
+
# as add new units like:
|
116
|
+
# :joules => [[:kilograms, 1], [:meters, 1], [:seconds, -2]]
|
117
|
+
def *(other)
|
118
|
+
if Numeric === other && kind && other.kind
|
119
|
+
raise UnitsError, "currently cannot mutiply two numers with units, try scalar multiplication instead"
|
120
|
+
|
121
|
+
elsif Numeric === other && kind.nil? && other.kind.nil?
|
122
|
+
multiply other
|
123
|
+
|
124
|
+
else
|
125
|
+
multiply_with_units other
|
126
|
+
end
|
127
|
+
end
|
128
|
+
def multiply_with_units(other)
|
129
|
+
multiply(other).send(unit || other.unit)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Fixnum
|
134
|
+
alias :add :+
|
135
|
+
# Raise an error if the other number has a unit
|
136
|
+
def +(other)
|
137
|
+
if Numeric === other && other.kind
|
138
|
+
raise UnitsError, "cannot add a number without units to one with units"
|
139
|
+
|
140
|
+
else
|
141
|
+
add other
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
alias :multiply :*
|
146
|
+
# Allow for scalar multiplication
|
147
|
+
def *(other)
|
148
|
+
if Numeric === other && other.kind
|
149
|
+
multiply_with_units other
|
150
|
+
|
151
|
+
else
|
152
|
+
multiply other
|
153
|
+
end
|
154
|
+
end
|
155
|
+
def multiply_with_units(other)
|
156
|
+
multiply(other).send(unit || other.unit)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
class String
|
161
|
+
alias :multiply :*
|
162
|
+
# Cannot multiply a String by anything Numeric with units
|
163
|
+
def *(other)
|
164
|
+
if Numeric === other && other.kind
|
165
|
+
raise UnitsError, "cannot multiply a String by anything Numeric with units"
|
166
|
+
else
|
167
|
+
multiply other
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
data/lib/cooking/version.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cooking
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
version: 0.0.5
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Mark Sadegi
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date: 2011-
|
13
|
+
date: 2011-04-18 00:00:00 -05:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,8 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ">="
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 0
|
30
24
|
version: "0"
|
31
25
|
type: :development
|
32
26
|
version_requirements: *id001
|
@@ -41,19 +35,11 @@ extra_rdoc_files: []
|
|
41
35
|
|
42
36
|
files:
|
43
37
|
- .gitignore
|
44
|
-
- .rvmrc
|
45
38
|
- Gemfile
|
46
|
-
- README.rdoc
|
47
39
|
- Rakefile
|
48
40
|
- cooking.gemspec
|
49
41
|
- lib/cooking.rb
|
50
|
-
- lib/cooking/base.rb
|
51
|
-
- lib/cooking/cook.rb
|
52
42
|
- lib/cooking/version.rb
|
53
|
-
- spec/cooking_spec.rb
|
54
|
-
- test/cooking/base_test.rb
|
55
|
-
- test/cooking/cooking_test.rb
|
56
|
-
- test/test_helper.rb
|
57
43
|
has_rdoc: true
|
58
44
|
homepage: ""
|
59
45
|
licenses: []
|
@@ -68,26 +54,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
54
|
requirements:
|
69
55
|
- - ">="
|
70
56
|
- !ruby/object:Gem::Version
|
71
|
-
segments:
|
72
|
-
- 0
|
73
57
|
version: "0"
|
74
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
59
|
none: false
|
76
60
|
requirements:
|
77
61
|
- - ">="
|
78
62
|
- !ruby/object:Gem::Version
|
79
|
-
segments:
|
80
|
-
- 0
|
81
63
|
version: "0"
|
82
64
|
requirements: []
|
83
65
|
|
84
66
|
rubyforge_project: cooking
|
85
|
-
rubygems_version: 1.
|
67
|
+
rubygems_version: 1.6.2
|
86
68
|
signing_key:
|
87
69
|
specification_version: 3
|
88
70
|
summary: A gem used for converting units of measurements in the kitchen
|
89
|
-
test_files:
|
90
|
-
|
91
|
-
- test/cooking/base_test.rb
|
92
|
-
- test/cooking/cooking_test.rb
|
93
|
-
- test/test_helper.rb
|
71
|
+
test_files: []
|
72
|
+
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm 1.9.2@cooking
|
data/README.rdoc
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
==Cooking Units
|
2
|
-
|
3
|
-
This package is originally units but since i can't find the project to fork on github, I made a gem.
|
4
|
-
Also, there was a minor bug (pounds to grams) and I extracted only the conversions
|
5
|
-
needed in the kitchen.
|
6
|
-
*Use either this package or units; using both will cause a stack too deep error.
|
7
|
-
|
8
|
-
== Basic Usage
|
9
|
-
|
10
|
-
require 'cooking/cook'
|
11
|
-
1.lb.to_ounces # => 16.0
|
12
|
-
|
13
|
-
== Usage
|
14
|
-
|
15
|
-
In a Rails3 App:
|
16
|
-
|
17
|
-
add: add the following to your Gemfile
|
18
|
-
|
19
|
-
gem 'cooking'
|
20
|
-
|
21
|
-
next: do it to it...
|
22
|
-
|
23
|
-
1.lb.to_ounces # => 16.0
|
24
|
-
1.lb.to_gram # => 453.59
|
25
|
-
|
26
|
-
Authors
|
27
|
-
* Mark Sadegi mailto:mark.sadegi@gmail.com
|
28
|
-
* Original package - Ruby Units http://rubyforge.org/projects/units)
|
29
|
-
* Lucas Carlson
|
30
|
-
* John Butler
|
data/lib/cooking/base.rb
DELETED
@@ -1,173 +0,0 @@
|
|
1
|
-
class CookingError < StandardError #:nodoc:
|
2
|
-
end
|
3
|
-
|
4
|
-
module Cooking
|
5
|
-
attr_reader :unit, :kind
|
6
|
-
alias :units :unit
|
7
|
-
|
8
|
-
# The heart of unit conversion, it will handle methods like: to_seconds and cast the number accordingly.
|
9
|
-
def method_missing(symbol, *args)
|
10
|
-
symbol = symbol.to_s.sub(/^to_/,'').intern if symbol.to_s =~ /^to_.+/
|
11
|
-
|
12
|
-
to_kind, to_unit = lookup_unit(symbol)
|
13
|
-
|
14
|
-
# If there is a kind and this is conversion and the aliases include the provided symbol, convert
|
15
|
-
if to_kind && to_unit && self.class.all_unit_aliases(to_kind).include?(symbol)
|
16
|
-
from_unit = (@unit || to_unit)
|
17
|
-
from_kind = lookup_unit(from_unit).first
|
18
|
-
|
19
|
-
if from_kind != to_kind
|
20
|
-
raise UnitsError, "invalid conversion, cannot convert #{from_unit} (a #{from_kind}) to #{to_unit} (a #{to_kind})"
|
21
|
-
|
22
|
-
else
|
23
|
-
# The reason these numbers have to be floats instead of integers is that all similar integers are the same ones
|
24
|
-
# in memory, so that @kind and @unit couldn't be different for different numbers
|
25
|
-
case self.class.unit_conversions[to_kind]
|
26
|
-
when Hash
|
27
|
-
result = Float( self * self.class.unit_conversions[from_kind][from_unit] / self.class.unit_conversions[to_kind][to_unit] )
|
28
|
-
when Symbol
|
29
|
-
result = Float( self * send(self.class.unit_conversions[to_kind], from_unit, to_unit) )
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
result.instance_eval do
|
34
|
-
@unit = to_unit
|
35
|
-
@kind = to_kind
|
36
|
-
end
|
37
|
-
|
38
|
-
return result
|
39
|
-
else
|
40
|
-
super
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
# lookup a kind and base unit (like [:volume, :liters]) when you input :liter
|
47
|
-
def lookup_unit(symbol)
|
48
|
-
self.class.unit_conversions.keys.each do |kind|
|
49
|
-
if Symbol === self.class.unit_conversions[kind]
|
50
|
-
return kind, symbol if send(:"#{ self.class.unit_conversions[kind] }_include?", symbol)
|
51
|
-
else
|
52
|
-
if self.class.unit_conversions[kind].include? symbol
|
53
|
-
return kind, symbol
|
54
|
-
else
|
55
|
-
s = self.class.unit_aliases[kind].find { |k,v| v.include? symbol }
|
56
|
-
return kind, s[0] if s
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
return nil, nil
|
61
|
-
end
|
62
|
-
|
63
|
-
module ClassMethods #:nodoc:
|
64
|
-
def unit_conversions() @@unit_conversions end
|
65
|
-
def unit_aliases() @@unit_aliases end
|
66
|
-
def add_unit_conversions(hash={}) unit_conversions.update(hash) end
|
67
|
-
def add_unit_aliases(hash={}) unit_aliases.update(hash) end
|
68
|
-
|
69
|
-
def init_units
|
70
|
-
@@unit_conversions = Hash.new
|
71
|
-
@@unit_aliases = Hash.new
|
72
|
-
end
|
73
|
-
|
74
|
-
def all_unit_aliases(kind)
|
75
|
-
results = Array.new
|
76
|
-
results += @@unit_conversions[kind].keys rescue nil
|
77
|
-
results += @@unit_aliases[kind].to_a.flatten rescue nil
|
78
|
-
|
79
|
-
return results.uniq
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def self.append_features(base) #:nodoc:
|
84
|
-
super
|
85
|
-
base.extend ClassMethods
|
86
|
-
base.init_units
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
class Numeric
|
91
|
-
include Cooking
|
92
|
-
end
|
93
|
-
|
94
|
-
class Float
|
95
|
-
alias :add :+
|
96
|
-
# Add only numbers that both have units or both don't have units
|
97
|
-
def +(other)
|
98
|
-
if Float === other && kind && other.kind
|
99
|
-
add_with_units( unit == other.unit ? other : other.send(unit) )
|
100
|
-
|
101
|
-
elsif Numeric === other && (kind || other.kind) && (kind.nil? || other.kind.nil?)
|
102
|
-
raise UnitsError, "cannot add a number without units to one with units"
|
103
|
-
|
104
|
-
else
|
105
|
-
add other
|
106
|
-
end
|
107
|
-
end
|
108
|
-
def add_with_units(other)
|
109
|
-
add(other).send(unit)
|
110
|
-
end
|
111
|
-
|
112
|
-
alias :multiply :*
|
113
|
-
# CURRENTLY: Scalar multiplication (a number with a unit to a number without a unit)
|
114
|
-
# TO COME: Non-scalar multiplication
|
115
|
-
# This will require keeping track of the exponents, like:
|
116
|
-
# meters is [:meters, 1], square inches is [:inches, 2], cubic mililiters is [:milileters, 3]
|
117
|
-
# And then we can get rid of the silly :m3's in the volume conversion as well
|
118
|
-
# as add new units like:
|
119
|
-
# :joules => [[:kilograms, 1], [:meters, 1], [:seconds, -2]]
|
120
|
-
def *(other)
|
121
|
-
if Numeric === other && kind && other.kind
|
122
|
-
raise UnitsError, "currently cannot mutiply two numers with units, try scalar multiplication instead"
|
123
|
-
|
124
|
-
elsif Numeric === other && kind.nil? && other.kind.nil?
|
125
|
-
multiply other
|
126
|
-
|
127
|
-
else
|
128
|
-
multiply_with_units other
|
129
|
-
end
|
130
|
-
end
|
131
|
-
def multiply_with_units(other)
|
132
|
-
multiply(other).send(unit || other.unit)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
class Fixnum
|
137
|
-
alias :add :+
|
138
|
-
# Raise an error if the other number has a unit
|
139
|
-
def +(other)
|
140
|
-
if Numeric === other && other.kind
|
141
|
-
raise UnitsError, "cannot add a number without units to one with units"
|
142
|
-
|
143
|
-
else
|
144
|
-
add other
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
alias :multiply :*
|
149
|
-
# Allow for scalar multiplication
|
150
|
-
def *(other)
|
151
|
-
if Numeric === other && other.kind
|
152
|
-
multiply_with_units other
|
153
|
-
|
154
|
-
else
|
155
|
-
multiply other
|
156
|
-
end
|
157
|
-
end
|
158
|
-
def multiply_with_units(other)
|
159
|
-
multiply(other).send(unit || other.unit)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
class String
|
164
|
-
alias :multiply :*
|
165
|
-
# Cannot multiply a String by anything Numeric with units
|
166
|
-
def *(other)
|
167
|
-
if Numeric === other && other.kind
|
168
|
-
raise UnitsError, "cannot multiply a String by anything Numeric with units"
|
169
|
-
else
|
170
|
-
multiply other
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
data/lib/cooking/cook.rb
DELETED
@@ -1,154 +0,0 @@
|
|
1
|
-
require 'cooking/base'
|
2
|
-
# module Cooking
|
3
|
-
class Numeric
|
4
|
-
WEIGHT = {
|
5
|
-
:pounds => 1.0,
|
6
|
-
:ounces => 0.0625,
|
7
|
-
:kilograms => 0.45359237,
|
8
|
-
:grams => 0.002204622621848776
|
9
|
-
}
|
10
|
-
WEIGHT_ALIASES = {
|
11
|
-
:pounds => [ :lb, :lbs, :pound ],
|
12
|
-
:ounces => [ :oz, :ozs, :ounce ],
|
13
|
-
:kilograms => [ :kg, :kgs, :kilogram ],
|
14
|
-
:grams => [ :gm, :gms, :gram ]
|
15
|
-
}
|
16
|
-
VOLUME = {
|
17
|
-
:pints => 0.125,
|
18
|
-
:milliliters => 0.000264172051,
|
19
|
-
:cubic_feet => 7.48051945,
|
20
|
-
:cups => 0.0625,
|
21
|
-
:microliters => 2.64172051,
|
22
|
-
:cubic_inches => 0.00432900431,
|
23
|
-
:liters => 0.264172051,
|
24
|
-
:cubic_centimeters => 0.000264172051,
|
25
|
-
:cubic_yards => 201.974025,
|
26
|
-
:hectoliters => 26.4172051,
|
27
|
-
:fluid_ounces => 0.0078125,
|
28
|
-
:cubic_millimeters => 2.64172051,
|
29
|
-
:gallons => 1,
|
30
|
-
:deciliters => 0.0264172051,
|
31
|
-
:tablespoons => 0.00390625,
|
32
|
-
:cubic_meters => 264.172051,
|
33
|
-
:quarts => 0.25,
|
34
|
-
:centiliters => 0.00264172051,
|
35
|
-
:teaspoons => 0.00130208333,
|
36
|
-
:cubic_decimeters => 0.264172051
|
37
|
-
}
|
38
|
-
VOLUME_ALIASES = {
|
39
|
-
:liters => [ :liter, :l ],
|
40
|
-
:hectoliters => [ :hectoliter, :hl ],
|
41
|
-
:deciliters => [ :deciliter, :dl ],
|
42
|
-
:centiliters => [ :centiliter, :cl ],
|
43
|
-
:milliliters => [ :milliliter, :ml ],
|
44
|
-
:microliters => [ :microliter, :ul ],
|
45
|
-
:cubic_centimeters => [ :cubic_centimeter, :cm3 ],
|
46
|
-
:cubic_millimeters => [ :cubic_millimeter, :mm3 ],
|
47
|
-
:cubic_meters => [ :cubic_meter, :m3 ],
|
48
|
-
:cubic_decimeters => [ :cubic_decimeter, :dm3 ],
|
49
|
-
:cubic_feet => [ :cubic_foot, :f3 ],
|
50
|
-
:cubic_inches => [ :cubic_inch, :i3 ],
|
51
|
-
:cubic_yards => [ :cubic_yard, :y3 ],
|
52
|
-
:gallons => [ :gallon, :gal, :gals ],
|
53
|
-
:quarts => [ :quart, :qt, :qts ],
|
54
|
-
:pints => [ :pint, :pt, :pts ],
|
55
|
-
:cups => [ :cup ],
|
56
|
-
:gills => [ :gill ],
|
57
|
-
:fluid_ounces => [ :fluid_oz, :fluid_ozs ],
|
58
|
-
:tablespoons => [ :tablespoon, :tbsp ],
|
59
|
-
:teaspoons => [ :teaspoon, :tsp ],
|
60
|
-
:fluid_drams => [ :fluid_dram ],
|
61
|
-
:minims => [ :minim ]
|
62
|
-
}
|
63
|
-
TIME = {
|
64
|
-
:seconds => 1.0,
|
65
|
-
:minutes => 60.0,
|
66
|
-
:hours => 3600.0,
|
67
|
-
:days => 86400.0,
|
68
|
-
:weeks => 604800.0,
|
69
|
-
:years => 31449600.0
|
70
|
-
}
|
71
|
-
TIME_ALIASES = {
|
72
|
-
:seconds => [ :sec, :second ],
|
73
|
-
:minutes => [ :min, :mins, :minute ],
|
74
|
-
:hours => [ :hour ],
|
75
|
-
:days => [ :day ],
|
76
|
-
:weeks => [ :week ],
|
77
|
-
:years => [ :year ]
|
78
|
-
}
|
79
|
-
SIZE = {
|
80
|
-
:bytes => 1.0,
|
81
|
-
:bits => 8.0,
|
82
|
-
:kilobytes => 1024.0,
|
83
|
-
:megabytes => 1048576.0,
|
84
|
-
:gigabytes => 1073741824.0,
|
85
|
-
:terabytes => 1099511627776.0,
|
86
|
-
:petabytes => 1.12589991e15
|
87
|
-
}
|
88
|
-
SIZE_ALIASES = {
|
89
|
-
:bits => [ :bit ],
|
90
|
-
:bytes => [ :b, :byte ],
|
91
|
-
:kilobytes => [ :kb, :kilobyte ],
|
92
|
-
:megabytes => [ :mb, :megabyte ],
|
93
|
-
:gigabytes => [ :gb, :gigabyte ],
|
94
|
-
:terabytes => [ :tb, :terabyte ],
|
95
|
-
:petabytes => [ :pb, :petabyte ]
|
96
|
-
}
|
97
|
-
LENGTH = {
|
98
|
-
:inches => 1.0,
|
99
|
-
:feet => 12.0,
|
100
|
-
:meters => 39.3700787,
|
101
|
-
:kilometers => 39370.0787,
|
102
|
-
:milimeters => 0.0393700787,
|
103
|
-
:centimeters => 0.393700787,
|
104
|
-
:miles => 63360.0
|
105
|
-
}
|
106
|
-
LENGTH_ALIASES = {
|
107
|
-
:inches => [ :inch ],
|
108
|
-
:feet => [ :foot ],
|
109
|
-
:miles => [ :mile ],
|
110
|
-
:meters => [ :m, :meter ],
|
111
|
-
:kilometers => [ :km, :kilometer ],
|
112
|
-
:milimeters => [ :mm, :milimeter ],
|
113
|
-
:centimeters => [ :cm, :centimeter ]
|
114
|
-
}
|
115
|
-
|
116
|
-
add_unit_conversions(
|
117
|
-
:weight => WEIGHT,
|
118
|
-
:volume => VOLUME,
|
119
|
-
:time => TIME,
|
120
|
-
:size => SIZE,
|
121
|
-
:length => LENGTH
|
122
|
-
)
|
123
|
-
|
124
|
-
add_unit_aliases(
|
125
|
-
:weight => WEIGHT_ALIASES,
|
126
|
-
:volume => VOLUME_ALIASES,
|
127
|
-
:time => TIME_ALIASES,
|
128
|
-
:size => SIZE_ALIASES,
|
129
|
-
:length => LENGTH_ALIASES
|
130
|
-
)
|
131
|
-
end
|
132
|
-
|
133
|
-
class Float #:nodoc:
|
134
|
-
alias :_to_i :to_i
|
135
|
-
def to_i
|
136
|
-
case kind
|
137
|
-
when :time
|
138
|
-
to_seconds._to_i
|
139
|
-
when :size
|
140
|
-
to_bytes._to_i
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
alias :_to_int :to_int
|
145
|
-
def to_int
|
146
|
-
case kind
|
147
|
-
when :time
|
148
|
-
to_seconds._to_int
|
149
|
-
when :size
|
150
|
-
to_bytes._to_int
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
# end
|
data/spec/cooking_spec.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'cooking/cook'
|
2
|
-
|
3
|
-
describe Cooking::Cook do
|
4
|
-
it "broccoli is gross" do
|
5
|
-
Cooking::Cook.portray("Broccoli").should eql("Gross!")
|
6
|
-
end
|
7
|
-
|
8
|
-
it "anything else is delicious" do
|
9
|
-
Cooking::Cook.portray("Not Broccoli").should eql("Delicious!")
|
10
|
-
end
|
11
|
-
end
|
data/test/cooking/base_test.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
|
3
|
-
class BaseTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@num1 = 1234.5678
|
6
|
-
@num2 = 9876.5432
|
7
|
-
|
8
|
-
[@num1, @num2].each do |n|
|
9
|
-
n.instance_eval do
|
10
|
-
self.class.add_unit_conversions(
|
11
|
-
:test_kind => {
|
12
|
-
:bases => 1.0,
|
13
|
-
:biggers => 10.0
|
14
|
-
},
|
15
|
-
:bad_kind => {
|
16
|
-
:flavors => 1.0,
|
17
|
-
}
|
18
|
-
)
|
19
|
-
self.class.add_unit_aliases(
|
20
|
-
:test_kind => {
|
21
|
-
:bases => [ :base ],
|
22
|
-
:biggers => [ :bigger ]
|
23
|
-
}
|
24
|
-
)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_add_unit_conversions
|
30
|
-
assert_equal :test_kind, @num1.to_bases.kind
|
31
|
-
assert_equal :bad_kind, @num2.flavors.kind
|
32
|
-
assert_equal :bases, @num1.to_bases.unit
|
33
|
-
assert_equal :bases, @num1.to_bases.to_bases.unit
|
34
|
-
assert_equal :biggers, @num2.to_biggers.unit
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_add_unit_aliases
|
38
|
-
assert_equal :bases, @num1.to_base.unit
|
39
|
-
assert_equal :biggers, @num2.to_bigger.unit
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_adding
|
43
|
-
assert_equal 11111.111, @num1 + @num2
|
44
|
-
assert_equal 11111.111, @num2 + @num1
|
45
|
-
|
46
|
-
assert_equal 99999.9998, @num1.to_base + @num2.to_biggers
|
47
|
-
assert_equal 22222.2212, @num2.to_base + @num1.to_biggers
|
48
|
-
|
49
|
-
assert_equal :bases, (@num1.to_base + @num2.to_biggers).units
|
50
|
-
assert_equal :biggers, (@num1.to_biggers + @num2.to_bases).units
|
51
|
-
|
52
|
-
assert_raise(UnitsError) { @num1.to_base + 2 }
|
53
|
-
assert_raise(UnitsError) { 5 + @num1.to_base }
|
54
|
-
assert_raise(UnitsError) { @num1.to_base + 2.0 }
|
55
|
-
assert_raise(UnitsError) { 5.0 + @num1.to_base }
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_multiplying
|
59
|
-
assert_equal 2469.1356, @num1 * 2
|
60
|
-
assert_equal 2469.1356, @num1.to_base * 2
|
61
|
-
assert_equal nil, (@num1 * 2).units
|
62
|
-
assert_equal :bases, (@num1.to_base * 2).units
|
63
|
-
|
64
|
-
assert_raise(UnitsError) { @num1.to_base * @num2.to_base }
|
65
|
-
assert_raise(TypeError) { @num1.to_base * "string" }
|
66
|
-
assert_raise(UnitsError) { "string" * @num1.to_base }
|
67
|
-
assert_equal "stringstringstring", "string" * 3.0
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
|
3
|
-
class CookingTest < Test::Unit::TestCase
|
4
|
-
def test_weight
|
5
|
-
assert_equal 64.0, 4.pounds.to_ozs
|
6
|
-
assert_equal :ounces, (4.pounds.to_ozs).units
|
7
|
-
assert_equal :weight, (4.pounds.to_ozs).kind
|
8
|
-
puts "4 lbs = #{(4.pounds.to_ozs)} oz."
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_weight2
|
12
|
-
assert_equal 1814.36948, 4.pounds.to_grams
|
13
|
-
assert_equal :grams, (4.pounds.to_grams).units
|
14
|
-
assert_equal :weight, (4.pounds.to_grams).kind
|
15
|
-
puts "1 lb = #{1.pounds.to_grams} grams"
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_volume
|
19
|
-
assert_equal 4.0, 1.cups.to_quarts
|
20
|
-
assert_equal :quarts, (1.cups.to_quarts).units
|
21
|
-
assert_equal :volume, (1.cups.to_quarts).kind
|
22
|
-
puts "1 cup = #{1.cups.to_quarts} quarts"
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_volume2
|
26
|
-
assert_equal 1.056688204, 1.liter.to_quarts
|
27
|
-
assert_equal :quarts, (1.liter.to_quarts).units
|
28
|
-
assert_equal :volume, (1.liter.to_quarts).kind
|
29
|
-
puts "1 liter = #{1.liter.to_quarts} quarts"
|
30
|
-
end
|
31
|
-
end
|
data/test/test_helper.rb
DELETED