m9t 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/GPL-LICENSE.txt +674 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE.txt +7 -0
- data/README.md +100 -0
- data/Rakefile +9 -14
- data/lib/m9t/base.rb +3 -3
- data/lib/m9t/distance.rb +3 -3
- data/lib/m9t/pressure.rb +4 -4
- data/lib/m9t/speed.rb +3 -4
- data/lib/m9t/version.rb +2 -2
- data/locales/it.yml +1 -1
- data/m9t.gemspec +29 -0
- data/test/base_test.rb +55 -0
- data/test/direction_test.rb +10 -7
- data/test/distance_test.rb +1 -4
- data/test/i18n_test.rb +2 -5
- data/test/pressure_test.rb +1 -4
- data/test/speed_test.rb +3 -6
- data/test/temperature_test.rb +6 -5
- data/test/test_helper.rb +17 -0
- metadata +97 -38
- data/COPYING +0 -20
- data/README.rdoc +0 -62
- data/test/test_all.rb +0 -3
data/Gemfile
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 Joe Yates
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
m9t [![Build Status](https://secure.travis-ci.org/joeyates/m9t.png)][Continuous Integration]
|
2
|
+
===
|
3
|
+
|
4
|
+
*Measurements and coversions library for Ruby*
|
5
|
+
|
6
|
+
* [Source Code]
|
7
|
+
* [API documentation]
|
8
|
+
* [Rubygem]
|
9
|
+
* [Continuous Integration]
|
10
|
+
|
11
|
+
[Source Code]: https://github.com/joeyates/m9t "Source code at GitHub"
|
12
|
+
[API documentation]: http://rubydoc.info/gems/m9t/frames "RDoc API Documentation at Rubydoc.info"
|
13
|
+
[Rubygem]: http://rubygems.org/gems/m9t "Ruby gem at rubygems.org"
|
14
|
+
[Continuous Integration]: http://travis-ci.org/joeyates/m9t "Build status by Travis-CI"
|
15
|
+
|
16
|
+
This package handles the basic units of measure:
|
17
|
+
|
18
|
+
- distance,
|
19
|
+
- direction,
|
20
|
+
- speed,
|
21
|
+
- temperature,
|
22
|
+
- weight.
|
23
|
+
|
24
|
+
The emphasis is on:
|
25
|
+
|
26
|
+
- coherent interface,
|
27
|
+
- ease of translation (using i18n).
|
28
|
+
|
29
|
+
Internals
|
30
|
+
=========
|
31
|
+
|
32
|
+
Internally, values are stored in SI units, with the exception of temperature:
|
33
|
+
|
34
|
+
- distance - meters,
|
35
|
+
- direction - degrees,
|
36
|
+
- speed - meters per second,
|
37
|
+
- temperature - degrees celcius,
|
38
|
+
- weight - kilograms.
|
39
|
+
|
40
|
+
Interface
|
41
|
+
=========
|
42
|
+
|
43
|
+
new: accepts the S.I. unit as a parameter:
|
44
|
+
|
45
|
+
height = M9t::Distance.new(1.75)
|
46
|
+
|
47
|
+
to_f: returns the decimal value(s):
|
48
|
+
|
49
|
+
height.to_f -> 1.75
|
50
|
+
|
51
|
+
other units:
|
52
|
+
there are class methods named after each known unit,
|
53
|
+
which take values in that unit
|
54
|
+
(actually, they are defined as needed):
|
55
|
+
|
56
|
+
marathon = M9t::Distance.miles(26.21875)
|
57
|
+
marathon.to_f -> 42194.988
|
58
|
+
|
59
|
+
to_s: returns a localized string with units:
|
60
|
+
|
61
|
+
I18n.locale = :it
|
62
|
+
puts M9t::Distance.new(3).to_s -> '3 metri'
|
63
|
+
|
64
|
+
Class methods for conversion
|
65
|
+
============================
|
66
|
+
|
67
|
+
Methods are available for conversion between any pair of units:
|
68
|
+
|
69
|
+
M9t::Distance.miles_to_meters(26.21875) -> 42194.988
|
70
|
+
|
71
|
+
Testing
|
72
|
+
=======
|
73
|
+
|
74
|
+
Coverage
|
75
|
+
--------
|
76
|
+
|
77
|
+
ruby 1.8.x:
|
78
|
+
|
79
|
+
$ rake rcov
|
80
|
+
|
81
|
+
ruby 1.9.x:
|
82
|
+
|
83
|
+
$ COVERAGE=1 rake test
|
84
|
+
|
85
|
+
Alternatives
|
86
|
+
============
|
87
|
+
|
88
|
+
- ruby-units
|
89
|
+
- Doesn't handle i18n:
|
90
|
+
- The library depends heavily on English string representations of units.
|
91
|
+
- Monkey patches a lot of core classes:
|
92
|
+
- Adds methods to e.g. Object.
|
93
|
+
|
94
|
+
License
|
95
|
+
=======
|
96
|
+
|
97
|
+
Dual license:
|
98
|
+
|
99
|
+
- MIT License: see MIT-LICENSE.txt,
|
100
|
+
- GPL version 3: see GPL-LICENSE.txt
|
data/Rakefile
CHANGED
@@ -1,15 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require 'rake/rdoctask'
|
1
|
+
require 'bundler'
|
3
2
|
require 'rake/testtask'
|
4
|
-
require 'rake/clean'
|
5
3
|
|
6
4
|
$:.unshift( File.dirname(__FILE__) + '/lib' )
|
7
|
-
require 'bundler/version'
|
8
5
|
require 'm9t'
|
9
6
|
|
10
|
-
RDOC_OPTS = ['--quiet', '--title', 'm9t: Measurement units', '--main', 'README.rdoc', '--inline-source']
|
11
|
-
CLEAN.include 'doc'
|
12
|
-
|
13
7
|
task :default => :test
|
14
8
|
|
15
9
|
Rake::TestTask.new do |t|
|
@@ -18,19 +12,20 @@ Rake::TestTask.new do |t|
|
|
18
12
|
t.verbose = true
|
19
13
|
end
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
15
|
+
if RUBY_VERSION < '1.9'
|
16
|
+
require 'rcov/rcovtask'
|
17
|
+
Rcov::RcovTask.new do |t|
|
18
|
+
t.test_files = FileList['test/*_test.rb']
|
19
|
+
t.rcov_opts << '--exclude /gems/'
|
20
|
+
end
|
26
21
|
end
|
27
22
|
|
28
23
|
desc "Build the gem"
|
29
24
|
task :build do
|
30
|
-
|
25
|
+
`gem build m9t.gemspec`
|
31
26
|
end
|
32
27
|
|
33
28
|
desc "Publish a new version of the gem"
|
34
29
|
task :release => :build do
|
35
|
-
|
30
|
+
`gem push m9t-#{M9t::VERSION::STRING}.gem`
|
36
31
|
end
|
data/lib/m9t/base.rb
CHANGED
@@ -9,7 +9,7 @@ module M9t
|
|
9
9
|
def self.generate_conversions(klass)
|
10
10
|
klass.instance_eval do |klass|
|
11
11
|
def convert(from, to, value)
|
12
|
-
value
|
12
|
+
value / self::CONVERSIONS[from] * self::CONVERSIONS[to]
|
13
13
|
end
|
14
14
|
|
15
15
|
def method_missing(name, *args, &block)
|
@@ -40,7 +40,7 @@ module M9t
|
|
40
40
|
return false if not self::CONVERSIONS[name.to_sym]
|
41
41
|
self.class.instance_exec do
|
42
42
|
define_method( name.to_sym ) do | *args |
|
43
|
-
new( args[ 0 ].to_f
|
43
|
+
new( args[ 0 ].to_f / self::CONVERSIONS[ name ] )
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -69,7 +69,7 @@ module M9t
|
|
69
69
|
# The name used for i18n translations
|
70
70
|
# M9t::Distance => 'distance'
|
71
71
|
def measurement_name
|
72
|
-
name.
|
72
|
+
name.split('::')[-1].downcase
|
73
73
|
end
|
74
74
|
|
75
75
|
def default_unit
|
data/lib/m9t/distance.rb
CHANGED
@@ -10,9 +10,9 @@ module M9t
|
|
10
10
|
DEFAULT_OPTIONS = {:units => :meters, :abbreviated => false, :precision => 5}
|
11
11
|
CONVERSIONS = {
|
12
12
|
:meters => 1.0,
|
13
|
-
:kilometers => 1000.0,
|
14
|
-
:feet => 0.3048,
|
15
|
-
:miles => 1609.344
|
13
|
+
:kilometers => 1.0 / 1000.0,
|
14
|
+
:feet => 1.0 / 0.3048,
|
15
|
+
:miles => 1.0 / 1609.344
|
16
16
|
}
|
17
17
|
|
18
18
|
include M9t::Base
|
data/lib/m9t/pressure.rb
CHANGED
@@ -10,10 +10,10 @@ module M9t
|
|
10
10
|
DEFAULT_OPTIONS = {:units => :bar, :abbreviated => false, :precision => 5}
|
11
11
|
CONVERSIONS = {
|
12
12
|
:bar => 1.0,
|
13
|
-
:pascals => 0.00001,
|
14
|
-
:hectopascals => 0.001,
|
15
|
-
:kilopascals => 0.01,
|
16
|
-
:inches_of_mercury => 3386.389 * 0.00001
|
13
|
+
:pascals => 1.0 / 0.00001,
|
14
|
+
:hectopascals => 1.0 / 0.001,
|
15
|
+
:kilopascals => 1.0 / 0.01,
|
16
|
+
:inches_of_mercury => 1.0 / ( 3386.389 * 0.00001 )
|
17
17
|
}
|
18
18
|
|
19
19
|
include M9t::Base
|
data/lib/m9t/speed.rb
CHANGED
@@ -9,10 +9,9 @@ module M9t
|
|
9
9
|
class Speed
|
10
10
|
DEFAULT_OPTIONS = {:units => :meters_per_second, :abbreviated => false, :precision => 5}
|
11
11
|
SECONDS_PER_HOUR = 60.0 * 60
|
12
|
-
KMH = M9t::Distance::CONVERSIONS[:kilometers]
|
13
|
-
MPH = M9t::Distance::CONVERSIONS[:miles]
|
14
|
-
|
15
|
-
KNOTS = KNOTS_TO_KMH / SECONDS_PER_HOUR * M9t::Distance::CONVERSIONS[:kilometers]
|
12
|
+
KMH = M9t::Distance::CONVERSIONS[:kilometers] * SECONDS_PER_HOUR
|
13
|
+
MPH = M9t::Distance::CONVERSIONS[:miles] * SECONDS_PER_HOUR
|
14
|
+
KNOTS = KMH / 1.852
|
16
15
|
CONVERSIONS = {
|
17
16
|
:meters_per_second => 1.0,
|
18
17
|
:kilometers_per_hour => KMH,
|
data/lib/m9t/version.rb
CHANGED
data/locales/it.yml
CHANGED
data/m9t.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift( File.dirname(__FILE__) + '/lib' )
|
3
|
+
require 'm9t/version'
|
4
|
+
|
5
|
+
gemspec = Gem::Specification.new do |s|
|
6
|
+
s.name = 'm9t'
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.version = M9t::VERSION::STRING
|
9
|
+
|
10
|
+
s.summary = 'Measurements and conversions library for Ruby'
|
11
|
+
s.description = 'Classes for handling basic measurement units: distance, direction, speed, temperature and pressure'
|
12
|
+
|
13
|
+
s.homepage = 'https://github.com/joeyates/m9t'
|
14
|
+
s.author = 'Joe Yates'
|
15
|
+
s.email = 'joe.g.yates@gmail.com'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.map( &:chomp! ).reject { | f | f[ 0 .. 0 ] == '.' }
|
18
|
+
s.test_files = `git ls-files`.map( &:chomp! ).select { | f | f =~ /_test.rb$/ }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.rubyforge_project = 'nowarning'
|
22
|
+
|
23
|
+
s.add_dependency 'rake' if RUBY_VERSION < '1.9'
|
24
|
+
s.add_dependency 'rake', '~> 0.8.7' if RUBY_VERSION > '1.9'
|
25
|
+
s.add_dependency 'i18n', '>= 0.3.5'
|
26
|
+
|
27
|
+
s.add_development_dependency 'rcov' if RUBY_VERSION < '1.9'
|
28
|
+
s.add_development_dependency 'simplecov' if RUBY_VERSION > '1.9'
|
29
|
+
end
|
data/test/base_test.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
4
|
+
|
5
|
+
class SomeMeasurement
|
6
|
+
DEFAULT_OPTIONS = {:units => :foos, :abbreviated => false, :decimals => 1}
|
7
|
+
CONVERSIONS = {:foos => 1.0,
|
8
|
+
:bars => 1 / 42.0}
|
9
|
+
|
10
|
+
include M9t::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
class SomeDerivedMeasurement < SomeMeasurement
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestM9tBase < Test::Unit::TestCase
|
17
|
+
|
18
|
+
def setup
|
19
|
+
I18n.locale = :en
|
20
|
+
SomeMeasurement.reset_options!
|
21
|
+
end
|
22
|
+
|
23
|
+
# class methods
|
24
|
+
|
25
|
+
def test_class_method_missing_for_known_units
|
26
|
+
assert_in_delta(0.0714, SomeMeasurement.foos_to_bars(3.0), 0.001)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_class_method_missing_fails_on_unknown_units
|
30
|
+
assert_raise(RuntimeError) do
|
31
|
+
SomeMeasurement.bazs_to_bars(3.0)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# instance methods
|
36
|
+
|
37
|
+
def test_instance_method_missing_for_known_units
|
38
|
+
some = SomeMeasurement.new(3.0)
|
39
|
+
assert_in_delta(0.0714, some.to_bars(3.0), 0.001)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_instance_method_missing_fails_on_unknown_units
|
43
|
+
some = SomeMeasurement.new(3.0)
|
44
|
+
assert_raise(RuntimeError) do
|
45
|
+
some.to_bazs(3.0)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# derived classes
|
50
|
+
|
51
|
+
def test_derived_class_gets_options
|
52
|
+
assert_equal(SomeMeasurement.options, SomeDerivedMeasurement.options)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/test/direction_test.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestM9tDirection < Test::Unit::TestCase
|
9
|
-
|
6
|
+
|
10
7
|
def setup
|
11
8
|
I18n.locale = :en
|
12
9
|
M9t::Direction.reset_options!
|
@@ -48,8 +45,8 @@ class TestM9tDirection < Test::Unit::TestCase
|
|
48
45
|
|
49
46
|
def test_degrees_to_compass
|
50
47
|
assert_equal('N', M9t::Direction.degrees_to_compass(0))
|
51
|
-
assert_equal('NE', M9t::Direction.degrees_to_compass(42)) #
|
52
|
-
assert_equal('E', M9t::Direction.degrees_to_compass(93)) #
|
48
|
+
assert_equal('NE', M9t::Direction.degrees_to_compass(42)) # Rounding up
|
49
|
+
assert_equal('E', M9t::Direction.degrees_to_compass(93)) # Rounding down
|
53
50
|
assert_equal('ESE', M9t::Direction.degrees_to_compass(113)) # 16ths
|
54
51
|
I18n.locale = :it
|
55
52
|
assert_equal('O', M9t::Direction.degrees_to_compass(270))
|
@@ -100,4 +97,10 @@ class TestM9tDirection < Test::Unit::TestCase
|
|
100
97
|
assert_equal(10, M9t::Direction.new('010').value)
|
101
98
|
end
|
102
99
|
|
100
|
+
def test_to_compass
|
101
|
+
direction = M9t::Direction.new(0)
|
102
|
+
|
103
|
+
assert_equal('N', direction.to_compass)
|
104
|
+
end
|
105
|
+
|
103
106
|
end
|
data/test/distance_test.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestM9tDistance < Test::Unit::TestCase
|
9
6
|
|
data/test/i18n_test.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestI18nMonkeyPatching < Test::Unit::TestCase
|
9
|
-
|
6
|
+
|
10
7
|
def setup
|
11
8
|
end
|
12
9
|
|
data/test/pressure_test.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestM9tDistance < Test::Unit::TestCase
|
9
6
|
|
data/test/speed_test.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestM9tSpeed < Test::Unit::TestCase
|
9
|
-
|
6
|
+
|
10
7
|
def setup
|
11
8
|
I18n.locale = :en
|
12
9
|
M9t::Speed.reset_options!
|
@@ -28,7 +25,7 @@ class TestM9tSpeed < Test::Unit::TestCase
|
|
28
25
|
# conversion constants
|
29
26
|
|
30
27
|
def test_knot_conversion
|
31
|
-
assert_in_delta(
|
28
|
+
assert_in_delta(1.9438, M9t::Speed::KNOTS, 0.0001)
|
32
29
|
end
|
33
30
|
|
34
31
|
# input conversions
|
data/test/temperature_test.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: utf-8
|
3
2
|
|
4
|
-
require '
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
|
3
|
+
require File.expand_path( 'test_helper', File.dirname( __FILE__ ) )
|
7
4
|
|
8
5
|
class TestM9tTemperature < Test::Unit::TestCase
|
9
|
-
|
6
|
+
|
10
7
|
def setup
|
11
8
|
I18n.locale = :en
|
12
9
|
M9t::Temperature.reset_options!
|
@@ -51,6 +48,10 @@ class TestM9tTemperature < Test::Unit::TestCase
|
|
51
48
|
assert_in_delta(32, M9t::Temperature.to_fahrenheit(0), 0.0001)
|
52
49
|
end
|
53
50
|
|
51
|
+
def test_class_kelvin_to_degrees
|
52
|
+
assert_in_delta(-273.15, M9t::Temperature.kelvin_to_degrees(0), 0.0001)
|
53
|
+
end
|
54
|
+
|
54
55
|
# Instance methods
|
55
56
|
|
56
57
|
# new
|