cooking 0.0.2

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@cooking
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cooking.gemspec
4
+ gemspec
@@ -0,0 +1,25 @@
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 were a few minor bugs (incorrect conversion calculations) and I extracted only the conversions
5
+ needed in the kitchen.
6
+
7
+ == Usage
8
+
9
+ In a Rails3 App:
10
+
11
+ add: add the following to your Gemfile
12
+
13
+ gem 'cooking'
14
+
15
+ next: do it to it...
16
+
17
+ 1.lb.to_ounces # => 16.0
18
+ 1.lb.to_gram # => 453.59
19
+
20
+ Authors
21
+ * Mark Sadegi mailto:mark.sadegi@gmail.com
22
+
23
+ * Original package - Ruby Units http://rubyforge.org/projects/units)
24
+ * Lucas Carlson
25
+ * John Butler
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cooking/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cooking"
7
+ s.version = Cooking::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mark Sadegi"]
10
+ s.email = ["mark.sadegi@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{A gem used for converting units of measurements in the kitchen}
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 were a few minor bugs (incorrect conversion calculations) and I extracted only the conversions needed in the kitchen.}
14
+
15
+ s.rubyforge_project = "cooking"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ # s.add_development_dependency "rspec", "~> 2.0.0.beta.22"
22
+ s.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1 @@
1
+ require 'cooking/base'
@@ -0,0 +1,173 @@
1
+ class UnitsError < StandardError #:nodoc:
2
+ end
3
+
4
+ module Units
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 Units
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
@@ -0,0 +1,155 @@
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
+ :cups => 1,
22
+ :microliters => 2.64172051,
23
+ :cubic_inches => 0.00432900431,
24
+ :liters => 0.264172051,
25
+ :cubic_centimeters => 0.000264172051,
26
+ :cubic_yards => 201.974025,
27
+ :hectoliters => 26.4172051,
28
+ :fluid_ounces => 0.0078125,
29
+ :cubic_millimeters => 2.64172051,
30
+ :gallons => 1,
31
+ :deciliters => 0.0264172051,
32
+ :tablespoons => 0.00390625,
33
+ :cubic_meters => 264.172051,
34
+ :quarts => 0.25,
35
+ :centiliters => 0.00264172051,
36
+ :teaspoons => 0.00130208333,
37
+ :cubic_decimeters => 0.264172051
38
+ }
39
+ VOLUME_ALIASES = {
40
+ :liters => [ :liter, :l ],
41
+ :hectoliters => [ :hectoliter, :hl ],
42
+ :deciliters => [ :deciliter, :dl ],
43
+ :centiliters => [ :centiliter, :cl ],
44
+ :milliliters => [ :milliliter, :ml ],
45
+ :microliters => [ :microliter, :ul ],
46
+ :cubic_centimeters => [ :cubic_centimeter, :cm3 ],
47
+ :cubic_millimeters => [ :cubic_millimeter, :mm3 ],
48
+ :cubic_meters => [ :cubic_meter, :m3 ],
49
+ :cubic_decimeters => [ :cubic_decimeter, :dm3 ],
50
+ :cubic_feet => [ :cubic_foot, :f3 ],
51
+ :cubic_inches => [ :cubic_inch, :i3 ],
52
+ :cubic_yards => [ :cubic_yard, :y3 ],
53
+ :gallons => [ :gallon, :gal, :gals ],
54
+ :quarts => [ :quart, :qt, :qts ],
55
+ :pints => [ :pint, :pt, :pts ],
56
+ :cups => [ :cup ],
57
+ :gills => [ :gill ],
58
+ :fluid_ounces => [ :fluid_oz, :fluid_ozs ],
59
+ :tablespoons => [ :tablespoon, :tbsp ],
60
+ :teaspoons => [ :teaspoon, :tsp ],
61
+ :fluid_drams => [ :fluid_dram ],
62
+ :minims => [ :minim ]
63
+ }
64
+ TIME = {
65
+ :seconds => 1.0,
66
+ :minutes => 60.0,
67
+ :hours => 3600.0,
68
+ :days => 86400.0,
69
+ :weeks => 604800.0,
70
+ :years => 31449600.0
71
+ }
72
+ TIME_ALIASES = {
73
+ :seconds => [ :sec, :second ],
74
+ :minutes => [ :min, :mins, :minute ],
75
+ :hours => [ :hour ],
76
+ :days => [ :day ],
77
+ :weeks => [ :week ],
78
+ :years => [ :year ]
79
+ }
80
+ SIZE = {
81
+ :bytes => 1.0,
82
+ :bits => 8.0,
83
+ :kilobytes => 1024.0,
84
+ :megabytes => 1048576.0,
85
+ :gigabytes => 1073741824.0,
86
+ :terabytes => 1099511627776.0,
87
+ :petabytes => 1.12589991e15
88
+ }
89
+ SIZE_ALIASES = {
90
+ :bits => [ :bit ],
91
+ :bytes => [ :b, :byte ],
92
+ :kilobytes => [ :kb, :kilobyte ],
93
+ :megabytes => [ :mb, :megabyte ],
94
+ :gigabytes => [ :gb, :gigabyte ],
95
+ :terabytes => [ :tb, :terabyte ],
96
+ :petabytes => [ :pb, :petabyte ]
97
+ }
98
+ LENGTH = {
99
+ :inches => 1.0,
100
+ :feet => 12.0,
101
+ :meters => 39.3700787,
102
+ :kilometers => 39370.0787,
103
+ :milimeters => 0.0393700787,
104
+ :centimeters => 0.393700787,
105
+ :miles => 63360.0
106
+ }
107
+ LENGTH_ALIASES = {
108
+ :inches => [ :inch ],
109
+ :feet => [ :foot ],
110
+ :miles => [ :mile ],
111
+ :meters => [ :m, :meter ],
112
+ :kilometers => [ :km, :kilometer ],
113
+ :milimeters => [ :mm, :milimeter ],
114
+ :centimeters => [ :cm, :centimeter ]
115
+ }
116
+
117
+ add_unit_conversions(
118
+ :weight => WEIGHT,
119
+ :volume => VOLUME,
120
+ :time => TIME,
121
+ :size => SIZE,
122
+ :length => LENGTH
123
+ )
124
+
125
+ add_unit_aliases(
126
+ :weight => WEIGHT_ALIASES,
127
+ :volume => VOLUME_ALIASES,
128
+ :time => TIME_ALIASES,
129
+ :size => SIZE_ALIASES,
130
+ :length => LENGTH_ALIASES
131
+ )
132
+ end
133
+
134
+ class Float #:nodoc:
135
+ alias :_to_i :to_i
136
+ def to_i
137
+ case kind
138
+ when :time
139
+ to_seconds._to_i
140
+ when :size
141
+ to_bytes._to_i
142
+ end
143
+ end
144
+
145
+ alias :_to_int :to_int
146
+ def to_int
147
+ case kind
148
+ when :time
149
+ to_seconds._to_int
150
+ when :size
151
+ to_bytes._to_int
152
+ end
153
+ end
154
+ end
155
+ # end
@@ -0,0 +1,3 @@
1
+ module Cooking
2
+ VERSION = "0.0.2"
3
+ end
@@ -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 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
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'test/unit'
4
+ require 'cooking/cook'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cooking
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Mark Sadegi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-24 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: This package is originally units but since i can't find the project to fork on github, I made a gem. Also, there were a few minor bugs (incorrect conversion calculations) and I extracted only the conversions needed in the kitchen.
34
+ email:
35
+ - mark.sadegi@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - README.rdoc
47
+ - Rakefile
48
+ - cooking.gemspec
49
+ - lib/cooking.rb
50
+ - lib/cooking/base.rb
51
+ - lib/cooking/cook.rb
52
+ - 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
+ has_rdoc: true
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: cooking
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: A gem used for converting units of measurements in the kitchen
89
+ test_files:
90
+ - spec/cooking_spec.rb
91
+ - test/cooking/base_test.rb
92
+ - test/cooking/cooking_test.rb
93
+ - test/test_helper.rb