cooking 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +34 -0
- data/cooking.gemspec +1 -1
- data/lib/cooking.rb +3 -0
- data/lib/cooking/cook.rb +153 -0
- data/lib/cooking/version.rb +1 -1
- data/spec/cooking_spec.rb +11 -0
- data/test/cooking/base_test.rb +70 -0
- data/test/cooking/cooking_test.rb +31 -0
- data/test/test_helper.rb +5 -0
- metadata +14 -5
data/README.rdoc
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
==Cooking Units
|
2
|
+
|
3
|
+
- 0.0.5 -beta
|
4
|
+
|
5
|
+
This package is originally units but since i can't find the project to fork on github, I made a gem.
|
6
|
+
Also, there was a minor bug (pounds to grams) and I extracted only the conversions
|
7
|
+
needed in the kitchen.
|
8
|
+
* Use either this package or units; using both will cause a stack too deep error.
|
9
|
+
* Also, I'm having issues with this on Heroku at the moment. If your super bent on using the package before the issues are resolved, you can always freeze the gem.
|
10
|
+
|
11
|
+
|
12
|
+
== Basic Usage
|
13
|
+
|
14
|
+
require 'cooking/cook'
|
15
|
+
1.lb.to_ounces # => 16.0
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
In a Rails3 App:
|
20
|
+
|
21
|
+
add: gem to your Gemfile
|
22
|
+
|
23
|
+
gem 'cooking'
|
24
|
+
|
25
|
+
next: do it to it...
|
26
|
+
|
27
|
+
1.lb.to_ounces # => 16.0
|
28
|
+
1.lb.to_gram # => 453.59
|
29
|
+
|
30
|
+
Authors
|
31
|
+
* Mark Sadegi mailto:mark.sadegi@gmail.com
|
32
|
+
* Original package - Ruby Units http://rubyforge.org/projects/units)
|
33
|
+
* Lucas Carlson
|
34
|
+
* John Butler
|
data/cooking.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Mark Sadegi"]
|
10
10
|
s.email = ["mark.sadegi@gmail.com"]
|
11
|
-
s.homepage = ""
|
11
|
+
s.homepage = "http://github.com/marka2g/cooking"
|
12
12
|
s.summary = %q{A gem used for converting units of measurements in the kitchen}
|
13
13
|
s.description = %q{This package is originally units but since i can't find the project to fork on github, I made a gem. Also, there is a minor bug (pounds to grams wrong conversion calculation) and I extracted only the conversions needed in the kitchen. Use either this package or units; both will cause a stack too deep error.}
|
14
14
|
|
data/lib/cooking.rb
CHANGED
data/lib/cooking/cook.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
# require 'cooking/base'
|
2
|
+
class Numeric
|
3
|
+
WEIGHT = {
|
4
|
+
:pounds => 1.0,
|
5
|
+
:ounces => 0.0625,
|
6
|
+
:kilograms => 0.45359237,
|
7
|
+
:grams => 0.002204622621848776
|
8
|
+
}
|
9
|
+
WEIGHT_ALIASES = {
|
10
|
+
:pounds => [ :lb, :lbs, :pound ],
|
11
|
+
:ounces => [ :oz, :ozs, :ounce ],
|
12
|
+
:kilograms => [ :kg, :kgs, :kilogram ],
|
13
|
+
:grams => [ :gm, :gms, :gram ]
|
14
|
+
}
|
15
|
+
VOLUME = {
|
16
|
+
:pints => 0.125,
|
17
|
+
:milliliters => 0.000264172051,
|
18
|
+
:cubic_feet => 7.48051945,
|
19
|
+
:cups => 0.0625,
|
20
|
+
:microliters => 2.64172051,
|
21
|
+
:cubic_inches => 0.00432900431,
|
22
|
+
:liters => 0.264172051,
|
23
|
+
:cubic_centimeters => 0.000264172051,
|
24
|
+
:cubic_yards => 201.974025,
|
25
|
+
:hectoliters => 26.4172051,
|
26
|
+
:fluid_ounces => 0.0078125,
|
27
|
+
:cubic_millimeters => 2.64172051,
|
28
|
+
:gallons => 1,
|
29
|
+
:deciliters => 0.0264172051,
|
30
|
+
:tablespoons => 0.00390625,
|
31
|
+
:cubic_meters => 264.172051,
|
32
|
+
:quarts => 0.25,
|
33
|
+
:centiliters => 0.00264172051,
|
34
|
+
:teaspoons => 0.00130208333,
|
35
|
+
:cubic_decimeters => 0.264172051
|
36
|
+
}
|
37
|
+
VOLUME_ALIASES = {
|
38
|
+
:liters => [ :liter, :l ],
|
39
|
+
:hectoliters => [ :hectoliter, :hl ],
|
40
|
+
:deciliters => [ :deciliter, :dl ],
|
41
|
+
:centiliters => [ :centiliter, :cl ],
|
42
|
+
:milliliters => [ :milliliter, :ml ],
|
43
|
+
:microliters => [ :microliter, :ul ],
|
44
|
+
:cubic_centimeters => [ :cubic_centimeter, :cm3 ],
|
45
|
+
:cubic_millimeters => [ :cubic_millimeter, :mm3 ],
|
46
|
+
:cubic_meters => [ :cubic_meter, :m3 ],
|
47
|
+
:cubic_decimeters => [ :cubic_decimeter, :dm3 ],
|
48
|
+
:cubic_feet => [ :cubic_foot, :f3 ],
|
49
|
+
:cubic_inches => [ :cubic_inch, :i3 ],
|
50
|
+
:cubic_yards => [ :cubic_yard, :y3 ],
|
51
|
+
:gallons => [ :gallon, :gal, :gals ],
|
52
|
+
:quarts => [ :quart, :qt, :qts ],
|
53
|
+
:pints => [ :pint, :pt, :pts ],
|
54
|
+
:cups => [ :cup ],
|
55
|
+
:gills => [ :gill ],
|
56
|
+
:fluid_ounces => [ :fluid_oz, :fluid_ozs ],
|
57
|
+
:tablespoons => [ :tablespoon, :tbsp ],
|
58
|
+
:teaspoons => [ :teaspoon, :tsp ],
|
59
|
+
:fluid_drams => [ :fluid_dram ],
|
60
|
+
:minims => [ :minim ]
|
61
|
+
}
|
62
|
+
TIME = {
|
63
|
+
:seconds => 1.0,
|
64
|
+
:minutes => 60.0,
|
65
|
+
:hours => 3600.0,
|
66
|
+
:days => 86400.0,
|
67
|
+
:weeks => 604800.0,
|
68
|
+
:years => 31449600.0
|
69
|
+
}
|
70
|
+
TIME_ALIASES = {
|
71
|
+
:seconds => [ :sec, :second ],
|
72
|
+
:minutes => [ :min, :mins, :minute ],
|
73
|
+
:hours => [ :hour ],
|
74
|
+
:days => [ :day ],
|
75
|
+
:weeks => [ :week ],
|
76
|
+
:years => [ :year ]
|
77
|
+
}
|
78
|
+
SIZE = {
|
79
|
+
:bytes => 1.0,
|
80
|
+
:bits => 8.0,
|
81
|
+
:kilobytes => 1024.0,
|
82
|
+
:megabytes => 1048576.0,
|
83
|
+
:gigabytes => 1073741824.0,
|
84
|
+
:terabytes => 1099511627776.0,
|
85
|
+
:petabytes => 1.12589991e15
|
86
|
+
}
|
87
|
+
SIZE_ALIASES = {
|
88
|
+
:bits => [ :bit ],
|
89
|
+
:bytes => [ :b, :byte ],
|
90
|
+
:kilobytes => [ :kb, :kilobyte ],
|
91
|
+
:megabytes => [ :mb, :megabyte ],
|
92
|
+
:gigabytes => [ :gb, :gigabyte ],
|
93
|
+
:terabytes => [ :tb, :terabyte ],
|
94
|
+
:petabytes => [ :pb, :petabyte ]
|
95
|
+
}
|
96
|
+
LENGTH = {
|
97
|
+
:inches => 1.0,
|
98
|
+
:feet => 12.0,
|
99
|
+
:meters => 39.3700787,
|
100
|
+
:kilometers => 39370.0787,
|
101
|
+
:milimeters => 0.0393700787,
|
102
|
+
:centimeters => 0.393700787,
|
103
|
+
:miles => 63360.0
|
104
|
+
}
|
105
|
+
LENGTH_ALIASES = {
|
106
|
+
:inches => [ :inch ],
|
107
|
+
:feet => [ :foot ],
|
108
|
+
:miles => [ :mile ],
|
109
|
+
:meters => [ :m, :meter ],
|
110
|
+
:kilometers => [ :km, :kilometer ],
|
111
|
+
:milimeters => [ :mm, :milimeter ],
|
112
|
+
:centimeters => [ :cm, :centimeter ]
|
113
|
+
}
|
114
|
+
|
115
|
+
add_unit_conversions(
|
116
|
+
:weight => WEIGHT,
|
117
|
+
:volume => VOLUME,
|
118
|
+
:time => TIME,
|
119
|
+
:size => SIZE,
|
120
|
+
:length => LENGTH
|
121
|
+
)
|
122
|
+
|
123
|
+
add_unit_aliases(
|
124
|
+
:weight => WEIGHT_ALIASES,
|
125
|
+
:volume => VOLUME_ALIASES,
|
126
|
+
:time => TIME_ALIASES,
|
127
|
+
:size => SIZE_ALIASES,
|
128
|
+
:length => LENGTH_ALIASES
|
129
|
+
)
|
130
|
+
end
|
131
|
+
|
132
|
+
class Float #:nodoc:
|
133
|
+
alias :_to_i :to_i
|
134
|
+
def to_i
|
135
|
+
case kind
|
136
|
+
when :time
|
137
|
+
to_seconds._to_i
|
138
|
+
when :size
|
139
|
+
to_bytes._to_i
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
alias :_to_int :to_int
|
144
|
+
def to_int
|
145
|
+
case kind
|
146
|
+
when :time
|
147
|
+
to_seconds._to_int
|
148
|
+
when :size
|
149
|
+
to_bytes._to_int
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
# end
|
data/lib/cooking/version.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
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
|
@@ -0,0 +1,70 @@
|
|
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
|
@@ -0,0 +1,31 @@
|
|
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 0.25, 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
ADDED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: cooking
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mark Sadegi
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-20 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -36,12 +36,18 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- .gitignore
|
38
38
|
- Gemfile
|
39
|
+
- README.rdoc
|
39
40
|
- Rakefile
|
40
41
|
- cooking.gemspec
|
41
42
|
- lib/cooking.rb
|
43
|
+
- lib/cooking/cook.rb
|
42
44
|
- lib/cooking/version.rb
|
45
|
+
- spec/cooking_spec.rb
|
46
|
+
- test/cooking/base_test.rb
|
47
|
+
- test/cooking/cooking_test.rb
|
48
|
+
- test/test_helper.rb
|
43
49
|
has_rdoc: true
|
44
|
-
homepage:
|
50
|
+
homepage: http://github.com/marka2g/cooking
|
45
51
|
licenses: []
|
46
52
|
|
47
53
|
post_install_message:
|
@@ -68,5 +74,8 @@ rubygems_version: 1.6.2
|
|
68
74
|
signing_key:
|
69
75
|
specification_version: 3
|
70
76
|
summary: A gem used for converting units of measurements in the kitchen
|
71
|
-
test_files:
|
72
|
-
|
77
|
+
test_files:
|
78
|
+
- spec/cooking_spec.rb
|
79
|
+
- test/cooking/base_test.rb
|
80
|
+
- test/cooking/cooking_test.rb
|
81
|
+
- test/test_helper.rb
|