simplificator-conversions 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "pascal.betz@simplificator.com"
11
11
  gem.homepage = "http://github.com/simplficator/conversions"
12
12
  gem.authors = ["simplificator"]
13
+ gem.files << 'lib/simplificator-conversions.rb'
13
14
  gem.add_development_dependency "thoughtbot-shoulda"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.3.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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplificator-conversions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - simplificator
@@ -38,6 +38,7 @@ files:
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - VERSION
41
+ - lib/simplificator-conversions.rb
41
42
  - test/conversions_test.rb
42
43
  - test/test_helper.rb
43
44
  has_rdoc: true