conversions 0.1.0 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -5
- data/LICENSE +15 -17
- data/README +44 -0
- data/Rakefile +13 -9
- data/TODO +0 -0
- data/VERSION.yml +5 -0
- data/conversions.gemspec +63 -0
- data/init.rb +1 -0
- data/lib/conversions.rb +52 -39
- data/lib/conversions/active_record_accessors.rb +49 -0
- data/lib/conversions/defaults.rb +27 -0
- data/lib/conversions/unit.rb +47 -0
- data/rails/init.rb +1 -0
- data/test/accessor_test.rb +59 -0
- data/test/conversions_test.rb +19 -34
- data/test/ext_test.rb +28 -0
- data/test/test_helper.rb +11 -5
- data/test/unit_test.rb +63 -0
- metadata +29 -23
- data/.document +0 -5
- data/README.rdoc +0 -18
- data/VERSION +0 -1
data/.gitignore
CHANGED
data/LICENSE
CHANGED
@@ -1,20 +1,18 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2008 Manfred Stienstra, Fingertips <manfred@fngtps.com>
|
2
2
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
the following conditions:
|
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:
|
10
9
|
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
13
12
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
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,44 @@
|
|
1
|
+
= Conversions
|
2
|
+
|
3
|
+
Unit conversion:
|
4
|
+
|
5
|
+
1.miles.to(:kilometres) #=> 1.609344
|
6
|
+
1.pounds.to(:kilograms) #=> 0.453592
|
7
|
+
1.pounds.to(:kilograms, 2) #=> 0.45
|
8
|
+
|
9
|
+
Or, more explicitly,
|
10
|
+
|
11
|
+
1.convert(:miles, :kilometres) #=> 1.609344
|
12
|
+
1.convert(:pounds, :kilograms) #=> 0.453592
|
13
|
+
1.convert(:pounds, :kilograms, :scale => 2) #=> 0.45
|
14
|
+
|
15
|
+
You can register your own conversions:
|
16
|
+
|
17
|
+
Conversions.register(:miles, :nautical_miles, 0.868976242)
|
18
|
+
1.miles.to(:nautical_miles) #=> 0.868976242
|
19
|
+
|
20
|
+
It also adds a class method to ActiveRecord::Base that allows you to define conversion methods for attributes:
|
21
|
+
|
22
|
+
class Car < ActiveRecord::Base
|
23
|
+
conversion_accessor :weight, :internal => :kilograms, :external => :pounds
|
24
|
+
end
|
25
|
+
|
26
|
+
car = Car.new(:weight => 1500)
|
27
|
+
car.weight_in_pounds #=> 3306.93393
|
28
|
+
car.weight_in_pounds = 3306 #=> 3306
|
29
|
+
|
30
|
+
== Installation
|
31
|
+
|
32
|
+
=== As a gem
|
33
|
+
|
34
|
+
Configure the gem in environment.rb:
|
35
|
+
|
36
|
+
config.gem 'Fingertips-conversions', :lib => 'conversions', :source => 'http://gems.github.com'
|
37
|
+
|
38
|
+
Install them using Rails' rake task:
|
39
|
+
|
40
|
+
$ rake gems:install
|
41
|
+
|
42
|
+
=== In your vendor directory:
|
43
|
+
|
44
|
+
script/install plugin git://github.com/Fingertips/conversions.git
|
data/Rakefile
CHANGED
@@ -5,14 +5,20 @@ begin
|
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "conversions"
|
8
|
-
gem.summary = %Q{adds conversion
|
9
|
-
gem.description = %Q{adds conversion
|
10
|
-
gem.email = "
|
11
|
-
gem.homepage = "http://github.com/
|
12
|
-
gem.authors = ["
|
13
|
-
gem.
|
8
|
+
gem.summary = %Q{A Ruby on Rails plugin that adds conversion capabilities to numeric objects}
|
9
|
+
gem.description = %Q{A Ruby on Rails plugin that adds conversion capabilities to numeric objects}
|
10
|
+
gem.email = "seamus@abshere.net"
|
11
|
+
gem.homepage = "http://github.com/seamusabshere/conversions"
|
12
|
+
gem.authors = ["Manfred Stienstra", "Seamus Abshere"]
|
13
|
+
gem.require_path = "lib"
|
14
|
+
gem.rdoc_options << '--line-numbers' << '--inline-source'
|
15
|
+
gem.rubyforge_project = "conversions"
|
14
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
17
|
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
20
|
+
rubyforge.doc_task = "rdoc"
|
21
|
+
end
|
16
22
|
rescue LoadError
|
17
23
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
24
|
end
|
@@ -37,8 +43,6 @@ rescue LoadError
|
|
37
43
|
end
|
38
44
|
end
|
39
45
|
|
40
|
-
task :test => :check_dependencies
|
41
|
-
|
42
46
|
task :default => :test
|
43
47
|
|
44
48
|
require 'rake/rdoctask'
|
@@ -53,4 +57,4 @@ Rake::RDocTask.new do |rdoc|
|
|
53
57
|
rdoc.title = "conversions #{version}"
|
54
58
|
rdoc.rdoc_files.include('README*')
|
55
59
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
-
end
|
60
|
+
end
|
data/TODO
ADDED
File without changes
|
data/VERSION.yml
ADDED
data/conversions.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{conversions}
|
8
|
+
s.version = "1.4.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Manfred Stienstra", "Seamus Abshere"]
|
12
|
+
s.date = %q{2009-11-02}
|
13
|
+
s.description = %q{A Ruby on Rails plugin that adds conversion capabilities to numeric objects}
|
14
|
+
s.email = %q{seamus@abshere.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"TODO",
|
25
|
+
"VERSION.yml",
|
26
|
+
"conversions.gemspec",
|
27
|
+
"init.rb",
|
28
|
+
"lib/conversions.rb",
|
29
|
+
"lib/conversions/active_record_accessors.rb",
|
30
|
+
"lib/conversions/defaults.rb",
|
31
|
+
"lib/conversions/unit.rb",
|
32
|
+
"rails/init.rb",
|
33
|
+
"test/accessor_test.rb",
|
34
|
+
"test/conversions_test.rb",
|
35
|
+
"test/ext_test.rb",
|
36
|
+
"test/test_helper.rb",
|
37
|
+
"test/unit_test.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/seamusabshere/conversions}
|
40
|
+
s.rdoc_options = ["--charset=UTF-8", "--line-numbers", "--inline-source"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubyforge_project = %q{conversions}
|
43
|
+
s.rubygems_version = %q{1.3.5}
|
44
|
+
s.summary = %q{A Ruby on Rails plugin that adds conversion capabilities to numeric objects}
|
45
|
+
s.test_files = [
|
46
|
+
"test/accessor_test.rb",
|
47
|
+
"test/conversions_test.rb",
|
48
|
+
"test/ext_test.rb",
|
49
|
+
"test/test_helper.rb",
|
50
|
+
"test/unit_test.rb"
|
51
|
+
]
|
52
|
+
|
53
|
+
if s.respond_to? :specification_version then
|
54
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
55
|
+
s.specification_version = 3
|
56
|
+
|
57
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
58
|
+
else
|
59
|
+
end
|
60
|
+
else
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'conversions'
|
data/lib/conversions.rb
CHANGED
@@ -1,46 +1,59 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
1
|
+
# Conversions makes it easy to convert between units.
|
2
|
+
module Conversions
|
3
|
+
mattr_accessor :conversions
|
4
|
+
|
5
|
+
# Clear all previously registered conversions
|
6
|
+
def self.clear
|
7
|
+
self.conversions = {}
|
8
|
+
end
|
9
|
+
clear
|
25
10
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
30
35
|
end
|
31
|
-
method_missing_without_unit_conversions(m, args)
|
32
36
|
end
|
33
|
-
|
34
|
-
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
40
49
|
end
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
require 'conversions/unit'
|
52
|
+
|
53
|
+
Conversions.load_defaults
|
54
|
+
Numeric.send(:include, Conversions::Ext)
|
55
|
+
|
56
|
+
if defined?(ActiveRecord)
|
57
|
+
require 'conversions/active_record_accessors'
|
58
|
+
ActiveRecord::Base.send(:extend, Conversions::ActiveRecordAccessors)
|
46
59
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Conversions
|
2
|
+
# Implements new accessor classmethods to define conversion accessors on active record classes.
|
3
|
+
module ActiveRecordAccessors
|
4
|
+
# Adds conversion methods to the model for a certain attribute.
|
5
|
+
#
|
6
|
+
# Options:
|
7
|
+
#
|
8
|
+
# * <tt>:internal</tt>: The unit of the internal value
|
9
|
+
# * <tt>:external</tt>: The unit of desired external value
|
10
|
+
# * <tt>:scale</tt>: If a scale is given, the external value is automatically rounded on the specified scale.
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# Examples:
|
14
|
+
#
|
15
|
+
# require 'conversions'
|
16
|
+
#
|
17
|
+
# class Flight
|
18
|
+
# extend Conversions::ActiveRecordAccessors
|
19
|
+
#
|
20
|
+
# attr_accessor :distance
|
21
|
+
# conversion_accessor :distance, :internal => :kilometers, :external => :miles, :scale => 2
|
22
|
+
#
|
23
|
+
# def initialize(distance)
|
24
|
+
# self.distance = distance
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# flight = Flight.new(1200)
|
29
|
+
# flight.distance_in_miles #=> 745.65, rounded down to two decimals as specified in :scale
|
30
|
+
#
|
31
|
+
# When used as a plugin for Rails, the ActiveRecord::Base is automatically extended.
|
32
|
+
#
|
33
|
+
# class Car < ActiveRecord::Base
|
34
|
+
# conversion_accessor :length, :internal => :kilometers, :external => :miles
|
35
|
+
# end
|
36
|
+
def conversion_accessor(attribute, options={})
|
37
|
+
if options[:internal].nil? or options[:external].nil?
|
38
|
+
raise ArgumentError, "Please specify both :external and :internal metrics."
|
39
|
+
end
|
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
|
43
|
+
end
|
44
|
+
define_method "#{attribute}_in_#{options[:external]}=" do |v|
|
45
|
+
send("#{attribute}=", v.blank? ? nil : v.to_f.convert(options[:external], options[:internal], options.except(:internal, :external)))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
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
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Conversions
|
2
|
+
# Proxy class to contain the unit as well as reference the base value
|
3
|
+
class Unit
|
4
|
+
# Create a new Unit instance.
|
5
|
+
#
|
6
|
+
# * _value_: The value to convert from (ie. 4.92)
|
7
|
+
# * _from_: The unit to convert from (ie. :miles)
|
8
|
+
def initialize(value, from)
|
9
|
+
@value = value
|
10
|
+
@from = from
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_i
|
14
|
+
@value.to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_f
|
18
|
+
@value.to_f
|
19
|
+
end
|
20
|
+
|
21
|
+
# Convert to a certain other unit.
|
22
|
+
#
|
23
|
+
# * _to_: The unit to convert to (ie. :kilometers)
|
24
|
+
# * _options_:
|
25
|
+
# * :scale: The number of digits behind the decimal point to you want to keep
|
26
|
+
def to(to, options={})
|
27
|
+
case options
|
28
|
+
when Integer
|
29
|
+
scale = options
|
30
|
+
when Hash
|
31
|
+
scale = options[:scale]
|
32
|
+
end
|
33
|
+
|
34
|
+
value = @value * self.class.exchange_rate(@from, to)
|
35
|
+
scale.nil? ? value : (value * (10 ** scale)).round / (10 ** scale).to_f
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.exchange_rate(from_unit, to_unit) #:nodoc:
|
39
|
+
return 1 if from_unit == to_unit
|
40
|
+
from = Conversions.conversions[from_unit]
|
41
|
+
raise ArgumentError, "Can't convert from `#{from}', unknown unit" if from.nil?
|
42
|
+
to = from[to_unit]
|
43
|
+
raise ArgumentError, "Can't convert from `#{from_unit}' to `#{to_unit}', unknown unit" if to.nil?
|
44
|
+
to
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Flight
|
4
|
+
extend Conversions::ActiveRecordAccessors
|
5
|
+
attr_accessor :distance, :fuel_consumption
|
6
|
+
conversion_accessor :distance, :internal => :kilometres, :external => :miles
|
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
|
+
end
|
12
|
+
|
13
|
+
class AccessorTest < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
reset_defaults
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@flight = Flight.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_include
|
23
|
+
assert @flight.respond_to?(:distance)
|
24
|
+
assert @flight.respond_to?(:distance_in_miles)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_conversion
|
28
|
+
@flight.distance = 1200
|
29
|
+
assert_in_delta 745.645430684801, @flight.distance_in_miles, DELTA
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_set_zero
|
33
|
+
assert_equal nil, @flight.distance_in_miles
|
34
|
+
@flight.distance_in_miles = 0
|
35
|
+
assert_equal 0, @flight.distance_in_miles
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_set_nil
|
39
|
+
assert_equal nil, @flight.distance_in_miles
|
40
|
+
@flight.distance_in_miles = nil
|
41
|
+
assert_equal nil, @flight.distance_in_miles
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_set_blank
|
45
|
+
assert_equal nil, @flight.distance_in_miles
|
46
|
+
@flight.distance_in_miles = ''
|
47
|
+
assert_equal nil, @flight.distance_in_miles
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_conversion_with_scale
|
51
|
+
@flight.fuel_consumption = 3400
|
52
|
+
assert_equal 898.18, @flight.fuel_consumption_in_gallons, DELTA
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_register
|
56
|
+
@flight.distance = 1200
|
57
|
+
assert_in_delta 215.98, @flight.distance_in_leagues, DELTA
|
58
|
+
end
|
59
|
+
end
|
data/test/conversions_test.rb
CHANGED
@@ -1,41 +1,26 @@
|
|
1
|
-
require 'test_helper'
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
3
|
class ConversionsTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
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
|
4
|
+
def setup
|
5
|
+
Conversions.clear
|
9
6
|
end
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
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
|
33
18
|
end
|
34
19
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
40
25
|
end
|
41
|
-
end
|
26
|
+
end
|
data/test/ext_test.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ExtTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
reset_defaults
|
6
|
+
end
|
7
|
+
|
8
|
+
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
|
+
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
|
21
|
+
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
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'test/unit'
|
3
|
-
require 'shoulda'
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
if not defined?(ActiveRecord)
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_record'
|
6
|
+
end
|
7
|
+
|
8
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
7
9
|
require 'conversions'
|
8
10
|
|
9
|
-
|
11
|
+
def reset_defaults
|
12
|
+
Conversions.clear
|
13
|
+
Conversions.load_defaults
|
10
14
|
end
|
15
|
+
|
16
|
+
DELTA = 0.0000001
|
data/test/unit_test.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class UnitTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
reset_defaults
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_exchange_rate
|
9
|
+
assert_in_delta 1.609344, Conversions::Unit.exchange_rate(:miles, :kilometres), DELTA
|
10
|
+
assert_in_delta 0.621371192237334, Conversions::Unit.exchange_rate(:kilometres, :miles), DELTA
|
11
|
+
assert_raises(ArgumentError) { Conversions::Unit.exchange_rate(:unknown, :miles) }
|
12
|
+
assert_raises(ArgumentError) { Conversions::Unit.exchange_rate(:miles, :unknown) }
|
13
|
+
assert_raises(ArgumentError) { Conversions::Unit.exchange_rate(nil, :miles) }
|
14
|
+
assert_raises(ArgumentError) { Conversions::Unit.exchange_rate(:miles, nil) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_exchange_rate_for_identity_transform
|
18
|
+
Conversions.conversions.keys.each do |unit|
|
19
|
+
assert_equal 1, Conversions::Unit.exchange_rate(unit, unit)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_to
|
24
|
+
amount = Conversions::Unit.new(10.0, :miles)
|
25
|
+
assert_in_delta 16.09344, amount.to(:kilometres), DELTA
|
26
|
+
|
27
|
+
amount = Conversions::Unit.new(10.0, :kilograms)
|
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
|
37
|
+
|
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)
|
43
|
+
end
|
44
|
+
|
45
|
+
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)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_to_f_and_to_i
|
52
|
+
amount = Conversions::Unit.new(10.5, :miles)
|
53
|
+
assert_equal 10, amount.to_i
|
54
|
+
assert_equal 10.5, amount.to_f
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_register
|
58
|
+
Conversions.register(:dollars, :cents, 100.0)
|
59
|
+
assert_in_delta 0.01, Conversions::Unit.exchange_rate(:cents, :dollars), DELTA
|
60
|
+
amount = Conversions::Unit.new(10.0, :dollars)
|
61
|
+
assert_equal 1000.0, amount.to(:cents, :scale => 2), DELTA
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,53 +1,56 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: conversions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Manfred Stienstra
|
8
|
+
- Seamus Abshere
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2009-
|
13
|
+
date: 2009-11-02 00:00:00 -05:00
|
13
14
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: A Ruby on Rails plugin that adds conversion capabilities to numeric objects
|
18
|
+
email: seamus@abshere.net
|
27
19
|
executables: []
|
28
20
|
|
29
21
|
extensions: []
|
30
22
|
|
31
23
|
extra_rdoc_files:
|
32
24
|
- LICENSE
|
33
|
-
- README
|
25
|
+
- README
|
34
26
|
files:
|
35
|
-
- .document
|
36
27
|
- .gitignore
|
37
28
|
- LICENSE
|
38
|
-
- README
|
29
|
+
- README
|
39
30
|
- Rakefile
|
40
|
-
-
|
31
|
+
- TODO
|
32
|
+
- VERSION.yml
|
33
|
+
- conversions.gemspec
|
34
|
+
- init.rb
|
41
35
|
- lib/conversions.rb
|
36
|
+
- lib/conversions/active_record_accessors.rb
|
37
|
+
- lib/conversions/defaults.rb
|
38
|
+
- lib/conversions/unit.rb
|
39
|
+
- rails/init.rb
|
40
|
+
- test/accessor_test.rb
|
42
41
|
- test/conversions_test.rb
|
42
|
+
- test/ext_test.rb
|
43
43
|
- test/test_helper.rb
|
44
|
+
- test/unit_test.rb
|
44
45
|
has_rdoc: true
|
45
|
-
homepage: http://github.com/
|
46
|
+
homepage: http://github.com/seamusabshere/conversions
|
46
47
|
licenses: []
|
47
48
|
|
48
49
|
post_install_message:
|
49
50
|
rdoc_options:
|
50
51
|
- --charset=UTF-8
|
52
|
+
- --line-numbers
|
53
|
+
- --inline-source
|
51
54
|
require_paths:
|
52
55
|
- lib
|
53
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -64,11 +67,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
67
|
version:
|
65
68
|
requirements: []
|
66
69
|
|
67
|
-
rubyforge_project:
|
70
|
+
rubyforge_project: conversions
|
68
71
|
rubygems_version: 1.3.5
|
69
72
|
signing_key:
|
70
73
|
specification_version: 3
|
71
|
-
summary: adds conversion
|
74
|
+
summary: A Ruby on Rails plugin that adds conversion capabilities to numeric objects
|
72
75
|
test_files:
|
76
|
+
- test/accessor_test.rb
|
73
77
|
- test/conversions_test.rb
|
78
|
+
- test/ext_test.rb
|
74
79
|
- test/test_helper.rb
|
80
|
+
- test/unit_test.rb
|
data/.document
DELETED
data/README.rdoc
DELETED
@@ -1,18 +0,0 @@
|
|
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.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.0
|