seamusabshere-conversions 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 4
4
+ :patch: 0
@@ -1,51 +1,59 @@
1
+ # Conversions makes it easy to convert between units.
1
2
  module Conversions
2
3
  mattr_accessor :conversions
3
- self.conversions = {}
4
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_)
5
21
  def self.register(from, to, rate)
6
22
  conversions[from] ||= {}
7
- conversions[from][to] ||= {}
8
23
  conversions[from][to] = rate
9
24
  conversions[to] ||= {}
10
- conversions[to][from] ||= {}
11
- conversions[to][from] = 1 / rate
25
+ conversions[to][from] = 1.0 / rate
26
+ Conversions.define_shortcut(from)
27
+ Conversions.define_shortcut(to)
12
28
  end
13
-
14
- # This is ugly
15
- {
16
- :miles => {
17
- :kilometres => 1.609344
18
- },
19
- :kilograms => {
20
- :grams => 1000.0,
21
- :pounds => 2.20462262,
22
- :short_tons => 0.00110231131,
23
- :tons => 0.00110231131
24
- },
25
- :tons => {
26
- :pounds => 2000.0
27
- },
28
- :gallons => {
29
- :litres => 3.7854118
30
- },
31
- :cubic_feet => {
32
- :cubic_meters => 0.0283168466
33
- },
34
- :miles_per_gallon => {
35
- :kilometres_per_litre => 0.425143707
36
- }
37
- }.each do |from_unit, to_units|
38
- to_units.each do |to_unit, rate|
39
- register(from_unit, to_unit, rate)
29
+
30
+ def self.define_shortcut(unit)
31
+ Numeric.class_eval do
32
+ define_method unit do
33
+ Conversions::Unit.new(self, unit)
34
+ end unless respond_to? unit
35
+ end
36
+ end
37
+
38
+ module Ext
39
+ # Convert from one unit to another.
40
+ #
41
+ # * _from_: The unit to convert from (ie. :miles, :stones, or :pints)
42
+ # * _to_: The unit to convert to
43
+ # * _options_:
44
+ # * :scale: The number of digits you want after the dot.
45
+ def convert(from, to, options={})
46
+ Conversions::Unit.new(self, from).to(to, options)
40
47
  end
41
48
  end
42
49
  end
43
50
 
44
51
  require 'conversions/unit'
45
- require 'conversions/ext'
46
- require 'conversions/active_record_accessors'
47
52
 
48
- Numeric.send :include, Conversions::Ext
53
+ Conversions.load_defaults
54
+ Numeric.send(:include, Conversions::Ext)
55
+
49
56
  if defined?(ActiveRecord)
