seamusabshere-conversions 0.1.0 → 1.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.
data/LICENSE ADDED
@@ -0,0 +1,18 @@
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 ADDED
@@ -0,0 +1,15 @@
1
+ Conversions
2
+ -----------
3
+
4
+ A plugin to convert units.
5
+
6
+ Installation
7
+ -----------
8
+
9
+ As a GemPlugin:
10
+
11
+ config.gem 'Fingertips-conversions', :lib => 'conversions', :source => 'http://gems.github.com'
12
+
13
+ As a normal plugin:
14
+
15
+ script/install plugin git://github.com/Fingertips/conversions.git
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the conversions plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for conversions plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Conversions'
19
+ rdoc.options << '--line-numbers' << '--inline-source' << '--charset' << 'utf-8'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/TODO ADDED
File without changes
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "conversions"
3
+ s.version = "1.2"
4
+ s.date = "2009-01-12"
5
+ s.summary = "A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
6
+ s.homepage = "http://github.com/Fingertips/conversions/tree/master"
7
+ s.description = "A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
8
+ s.authors = "Fingertips"
9
+ s.email = "manfred@fngtps.com"
10
+ #s.files = Dir['[a-zA-Z]*'] + Dir['lib/**/*.rb'] + Dir['rails/*.rb'] + Dir['test/**/*']
11
+ s.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"]
12
+ s.test_file = "test/unit_test.rb"
13
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'conversions'
data/lib/conversions.rb CHANGED
@@ -1,59 +1,31 @@
1
- # Conversions makes it easy to convert between units.
2
1
  module Conversions
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
- Conversions.define_shortcut(from)
27
- Conversions.define_shortcut(to)
28
- end
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)
47
- end
48
- end
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
+ }
49
22
  end
50
23
 
51
24
  require 'conversions/unit'
25
+ require 'conversions/ext'
26
+ require 'conversions/active_record_accessors'
52
27
 
53
- Conversions.load_defaults
54
- Numeric.send(:include, Conversions::Ext)
55
-
28
+ Numeric.send :include, Conversions::Ext
56
29
  if defined?(ActiveRecord)
57
- require 'conversions/active_record_accessors'
58
- ActiveRecord::Base.send(:extend, Conversions::ActiveRecordAccessors)
59
- end
30
+ ActiveRecord::Base.send :extend, Conversions::ActiveRecordAccessors
31
+ end
@@ -1,4 +1,4 @@
1
- module Conversions
1
+ module Conversions #:nodoc:
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
38
38
  raise ArgumentError, "Please specify both :external and :internal metrics."
39
39
  end
40
40
  define_method "#{attribute}_in_#{options[:external]}" do
41
- value = send(attribute)
42
- value ? value.convert(options[:internal], options[:external], options.except(:internal, :external)) : nil
41
+ v = send(attribute)
42
+ v ? v.send(options[:internal]).to(options[:external], options[:scale]) : nil
43
43
  end
44
44
  define_method "#{attribute}_in_#{options[:external]}=" do |v|
45
- send("#{attribute}=", v.to_f.convert(options[:external], options[:internal], options.except(:internal, :external)))
45
+ send("#{attribute}=", v.to_f.send(options[:external]).to(options[:internal]))
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,10 @@
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
@@ -1,4 +1,4 @@
1
- module Conversions
1
+ module Conversions #:nodoc
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,27 +13,34 @@ module Conversions
13
13
  # Convert to a certain other unit.
14
14
  #
15
15
  # * _to_: The unit to convert to (ie. :kilometers)
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
-
16
+ # * _scale_: The number of digits behind the decimal point to you want to keep (Optional)
17
+ def to(to, scale=nil)
26
18
  value = @value * self.class.exchange_rate(@from, to)
27
19
  scale.nil? ? value : (value * (10 ** scale)).round / (10 ** scale).to_f
28
20
  end
29
21
 
30
22
  def self.exchange_rate(from_unit, to_unit) #:nodoc:
31
23
  return 1 if from_unit == to_unit
32
- from = Conversions.conversions[from_unit]
24
+ from = conversion[from_unit]
33
25
  raise ArgumentError, "Can't convert from `#{from}', unknown unit" if from.nil?
34
26
  to = from[to_unit]
35
27
  raise ArgumentError, "Can't convert from `#{from_unit}' to `#{to_unit}', unknown unit" if to.nil?
36
28
  to
37
29
  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
38
45
  end
39
46
  end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'init')
@@ -5,16 +5,10 @@ class Flight
5
5
  attr_accessor :distance, :fuel_consumption
6
6
  conversion_accessor :distance, :internal => :kilometres, :external => :miles
7
7
  conversion_accessor :fuel_consumption, :internal => :litres, :external => :gallons, :scale => 2
8
-
9
- Conversions.register(:kilometres, :leagues, 0.179985601)
10
- conversion_accessor :distance, :internal => :kilometres, :external => :leagues, :scale => 2
11
8
  end
12
9
 
13
10
  class AccessorTest < Test::Unit::TestCase
14
- def setup
15
- reset_defaults
16
- end
17
-
11
+
18
12
  def setup
19
13
  @flight = Flight.new
20
14
  end
@@ -33,9 +27,4 @@ class AccessorTest < Test::Unit::TestCase
33
27
  @flight.fuel_consumption = 3400
34
28
  assert_equal 898.18, @flight.fuel_consumption_in_gallons, DELTA
35
29
  end
36
-
37
- def test_register
38
- @flight.distance = 1200
39
- assert_in_delta 215.98, @flight.distance_in_leagues, DELTA
40
- end
41
30
  end
