Fingertips-conversions 1.2.1 → 1.3.0

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/conversions.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "conversions"
3
- spec.version = "1.2.1"
3
+ spec.version = "1.3.0"
4
4
 
5
5
  spec.author = "Manfred Stienstra"
6
6
  spec.email = "manfred@fngtps.com"
@@ -13,8 +13,8 @@ Gem::Specification.new do |spec|
13
13
  EOF
14
14
  spec.homepage = "http://github.com/Fingertips/conversions/tree/master"
15
15
 
16
- spec.files = ["conversions.gemspec", "init.rb", "lib", "LICENSE", "rails", "Rakefile", "README", "test", "TODO", "lib/conversions/active_record_accessors.rb", "lib/conversions/ext.rb", "lib/conversions/unit.rb", "lib/conversions.rb", "rails/init.rb", "test/accessor_test.rb", "test/ext_test.rb", "test/test_helper.rb", "test/unit_test.rb"]
17
- spec.test_files = ["test/unit_test.rb", "test/accessor_test.rb", "test/ext_test.rb", "test/test_helper.rb"]
16
+ spec.files = ["conversions.gemspec", "init.rb", "lib", "LICENSE", "rails", "Rakefile", "README", "test", "TODO", "lib/conversions/active_record_accessors.rb", "lib/conversions/defaults.rb", "lib/conversions/unit.rb", "lib/conversions.rb", "rails/init.rb", "test/accessor_test.rb", "test/ext_test.rb", "test/test_helper.rb", "test/unit_test.rb", "test/conversions_test.rb"]
17
+ spec.test_files = ["test/unit_test.rb", "test/accessor_test.rb", "test/ext_test.rb", "test/test_helper.rb", "test/conversions_test.rb"]
18
18
 
19
19
  spec.has_rdoc = true
20
20
  spec.extra_rdoc_files = ['README', 'LICENSE']
data/lib/conversions.rb CHANGED
@@ -1,31 +1,49 @@
1
+ # Conversions makes it easy to convert between units.
1
2
  module Conversions
2
- CONVERSION = {
3
- :miles => {
4
- :kilometres => 1.609344
5
- },
6
- :kilograms => {
7
- :grams => 1000.0,
8
- :pounds => 2.20462262,
9
- :short_tons => 0.00110231131,
10
- :tons => 0.00110231131
11
- },
12
- :tons => {
13
- :pounds => 2000.0
14
- },
15
- :gallons => {
16
- :litres => 3.7854118
17
- },
18
- :cubic_feet => {
19
- :cubic_meters => 0.0283168466
20
- }
21
- }
3
+ mattr_accessor :conversions
4
+
5
+ # Clear all previously registered conversions
6
+ def self.clear
7
+ self.conversions = {}
8
+ end
9
+ clear
10
+
11
+ # Load all the default conversions shipped with the code
12
+ def self.load_defaults
13
+ load File.expand_path('../conversions/defaults.rb', __FILE__)
14
+ end
15
+
16
+ # Register a new conversion. This automatically also registers the inverse conversion.
17
+ #
18
+ # * _from_: The unit to convert from (ie. :miles, :stones, or :pints)
19
+ # * _to_: The unit to convert to
20
+ # * _rate_: The conversion rate from _from_ to _to_. (_from_ * _rate_ = _to_)
21
+ def self.register(from, to, rate)
22
+ conversions[from] ||= {}
23
+ conversions[from][to] = rate
24
+ conversions[to] ||= {}
25
+ conversions[to][from] = 1.0 / rate
26
+ end
27
+
28
+ module Ext
29
+ # Convert from one unit to another.
30
+ #
31
+ # * _from_: The unit to convert from (ie. :miles, :stones, or :pints)
32
+ # * _to_: The unit to convert to
33
+ # * _options_:
34
+ # * :scale: The number of digits you want after the dot.
35
+ def convert(from, to, options={})
36
+ Conversions::Unit.new(self, from).to(to, options)
37
+ end
38
+ end
22
39
  end
23
40
 
24
41
  require 'conversions/unit'
25
- require 'conversions/ext'
26
- require 'conversions/active_record_accessors'
27
42
 
28
- Numeric.send :include, Conversions::Ext
43
+ Conversions.load_defaults
44
+ Numeric.send(:include, Conversions::Ext)
45
+
29
46
  if defined?(ActiveRecord)
30
- ActiveRecord::Base.send :extend, Conversions::ActiveRecordAccessors
31
- end
47
+ require 'conversions/active_record_accessors'
48
+ ActiveRecord::Base.send(:extend, Conversions::ActiveRecordAccessors)
49
+ end
@@ -1,4 +1,4 @@
1
- module Conversions #:nodoc:
1
+ module Conversions
2
2
  # Implements new accessor classmethods to define conversion accessors on active record classes.
3
3
  module ActiveRecordAccessors
4
4
  # Adds conversion methods to the model for a certain attribute.
@@ -38,11 +38,11 @@ module Conversions #:nodoc:
38
38
  raise ArgumentError, "Please specify both :external and :internal metrics."
39
39
  end