50
- ActiveRecord::Base.send :extend, Conversions::ActiveRecordAccessors
51
- end
57
+ require 'conversions/active_record_accessors'
58
+ ActiveRecord::Base.send(:extend, Conversions::ActiveRecordAccessors)
59
+ 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(:convert, options[:internal], options[:external], :scale => 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(:convert, options[:external], 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,9 +13,16 @@ 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, options = {})
18
- scale = options.delete(:scale)
16
+ # * _options_:
17
+ # * :scale: The number of digits behind the decimal point to you want to keep
18
+ def to(to, options={})
19
+ case options
20
+ when Integer
21
+ scale = options
22
+ when Hash
23
+ scale = options[:scale]
24
+ end
25
+
19
26
  value = @value * self.class.exchange_rate(@from, to)
20
27
  scale.nil? ? value : (value * (10 ** scale)).round / (10 ** scale).to_f
21
28
  end
@@ -11,7 +11,10 @@ class Flight
11
11
  end
12
12
 
13
13
  class AccessorTest < Test::Unit::TestCase
14
-
14
+ def setup
15
+ reset_defaults
16
+ end
17
+
15
18
  def setup
16
19
  @flight = Flight.new
17
20
  end
@@ -0,0 +1,26 @@
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
+ 1.records.to(:cds)
16
+ 1.cds.to(:records)
17
+ end
18
+ end
19
+
20
+ def test_clear
21
+ Conversions.register(:records, :cds, 0.47)
22
+ assert 2, Conversions.conversions.length
23
+ Conversions.clear
24
+ assert 0, Conversions.conversions.length
25
+ end
26
+ end
@@ -1,6 +1,10 @@
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
7
+
4
8
  def test_conversions
5
9
  assert_in_delta 1.609344, 1.convert(:miles, :kilometres), DELTA
6
10
  assert_in_delta 1.609344, 1.0.convert(:miles, :kilometres), DELTA
@@ -8,10 +12,17 @@ class ExtTest < Test::Unit::TestCase
8
12
  assert_in_delta 0.00110231131092439, 1.convert(:kilograms, :tons), DELTA
9
13
  assert_in_delta 2.20462262184878, 1.convert(:kilograms, :pounds), DELTA
10
14
  assert_in_delta 1, ( 1.convert(:kilograms, :pounds) * 1.convert(:pounds, :kilograms) ), DELTA
15
+ assert_in_delta 1.609344, 1.miles.to(:kilometres), DELTA
16
+ assert_in_delta 1.609344, 1.0.miles.to(:kilometres), DELTA
17
+ assert_in_delta 0.45359237, 1.pounds.to(:kilograms), DELTA
18
+ assert_in_delta 0.00110231131092439, 1.kilograms.to(:tons), DELTA
19
+ assert_in_delta 2.20462262184878, 1.kilograms.to(:pounds), DELTA
20
+ assert_in_delta 1, ( 1.kilograms.to(:pounds) * 1.pounds.to(:kilograms) ), DELTA
11
21
  end
12
22
 
13
23
  def test_register
14
24
  Conversions.register(:dollars, :cents, 100.0)
15
25
  assert_in_delta 1000.0, 10.convert(:dollars, :cents), DELTA
26
+ assert_in_delta 1000.0, 10.dollars.to(:cents), DELTA
16
27
  end
17
28
  end
@@ -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
@@ -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
@@ -20,21 +24,26 @@ 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, :scale => 2), DELTA
25
-
26
27
  amount = Conversions::Unit.new(10.0, :kilograms)
27
28
  assert_in_delta 22.0462262184878, amount.to(:pounds), DELTA
29
+ end
30
+
31
+ def test_to_with_options
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)
36
+ end
28
37
 
29
- amount = Conversions::Unit.new(10.0, :kilograms)
30
- assert_equal 22.05, amount.to(:pounds, :scale => 2), DELTA
31
-
32
- amount = Conversions::Unit.new(10.0, :miles_per_gallon)
33
- assert_equal 4.25, amount.to(:kilometres_per_litre, :scale => 2), DELTA
38
+ def test_to_with_scale
39
+ amount = Conversions::Unit.new(10.0, :miles)
40
+ assert_equal 16.1, amount.to(:kilometres, 1)
41
+ assert_equal 16.09, amount.to(:kilometres, 2)
42
+ assert_equal 16.093, amount.to(:kilometres, 3)
34
43
  end
35
44
 
36
45
  def test_identity_transforms
37
- Conversions.conversions.each do |unit|
46
+ Conversions.conversions.keys.each do |unit|
38
47
  assert_equal 1.0, Conversions::Unit.new(1.0, unit).to(unit, :scale => 2)
39
48
  end
40
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seamusabshere-conversions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.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-03-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -19,25 +19,17 @@ executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files:
23
- - README
24
- - LICENSE
22
+ extra_rdoc_files: []
23
+
25
24
  files:
26
- - conversions.gemspec
27
- - init.rb
28
- - lib
29
- - LICENSE
30
- - rails
31
- - Rakefile
32
- - README
33
- - test
34
- - TODO
25
+ - VERSION.yml
26
+ - lib/conversions
35
27
  - lib/conversions/active_record_accessors.rb
36
- - lib/conversions/ext.rb
28
+ - lib/conversions/defaults.rb
37
29
  - lib/conversions/unit.rb
38
30
  - lib/conversions.rb
39
- - rails/init.rb
40
31
  - test/accessor_test.rb
32
+ - test/conversions_test.rb
41
33
  - test/ext_test.rb
42
34
  - test/test_helper.rb
43
35
  - test/unit_test.rb
@@ -45,7 +37,8 @@ has_rdoc: true
45
37
  homepage: http://github.com/Fingertips/conversions/tree/master
46
38
  post_install_message:
47
39
  rdoc_options:
48
- - --charset=utf-8
40
+ - --inline-source
41
+ - --charset=UTF-8
49
42
  require_paths:
