conversions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 pascalbetz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = conversions
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 simplificator. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "conversions"
8
+ gem.summary = %Q{adds conversion methods to numbers}
9
+ gem.description = %Q{adds conversion methods to numbers}
10
+ gem.email = "pascal.betz@simplificator.com"
11
+ gem.homepage = "http://github.com/simplficator/conversions"
12
+ gem.authors = ["simplificator"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "conversions #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,46 @@
1
+ module UnitConversions
2
+ UNITS = ['kg', 'g', 't', 'mm2', 'm2']
3
+ CONVERT_TO_PATTERN = /(#{UNITS.join('|')})_to_(#{UNITS.join('|')})/
4
+ CONVERSIONS = {
5
+ 'kg' => {
6
+ 't' => lambda() {|kg| kg / 1000.0 },
7
+ 'g' => lambda() {|kg| kg * 1000 },
8
+ },
9
+ 'g' => {
10
+ 'kg' => lambda() {|g| g / 1000.0},
11
+ 't' => lambda() {|g| g / (1000.0 * 1000) },
12
+ },
13
+ 't' => {
14
+ 'kg' => lambda() {|t| t * 1000},
15
+ 'g' => lambda() {|t| t * (1000 * 1000)},
16
+ },
17
+ 'm2' => {
18
+ 'mm2' => lambda() {|m2| m2 * (1000 * 1000.0)},
19
+ },
20
+ 'mm2' => {
21
+ 'm2' => lambda() {|mm2| mm2 / (1000 * 1000.0)},
22
+ },
23
+ }
24
+
25
+
26
+ def method_missing_with_unit_conversions(m, *args)
27
+ if args.length == 0
28
+ m.to_s =~ CONVERT_TO_PATTERN
29
+ return CONVERSIONS[$1][$2].call(self) if CONVERSIONS[$1] && CONVERSIONS[$1][$2]
30
+ end
31
+ method_missing_without_unit_conversions(m, args)
32
+ end
33
+
34
+ end
35
+
36
+ class Fixnum
37
+ include UnitConversions
38
+ alias :method_missing_without_unit_conversions :method_missing
39
+ alias :method_missing :method_missing_with_unit_conversions
40
+ end
41
+
42
+ class Float
43
+ include UnitConversions
44
+ alias :method_missing_without_unit_conversions :method_missing
45
+ alias :method_missing :method_missing_with_unit_conversions
46
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class ConversionsTest < Test::Unit::TestCase
4
+ should 'convert from grams to tons' do
5
+ assert_equal 1, 1000000.g_to_t
6
+ assert_equal 1.234567, 1234567.g_to_t
7
+ assert_equal 0.001, 1000.g_to_t
8
+ assert_equal 0.000001, 1.g_to_t
9
+ end
10
+
11
+ should 'convert from grams to kg' do
12
+ assert_equal 1, 1000.g_to_kg
13
+ assert_equal 0.001, 1.g_to_kg
14
+ end
15
+
16
+ should 'convert from t to grams' do
17
+ assert_equal 1000000, 1.t_to_g
18
+ assert_equal 1234, (0.001234).t_to_g
19
+ end
20
+
21
+ should 'convert from kg to grams' do
22
+ assert_equal 1000, 1.kg_to_g
23
+ assert_equal 12, 0.012.kg_to_g
24
+ end
25
+
26
+ should 'convert from mm2 to m2' do
27
+ assert_equal 1, (1000 * 1000).mm2_to_m2
28
+ assert_equal 0.025, (100 * 250).mm2_to_m2
29
+ end
30
+
31
+ should 'convert from m2 to mm2' do
32
+ assert_equal 1000, (0.01 * 0.1).m2_to_mm2
33
+ end
34
+
35
+
36
+ should 'raise if source or target unit can not be found' do
37
+ assert_raises(NoMethodError) do
38
+ 1000.kg_to_m
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'conversions'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conversions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - simplificator
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-28 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: adds conversion methods to numbers
26
+ email: pascal.betz@simplificator.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/conversions.rb
42
+ - test/conversions_test.rb
43
+ - test/test_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/simplficator/conversions
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: adds conversion methods to numbers
72
+ test_files:
73
+ - test/conversions_test.rb
74
+ - test/test_helper.rb