40
40
  define_method "#{attribute}_in_#{options[:external]}" do
41
- v = send(attribute)
42
- v ? v.send(options[:internal]).to(options[:external], options[:scale]) : nil
41
+ value = send(attribute)
42
+ value ? value.convert(options[:internal], options[:external], options.except(:internal, :external)) : nil
43
43
  end
44
44
  define_method "#{attribute}_in_#{options[:external]}=" do |v|
45
- send("#{attribute}=", v.to_f.send(options[:external]).to(options[:internal]))
45
+ send("#{attribute}=", v.to_f.convert(options[:external], options[:internal]), options.except(:internal, :external))
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,27 @@
1
+ {
2
+ :miles => {
3
+ :kilometres => 1.609344
4
+ },
5
+ :kilograms => {
6
+ :grams => 1000.0,
7
+ :pounds => 2.20462262,
8
+ :short_tons => 0.00110231131,
9
+ :tons => 0.00110231131
10
+ },
11
+ :tons => {
12
+ :pounds => 2000.0
13
+ },
14
+ :gallons => {
15
+ :litres => 3.7854118
16
+ },
17
+ :cubic_feet => {
18
+ :cubic_meters => 0.0283168466
19
+ },
20
+ :miles_per_gallon => {
21
+ :kilometres_per_litre => 0.425143707
22
+ }
23
+ }.each do |from_unit, to_units|
24
+ to_units.each do |to_unit, rate|
25
+ Conversions.register(from_unit, to_unit, rate)
26
+ end
27
+ end
@@ -1,4 +1,4 @@
1
- module Conversions #:nodoc
1
+ module Conversions
2
2
  # Proxy class to contain the unit as well as reference the base value
3
3
  class Unit
4
4
  # Create a new Unit instance.
@@ -13,34 +13,20 @@ module Conversions #:nodoc
13
13
  # Convert to a certain other unit.
14
14
  #
15
15
  # * _to_: The unit to convert to (ie. :kilometers)
16
- # * _scale_: The number of digits behind the decimal point to you want to keep (Optional)
17
- def to(to, scale=nil)
16
+ # * _options_:
17
+ # * :scale: The number of digits behind the decimal point to you want to keep
18
+ def to(to, options={})
18
19
  value = @value * self.class.exchange_rate(@from, to)
19
- scale.nil? ? value : (value * (10 ** scale)).round / (10 ** scale).to_f
20
+ options[:scale].nil? ? value : (value * (10 ** options[:scale])).round / (10 ** options[:scale]).to_f
20
21
  end
21
22
 
22
23
  def self.exchange_rate(from_unit, to_unit) #:nodoc:
23
24
  return 1 if from_unit == to_unit
24
- from = conversion[from_unit]
25
+ from = Conversions.conversions[from_unit]
25
26
  raise ArgumentError, "Can't convert from `#{from}', unknown unit" if from.nil?
26
27
  to = from[to_unit]
27
28
  raise ArgumentError, "Can't convert from `#{from_unit}' to `#{to_unit}', unknown unit" if to.nil?
28
29
  to
29
30
  end
30
-
31
- def self.conversion #:nodoc:
32
- if !defined? @@conversion
33
- @@conversion = {}
34
- CONVERSION.each do |from, conversion|
35
- conversion.each do |to, value|
36
- @@conversion[from] ||= {}
37
- @@conversion[from][to] = value
38
- @@conversion[to] ||= {}
39
- @@conversion[to][from] = 1.0 / value
40
- end
41
- end
42
- end
43
- @@conversion
44
- end
45
31
  end
46
32
  end
@@ -8,7 +8,10 @@ class Flight
8
8
  end
9
9
 
10
10
  class AccessorTest < Test::Unit::TestCase
11
-
11
+ def setup
12
+ reset_defaults
13
+ end
14
+
12
15
  def setup
13
16
  @flight = Flight.new
14
17
  end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ConversionsTest < Test::Unit::TestCase
4
+ def setup
5
+ Conversions.clear
6
+ end
7
+
8
+ def test_register
9
+ Conversions.register(:records, :cds, 0.47)
10
+ assert 2, Conversions.conversions.length
11
+
12
+ assert_nothing_raised do
13
+ 1.convert(:records, :cds)
14
+ 1.convert(:cds, :records)
15
+ end
16
+ end
17
+
18
+ def test_clear
19
+ Conversions.register(:records, :cds, 0.47)
20
+ assert 2, Conversions.conversions.length
21
+ Conversions.clear
22
+ assert 0, Conversions.conversions.length
23
+ end
24
+ end
data/test/ext_test.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class ExtTest < Test::Unit::TestCase
4
+ def setup
5
+ reset_defaults
6
+ end
4
7
 
5
8
  def test_conversions