50
43
  - lib
51
44
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -67,8 +60,5 @@ rubygems_version: 1.2.0
67
60
  signing_key:
68
61
  specification_version: 2
69
62
  summary: A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
70
- test_files:
71
- - test/unit_test.rb
72
- - test/accessor_test.rb
73
- - test/ext_test.rb
74
- - test/test_helper.rb
63
+ test_files: []
64
+
data/LICENSE DELETED
@@ -1,18 +0,0 @@
1
- Copyright (c) 2008 Manfred Stienstra, Fingertips <manfred@fngtps.com>
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- this software and associated documentation files (the "Software"), to deal in
5
- the Software without restriction, including without limitation the rights to
6
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
- the Software, and to permit persons to whom the Software is furnished to do so,
8
- subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all
11
- copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README DELETED
@@ -1,32 +0,0 @@
1
- = Conversions
2
-
3
- The conversions plugin does a number of things. The core functionality is the unit conversion:
4
-
5
- 1.miles.to(:kilometres) #=> 1.609344
6
- 1.pounds.to(:kilograms) #=> 0.453592
7
-
8
- It also adds a class method to ActiveRecord::Base that allows you to define conversion methods for attributes:
9
-
10
- class Car < ActiveRecord::Base
11
- conversion_accessor :weight, :internal => :kilograms, :external => :pounds
12
- end
13
-
14
- car = Car.new(:weight => 1500)
15
- car.weight_in_pounds #=> 3306.93393
16
-
17
-
18
- == Installation
19
-
20
- === As a gem
21
-
22
- Configure the gem in environment.rb:
23
-
24
- config.gem 'Fingertips-conversions', :lib => 'conversions', :source => 'http://gems.github.com'
25
-
26
- Install them using Rails' rake task:
27
-
28
- $ rake gems:install
29
-
30
- === In your vendor directory:
31
-
32
- script/install plugin git://github.com/Fingertips/conversions.git
data/Rakefile DELETED
@@ -1,34 +0,0 @@
1
- require 'rake/testtask'
2
- require 'rake/rdoctask'
3
-
4
- desc 'Default: run unit tests.'
5
- task :default => :test
6
-
7
- desc 'Test the conversions plugin.'
8
- Rake::TestTask.new(:test) do |t|
9
- t.libs << 'lib'
10
- t.pattern = 'test/**/*_test.rb'
11
- t.verbose = true
12
- end
13
-
14
- namespace :rdoc do
15
- desc 'Generate documentation for conversions plugin.'
16
- Rake::RDocTask.new(:generate) do |rdoc|
17
- rdoc.rdoc_dir = 'documentation'
18
- rdoc.title = 'Conversions'
19
- rdoc.options << '--line-numbers' << '--inline-source' << '--charset' << 'utf-8'
20
- rdoc.rdoc_files.include('README', 'lib/**/*.rb')
21
- end
22
- end
23
-
24
- namespace :gem do
25
- desc "Build the gem"
26
- task :build do
27
- sh 'gem build conversions.gemspec'
28
- end
29
-
30
- desc "Install the gem"
31
- task :install => :build do
32
- sh 'sudo gem install conversions-*.gem'
33
- end
34
- end
data/TODO DELETED
File without changes
@@ -1,22 +0,0 @@
1
- Gem::Specification.new do |spec|
2
- spec.name = "conversions"
3
- spec.version = "1.3.0"
4
-
5
- spec.author = "Manfred Stienstra"
6
- spec.email = "manfred@fngtps.com"
7
-
8
- spec.description = <<-EOF
9
- A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
10
- EOF
11
- spec.summary = <<-EOF
12
- A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
13
- EOF
14
- spec.homepage = "http://github.com/Fingertips/conversions/tree/master"
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"]
18
-
19
- spec.has_rdoc = true
20
- spec.extra_rdoc_files = ['README', 'LICENSE']
21
- spec.rdoc_options << "--charset=utf-8"
22
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'conversions'
@@ -1,7 +0,0 @@
1
- module Conversions #:nodoc:
2
- module Ext
3
- def convert(from, to, options={})
4
- Unit.new(self, from).to(to, options)
5
- end
6
- end
7
- end
@@ -1 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'init')