convertable_values 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +55 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/convertable_values.gemspec +67 -0
- data/lib/convertable_values.rb +73 -0
- data/lib/numeric/numeric_units.rb +176 -0
- data/test/helper.rb +18 -0
- data/test/test_convertable_values.rb +95 -0
- metadata +142 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.1"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.1)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.5.1)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Stephen Blankenship
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
This gem provides 2 things:
|
4
|
+
1. a simple unit conversion library, and
|
5
|
+
2. a pattern for transparently converting to/from base units.
|
6
|
+
|
7
|
+
Example scenario: You have a model that wants to store values with units (e.g., 200 lbs),
|
8
|
+
but you want (or at least should want) to store all the values in the same base unit (e.g., 90.718474 kg).
|
9
|
+
This gem makes using this design pattern totally transparent.
|
10
|
+
|
11
|
+
The main goal behind this library is to enable transparent handling of unit conversions on a model. The obvious use-case
|
12
|
+
for this is to ensure all values of a similar system are stored in the same unit. The benefit is that you can then do batch
|
13
|
+
computations on the data-set without having to worry about the unit each record's value is stored in.
|
14
|
+
|
15
|
+
The unit conversions in this library are done as simply as possible by extending Numeric.
|
16
|
+
Comprehensiveness and precision of the unit conversion capabilities are not on par with Ruby libraries such as Ruby-Units and Alchemist.
|
17
|
+
If your needs are more based on converting in many different crazy systems then I suggest checking out those libraries.
|
18
|
+
|
19
|
+
One last quick note. The simple conversion approach taken here is faster than ruby-units or alchemist. In most cases this
|
20
|
+
won't matter, but it's still worth noting...
|
21
|
+
|
22
|
+
= Using It
|
23
|
+
|
24
|
+
1. gem install convertable_values
|
25
|
+
2. include ConvertableValues and "convert" your attributes
|
26
|
+
|
27
|
+
In a standard class:
|
28
|
+
|
29
|
+
class MyNormalClass
|
30
|
+
include ConvertableValues
|
31
|
+
attr_accessor :value, :unit
|
32
|
+
|
33
|
+
convert(:value, :unit)
|
34
|
+
end
|
35
|
+
|
36
|
+
Including for your entire Rails project:
|
37
|
+
|
38
|
+
# Stick in a file in initializers or in environment file
|
39
|
+
ActiveRecord::Base.send(:include, ConvertableValues)
|
40
|
+
|
41
|
+
== Contributing to convertable_values
|
42
|
+
|
43
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
44
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
45
|
+
* Fork the project
|
46
|
+
* Start a feature/bugfix branch
|
47
|
+
* Commit and push until you are happy with your contribution
|
48
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
49
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
50
|
+
|
51
|
+
== Copyright
|
52
|
+
|
53
|
+
Copyright (c) 2010 Stephen Blankenship. See LICENSE.txt for
|
54
|
+
further details.
|
55
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "convertable_values"
|
16
|
+
gem.homepage = "http://github.com/stephenb/convertable_values"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.}
|
19
|
+
gem.description = %Q{Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.
|
20
|
+
Example usage: You have a model that wants to store values with units (e.g., 200 lbs), but you want (or at least should want)
|
21
|
+
to store all the values in a base unit (e.g., 90.718474 kilograms). This gem makes using this design pattern totally transparent.}
|
22
|
+
gem.email = "stephenrb@gmail.com"
|
23
|
+
gem.authors = ["Stephen Blankenship"]
|
24
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
25
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
26
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
27
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
28
|
+
end
|
29
|
+
Jeweler::RubygemsDotOrgTasks.new
|
30
|
+
|
31
|
+
require 'rake/testtask'
|
32
|
+
Rake::TestTask.new(:test) do |test|
|
33
|
+
test.libs << 'lib' << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'rcov/rcovtask'
|
39
|
+
Rcov::RcovTask.new do |test|
|
40
|
+
test.libs << 'test'
|
41
|
+
test.pattern = 'test/**/test_*.rb'
|
42
|
+
test.verbose = true
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "convertable_values #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{convertable_values}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stephen Blankenship"]
|
12
|
+
s.date = %q{2010-11-14}
|
13
|
+
s.description = %q{Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.
|
14
|
+
Example usage: You have a model that wants to store values with units (e.g., 200 lbs), but you want (or at least should want)
|
15
|
+
to store all the values in a base unit (e.g., 90.718474 kilograms). This gem makes using this design pattern totally transparent.}
|
16
|
+
s.email = %q{stephenrb@gmail.com}
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.rdoc"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"convertable_values.gemspec",
|
30
|
+
"lib/convertable_values.rb",
|
31
|
+
"lib/numeric/numeric_units.rb",
|
32
|
+
"test/helper.rb",
|
33
|
+
"test/test_convertable_values.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/stephenb/convertable_values}
|
36
|
+
s.licenses = ["MIT"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.}
|
40
|
+
s.test_files = [
|
41
|
+
"test/helper.rb",
|
42
|
+
"test/test_convertable_values.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
53
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
58
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'numeric/numeric_units'
|
2
|
+
|
3
|
+
module ConvertableValues
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
# Calling convert(value_attr_name, unit_attr_name) will set up that
|
12
|
+
# pair of value/unit attributes to automatically convert the valu to/from the
|
13
|
+
# given unit's base unit.
|
14
|
+
def convert(value_attr, unit_attr)
|
15
|
+
# Create override method for converting value to base unit when set
|
16
|
+
define_method "#{value_attr}=".to_sym do |new_value|
|
17
|
+
unit_str = send(unit_attr.to_sym)
|
18
|
+
|
19
|
+
if new_value && unit_str
|
20
|
+
# store the value converted to the base unit corresponding to the given unit
|
21
|
+
# write_attribute(value_attr.to_sym, v.send(unit_attr.to_sym))
|
22
|
+
instance_variable_set("@#{value_attr}".to_sym, new_value.send(unit_str))
|
23
|
+
else
|
24
|
+
instance_variable_set("@#{value_attr}".to_sym, new_value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Create override method for converting value to stored unit when accessed
|
29
|
+
define_method value_attr.to_sym do
|
30
|
+
unit_str = send(unit_attr.to_sym)
|
31
|
+
|
32
|
+
if unit_str
|
33
|
+
# return the value converted back to whatever unit was stored
|
34
|
+
instance_variable_get("@#{value_attr}".to_sym).to(unit_str.to_sym)
|
35
|
+
else
|
36
|
+
instance_variable_get("@#{value_attr}".to_sym)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Create override method for updating value when unit is set/changed
|
41
|
+
define_method "#{unit_attr}=".to_sym do |new_unit|
|
42
|
+
old_unit = instance_variable_get("@#{unit_attr}".to_sym)
|
43
|
+
instance_variable_set("@#{unit_attr}".to_sym, new_unit)
|
44
|
+
|
45
|
+
# Re-assign the value so it will be converted properly
|
46
|
+
value = instance_variable_get("@#{value_attr}".to_sym)
|
47
|
+
send("#{value_attr}=".to_sym, value) if value && old_unit.nil?
|
48
|
+
|
49
|
+
new_unit
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#### Base Units ####
|
54
|
+
|
55
|
+
def base_mass_unit
|
56
|
+
:kilograms
|
57
|
+
end
|
58
|
+
|
59
|
+
def base_volume_unit
|
60
|
+
:fluid_ounce
|
61
|
+
end
|
62
|
+
|
63
|
+
def base_length_unit
|
64
|
+
:meters
|
65
|
+
end
|
66
|
+
|
67
|
+
def base_time_unit
|
68
|
+
:seconds
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
# This file extends the Numeric class to provide simple utility for unit conversions.
|
2
|
+
# To properly use these extenstions, default units will/should be used:
|
3
|
+
# => MASS = kilograms
|
4
|
+
# => DISTANCE/MEASUREMENT = meters
|
5
|
+
# => TIME = seconds
|
6
|
+
# => VOLUME = fluid ounces
|
7
|
+
#
|
8
|
+
# So, calling 3.pounds will return 1.36077711, which is the value converted to the base unit of kilograms.
|
9
|
+
#
|
10
|
+
# When retrieving the values, you can call one of the slew of to_* methods.
|
11
|
+
# => 5.km.to_miles -> 3.10685596118667
|
12
|
+
# whereas
|
13
|
+
# => 5.km -> 5000 (meters)
|
14
|
+
#
|
15
|
+
class Numeric
|
16
|
+
|
17
|
+
##### MASS #####
|
18
|
+
|
19
|
+
def kilograms
|
20
|
+
self
|
21
|
+
end
|
22
|
+
alias :kilogram :kilograms
|
23
|
+
alias :kg :kilograms
|
24
|
+
alias :kgs :kilograms
|
25
|
+
|
26
|
+
def pounds
|
27
|
+
self * 0.45359237.kilograms
|
28
|
+
end
|
29
|
+
alias :pound :pounds
|
30
|
+
alias :lb :pounds
|
31
|
+
alias :lbs :pounds
|
32
|
+
|
33
|
+
def ounce
|
34
|
+
self * 0.0283495231.kilograms
|
35
|
+
end
|
36
|
+
alias :oz :ounce
|
37
|
+
alias :ounces :ounce
|
38
|
+
|
39
|
+
def stone
|
40
|
+
self * 6.35029318.kilograms
|
41
|
+
end
|
42
|
+
alias :stones :stone
|
43
|
+
|
44
|
+
def ton
|
45
|
+
self * 907.18474.kilograms
|
46
|
+
end
|
47
|
+
alias :tons :ton
|
48
|
+
|
49
|
+
|
50
|
+
###### DISTANCE/MEASUREMENTS/LENGTH #####
|
51
|
+
|
52
|
+
def meters
|
53
|
+
self
|
54
|
+
end
|
55
|
+
alias :m :meters
|
56
|
+
alias :meter :meters
|
57
|
+
|
58
|
+
def millimeters
|
59
|
+
self * 0.001.meters
|
60
|
+
end
|
61
|
+
alias :mm :millimeters
|
62
|
+
alias :millimeter :millimeters
|
63
|
+
|
64
|
+
def centimeters
|
65
|
+
self * 0.01.meters
|
66
|
+
end
|
67
|
+
alias :cm :centimeters
|
68
|
+
alias :centimeter :centimeters
|
69
|
+
|
70
|
+
def feet
|
71
|
+
self * 0.3048.meters
|
72
|
+
end
|
73
|
+
alias :ft :feet
|
74
|
+
alias :foot :feet
|
75
|
+
|
76
|
+
def inches
|
77
|
+
self * 0.0254.meters
|
78
|
+
end
|
79
|
+
alias :in :inches
|
80
|
+
alias :inch :inches
|
81
|
+
|
82
|
+
def yards
|
83
|
+
self * 3.feet
|
84
|
+
end
|
85
|
+
alias :yd :yards
|
86
|
+
alias :yard :yards
|
87
|
+
|
88
|
+
def miles
|
89
|
+
self * 1609.344.meters
|
90
|
+
end
|
91
|
+
alias :mi :miles
|
92
|
+
alias :mile :miles
|
93
|
+
|
94
|
+
def kilometers
|
95
|
+
self * 1000.meters
|
96
|
+
end
|
97
|
+
alias :km :kilometers
|
98
|
+
alias :kilometer :kilometers
|
99
|
+
|
100
|
+
|
101
|
+
#### TIME ####
|
102
|
+
|
103
|
+
def seconds
|
104
|
+
self
|
105
|
+
end
|
106
|
+
alias :second :seconds
|
107
|
+
alias :secs :seconds
|
108
|
+
alias :sec :seconds
|
109
|
+
|
110
|
+
def minutes
|
111
|
+
self * 60.seconds
|
112
|
+
end
|
113
|
+
alias :min :minutes
|
114
|
+
alias :minute :minutes
|
115
|
+
alias :mins :minutes
|
116
|
+
|
117
|
+
def hours
|
118
|
+
self * 60.minutes
|
119
|
+
end
|
120
|
+
alias :hr :hours
|
121
|
+
alias :hrs :hours
|
122
|
+
alias :hour :hours
|
123
|
+
|
124
|
+
|
125
|
+
#### VOLUME #####
|
126
|
+
|
127
|
+
def fluid_ounce
|
128
|
+
self
|
129
|
+
end
|
130
|
+
alias :fluid_ounces :fluid_ounce
|
131
|
+
alias :fl_oz :fluid_ounce
|
132
|
+
|
133
|
+
def teaspoon
|
134
|
+
self * (1.0/6.0)
|
135
|
+
end
|
136
|
+
alias :teaspoons :teaspoon
|
137
|
+
alias :tsp :teaspoon
|
138
|
+
|
139
|
+
def tablespoon
|
140
|
+
self * 0.5.fl_oz
|
141
|
+
end
|
142
|
+
alias :tablespoons :tablespoon
|
143
|
+
alias :tbsp :tablespoon
|
144
|
+
|
145
|
+
def cup
|
146
|
+
self * 8.fl_oz
|
147
|
+
end
|
148
|
+
alias :cups :cup
|
149
|
+
|
150
|
+
def pint
|
151
|
+
self * 16.fl_oz
|
152
|
+
end
|
153
|
+
alias :pints :pint
|
154
|
+
alias :pt :pint
|
155
|
+
|
156
|
+
def quart
|
157
|
+
self * 16.fl_oz
|
158
|
+
end
|
159
|
+
alias :quarts :quart
|
160
|
+
alias :qt :quart
|
161
|
+
|
162
|
+
# Helps convert back to a proper value
|
163
|
+
def method_missing(method, *args)
|
164
|
+
if match = /^to_([a-zA-Z_]*)$/.match(method.to_s)
|
165
|
+
self / 1.send(match[1]).to_f
|
166
|
+
else
|
167
|
+
super
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# Call using something like to(:pounds)
|
172
|
+
def to(unit)
|
173
|
+
self.send("to_#{unit.to_s}")
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'convertable_values'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestConvertableValues < Test::Unit::TestCase
|
4
|
+
|
5
|
+
P = 1**-10
|
6
|
+
|
7
|
+
context "A Numeric object" do
|
8
|
+
should "auto-convert numbers properly to their value in the base unit" do
|
9
|
+
assert_in_delta 1.pound, (1.kg * 0.45359237), P
|
10
|
+
assert_in_delta 1.mile, (1.m * 1609.344), P
|
11
|
+
assert_in_delta 1.hour, (1.sec * 60 * 60), P
|
12
|
+
assert_in_delta 1.teaspoon, (1.0.fl_oz / 6.0), P
|
13
|
+
end
|
14
|
+
|
15
|
+
should "support to_unit" do
|
16
|
+
assert_in_delta 354.3.to_pounds, 781.097795, P
|
17
|
+
assert_in_delta 354.3.lbs.to_stones, 25.3071429, P
|
18
|
+
end
|
19
|
+
|
20
|
+
should "support to(:unit)" do
|
21
|
+
assert_respond_to 354.3, :to
|
22
|
+
assert_in_delta 354.3.to(:pounds), 781.097795, P
|
23
|
+
assert_in_delta 354.3.lbs.to(:stones), 25.3071429, P
|
24
|
+
end
|
25
|
+
|
26
|
+
should "convert compatible numbers equally" do
|
27
|
+
assert_equal 5.km, 5000.meters
|
28
|
+
assert_equal 60.secs, 1.min
|
29
|
+
assert_equal 3.feet, 1.yard
|
30
|
+
assert_equal 3.1068559611866697.mi, 5.km
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "A class that includes ConvertableValues" do
|
35
|
+
setup do
|
36
|
+
class AClassIncludingConvertableValues
|
37
|
+
include ConvertableValues
|
38
|
+
attr_accessor :value, :unit
|
39
|
+
convert(:value, :unit)
|
40
|
+
end
|
41
|
+
@obj = AClassIncludingConvertableValues.new
|
42
|
+
|
43
|
+
assert_nothing_raised do
|
44
|
+
@obj.unit = 'mi'
|
45
|
+
@obj.value = 3.10685596
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
should "set the value converted to the base unit" do
|
50
|
+
assert_in_delta @obj.instance_variable_get(:@value), 5000, P # Bypass accessor
|
51
|
+
end
|
52
|
+
|
53
|
+
should "set the value converted to the base unit" do
|
54
|
+
assert_in_delta @obj.value, 3.10685596, P
|
55
|
+
end
|
56
|
+
|
57
|
+
should "return the set unit" do
|
58
|
+
assert_equal @obj.unit, 'mi'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "When setting the unit after the value" do
|
63
|
+
setup do
|
64
|
+
class AClassIncludingConvertableValues
|
65
|
+
include ConvertableValues
|
66
|
+
attr_accessor :value, :unit
|
67
|
+
convert(:value, :unit)
|
68
|
+
end
|
69
|
+
@obj = AClassIncludingConvertableValues.new
|
70
|
+
@obj.value = 3.10685596
|
71
|
+
end
|
72
|
+
|
73
|
+
should "return the inputted value before setting unit" do
|
74
|
+
assert_equal @obj.value, 3.10685596
|
75
|
+
end
|
76
|
+
|
77
|
+
should "return nil unit" do
|
78
|
+
assert_nil @obj.unit
|
79
|
+
end
|
80
|
+
|
81
|
+
should "return the converted value after setting unit" do
|
82
|
+
@obj.unit = 'mi'
|
83
|
+
assert_in_delta @obj.instance_variable_get(:@value), 5000, P # Bypass accessor
|
84
|
+
assert_in_delta @obj.value, 3.10685596, P
|
85
|
+
end
|
86
|
+
|
87
|
+
should "return the value converted to the latest unit after setting unit multiple times" do
|
88
|
+
@obj.unit = 'mi'
|
89
|
+
@obj.unit = 'km'
|
90
|
+
assert_in_delta @obj.instance_variable_get(:@value), 5000, P # Bypass accessor
|
91
|
+
assert_in_delta @obj.value, 5, P
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: convertable_values
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stephen Blankenship
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-14 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :development
|
24
|
+
name: shoulda
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
name: bundler
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
type: :development
|
54
|
+
name: jeweler
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 1
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 5
|
64
|
+
- 1
|
65
|
+
version: 1.5.1
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
name: rcov
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirement: *id004
|
81
|
+
description: |-
|
82
|
+
Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.
|
83
|
+
Example usage: You have a model that wants to store values with units (e.g., 200 lbs), but you want (or at least should want)
|
84
|
+
to store all the values in a base unit (e.g., 90.718474 kilograms). This gem makes using this design pattern totally transparent.
|
85
|
+
email: stephenrb@gmail.com
|
86
|
+
executables: []
|
87
|
+
|
88
|
+
extensions: []
|
89
|
+
|
90
|
+
extra_rdoc_files:
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.rdoc
|
93
|
+
files:
|
94
|
+
- .document
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.rdoc
|
99
|
+
- Rakefile
|
100
|
+
- VERSION
|
101
|
+
- convertable_values.gemspec
|
102
|
+
- lib/convertable_values.rb
|
103
|
+
- lib/numeric/numeric_units.rb
|
104
|
+
- test/helper.rb
|
105
|
+
- test/test_convertable_values.rb
|
106
|
+
has_rdoc: true
|
107
|
+
homepage: http://github.com/stephenb/convertable_values
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
version: "0"
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
hash: 3
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
version: "0"
|
133
|
+
requirements: []
|
134
|
+
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 1.3.7
|
137
|
+
signing_key:
|
138
|
+
specification_version: 3
|
139
|
+
summary: Convertable Values provides a simple unit conversion library and pattern for transparently storing base units.
|
140
|
+
test_files:
|
141
|
+
- test/helper.rb
|
142
|
+
- test/test_convertable_values.rb
|