6
- assert_in_delta 1.609344, 1.miles.to(:kilometres), DELTA
7
- assert_in_delta 1.609344, 1.0.miles.to(:kilometres), DELTA
8
- assert_in_delta 0.45359237, 1.pounds.to(:kilograms), DELTA
9
- assert_in_delta 0.00110231131092439, 1.kilograms.to(:tons), DELTA
10
- assert_in_delta 2.20462262184878, 1.kilograms.to(:pounds), DELTA
11
- assert_in_delta 1, ( 1.kilograms.to(:pounds) * 1.pounds.to(:kilograms) ), DELTA
9
+ assert_in_delta 1.609344, 1.convert(:miles, :kilometres), DELTA
10
+ assert_in_delta 1.609344, 1.0.convert(:miles, :kilometres), DELTA
11
+ assert_in_delta 0.45359237, 1.convert(:pounds, :kilograms), DELTA
12
+ assert_in_delta 0.00110231131092439, 1.convert(:kilograms, :tons), DELTA
13
+ assert_in_delta 2.20462262184878, 1.convert(:kilograms, :pounds), DELTA
14
+ assert_in_delta 1, ( 1.convert(:kilograms, :pounds) * 1.convert(:pounds, :kilograms) ), DELTA
12
15
  end
13
16
  end
data/test/test_helper.rb CHANGED
@@ -8,4 +8,9 @@ end
8
8
  $:.unshift File.dirname(__FILE__) + '/../lib'
9
9
  require 'conversions'
10
10
 
11
+ def reset_defaults
12
+ Conversions.clear
13
+ Conversions.load_defaults
14
+ end
15
+
11
16
  DELTA = 0.0000001
data/test/unit_test.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class UnitTest < Test::Unit::TestCase
4
+ def setup
5
+ reset_defaults
6
+ end
7
+
4
8
  def test_exchange_rate
5
9
  assert_in_delta 1.609344, Conversions::Unit.exchange_rate(:miles, :kilometres), DELTA
6
10
  assert_in_delta 0.621371192237334, Conversions::Unit.exchange_rate(:kilometres, :miles), DELTA
@@ -11,7 +15,7 @@ class UnitTest < Test::Unit::TestCase
11
15
  end
12
16
 
13
17
  def test_exchange_rate_for_identity_transform
14
- Conversions::Unit.conversion.keys.each do |unit|
18
+ Conversions.conversions.keys.each do |unit|
15
19
  assert_equal 1, Conversions::Unit.exchange_rate(unit, unit)
16
20
  end
17
21
  end
@@ -20,19 +24,20 @@ class UnitTest < Test::Unit::TestCase
20
24
  amount = Conversions::Unit.new(10.0, :miles)
21
25
  assert_in_delta 16.09344, amount.to(:kilometres), DELTA
22
26
 
23
- amount = Conversions::Unit.new(10.0, :miles)
24
- assert_equal 16.09, amount.to(:kilometres, 2), DELTA
25
-
26
27
  amount = Conversions::Unit.new(10.0, :kilograms)
27
28
  assert_in_delta 22.0462262184878, amount.to(:pounds), DELTA
28
-
29
- amount = Conversions::Unit.new(10.0, :kilograms)
30
- assert_equal 22.05, amount.to(:pounds, 2), DELTA
29
+ end
30
+
31
+ def test_to_with_scale
32
+ amount = Conversions::Unit.new(10.0, :miles)
33
+ assert_equal 16.1, amount.to(:kilometres, :scale => 1)
34
+ assert_equal 16.09, amount.to(:kilometres, :scale => 2)
35
+ assert_equal 16.093, amount.to(:kilometres, :scale => 3)
31
36
  end
32
37
 
33
38
  def test_identity_transforms
34
- Conversions::Unit.conversion.keys.each do |unit|
35
- assert_equal 1.0, Conversions::Unit.new(1.0, unit).to(unit, 2)
39
+ Conversions.conversions.keys.each do |unit|
40
+ assert_equal 1.0, Conversions::Unit.new(1.0, unit).to(unit, :scale => 2)
36
41
  end
37
42
  end
38
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Fingertips-conversions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manfred Stienstra
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-11 00:00:00 -08:00
12
+ date: 2009-02-13 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,7 +33,7 @@ files:
33
33
  - test
34
34
  - TODO
35
35
  - lib/conversions/active_record_accessors.rb
36
- - lib/conversions/ext.rb
36
+ - lib/conversions/defaults.rb
37
37
  - lib/conversions/unit.rb
38
38
  - lib/conversions.rb
39
39
  - rails/init.rb
@@ -41,6 +41,7 @@ files:
41
41
  - test/ext_test.rb
42
42
  - test/test_helper.rb
43
43
  - test/unit_test.rb
44
+ - test/conversions_test.rb
44
45
  has_rdoc: true
45
46
  homepage: http://github.com/Fingertips/conversions/tree/master
46
47
  post_install_message:
@@ -72,3 +73,4 @@ test_files:
72
73
  - test/accessor_test.rb
73
74
  - test/ext_test.rb
74
75
  - test/test_helper.rb
76
+ - test/conversions_test.rb
@@ -1,10 +0,0 @@
1
- module Conversions #:nodoc:
2
- # Defines all the conversion methods (miles, kilometers, etc…)
3
- module Ext
4
- Unit.conversion.each do |method, _|
5
- define_method method do
6
- Unit.new self, method
7
- end unless respond_to? method
8
- end
9
- end
10
- end