data/test/ext_test.rb CHANGED
@@ -1,17 +1,8 @@
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
5
  def test_conversions
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
15
6
  assert_in_delta 1.609344, 1.miles.to(:kilometres), DELTA
16
7
  assert_in_delta 1.609344, 1.0.miles.to(:kilometres), DELTA
17
8
  assert_in_delta 0.45359237, 1.pounds.to(:kilograms), DELTA
@@ -19,10 +10,4 @@ class ExtTest < Test::Unit::TestCase
19
10
  assert_in_delta 2.20462262184878, 1.kilograms.to(:pounds), DELTA
20
11
  assert_in_delta 1, ( 1.kilograms.to(:pounds) * 1.pounds.to(:kilograms) ), DELTA
21
12
  end
22
-
23
- def test_register
24
- Conversions.register(:dollars, :cents, 100.0)
25
- assert_in_delta 1000.0, 10.convert(:dollars, :cents), DELTA
26
- assert_in_delta 1000.0, 10.dollars.to(:cents), DELTA
27
- end
28
13
  end
data/test/test_helper.rb CHANGED
@@ -8,9 +8,4 @@ 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
-
16
11
  DELTA = 0.0000001
data/test/unit_test.rb CHANGED
@@ -1,10 +1,6 @@
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
-
8
4
  def test_exchange_rate
9
5
  assert_in_delta 1.609344, Conversions::Unit.exchange_rate(:miles, :kilometres), DELTA
10
6
  assert_in_delta 0.621371192237334, Conversions::Unit.exchange_rate(:kilometres, :miles), DELTA
@@ -15,7 +11,7 @@ class UnitTest < Test::Unit::TestCase
15
11
  end
16
12
 
17
13
  def test_exchange_rate_for_identity_transform
18
- Conversions.conversions.keys.each do |unit|
14
+ Conversions::Unit.conversion.keys.each do |unit|
19
15
  assert_equal 1, Conversions::Unit.exchange_rate(unit, unit)
20
16
  end
21
17
  end
@@ -24,34 +20,19 @@ class UnitTest < Test::Unit::TestCase
24
20
  amount = Conversions::Unit.new(10.0, :miles)
25
21
  assert_in_delta 16.09344, amount.to(:kilometres), DELTA
26
22
 
23
+ amount = Conversions::Unit.new(10.0, :miles)
24
+ assert_equal 16.09, amount.to(:kilometres, 2), DELTA
25
+
27
26
  amount = Conversions::Unit.new(10.0, :kilograms)
28
27
  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
37
28
 
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)
29
+ amount = Conversions::Unit.new(10.0, :kilograms)
30
+ assert_equal 22.05, amount.to(:pounds, 2), DELTA
43
31
  end
44
32
 
45
33
  def test_identity_transforms
46
- Conversions.conversions.keys.each do |unit|
47
- assert_equal 1.0, Conversions::Unit.new(1.0, unit).to(unit, :scale => 2)
34
+ Conversions::Unit.conversion.keys.each do |unit|
35
+ assert_equal 1.0, Conversions::Unit.new(1.0, unit).to(unit, 2)
48
36
  end
49
37
  end
50
-
51
- def test_register
52
- Conversions.register(:dollars, :cents, 100.0)
53
- assert_in_delta 0.01, Conversions::Unit.exchange_rate(:cents, :dollars), DELTA
54
- amount = Conversions::Unit.new(10.0, :dollars)
55
- assert_equal 1000.0, amount.to(:cents, :scale => 2), DELTA
56
- end
57
38
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seamusabshere-conversions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: "1.2"
5
5
  platform: ruby
6
6
  authors:
7
- - Manfred Stienstra
7
+ - Fingertips
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-16 00:00:00 -07:00
12
+ date: 2009-01-12 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
16
+ description: A Ruby on Rails plugin that adds conversion capabilities to numeric objects
17
17
  email: manfred@fngtps.com
18
18
  executables: []
19
19
 
@@ -22,22 +22,29 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - lib/conversions
25
+ - conversions.gemspec
26
+ - init.rb
27
+ - lib
28
+ - LICENSE
29
+ - rails
30
+ - Rakefile
31
+ - README
32
+ - test
33
+ - TODO
26
34
  - lib/conversions/active_record_accessors.rb
27
- - lib/conversions/defaults.rb
35
+ - lib/conversions/ext.rb
28
36
  - lib/conversions/unit.rb
29
37
  - lib/conversions.rb
38
+ - rails/init.rb
30
39
  - test/accessor_test.rb
31
- - test/conversions_test.rb
32
40
  - test/ext_test.rb
33
41
  - test/test_helper.rb
34
42
  - test/unit_test.rb
35
- has_rdoc: true
43
+ has_rdoc: false
36
44
  homepage: http://github.com/Fingertips/conversions/tree/master
37
45
  post_install_message:
38
- rdoc_options:
39
- - --inline-source
40
- - --charset=UTF-8
46
+ rdoc_options: []
47
+
41
48
  require_paths:
42
49
  - lib
43
50
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -58,6 +65,6 @@ rubyforge_project:
58
65
  rubygems_version: 1.2.0
59
66
  signing_key:
60
67
  specification_version: 2
61
- summary: A Ruby on Rails plugin that adds conversion capabilities to numeric objects"
62
- test_files: []
63
-
68
+ summary: A Ruby on Rails plugin that adds conversion capabilities to numeric objects
69
+ test_files:
70
+ - test/unit_test.rb
@@ -1,27 +0,0 @@
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,26 +0,0 @@
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