m9t 0.1.0

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/COPYING ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Joe Yates
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.
@@ -0,0 +1,21 @@
1
+ == m9t - Various units of measurement
2
+
3
+ This package aims to handle the basic units of measure:
4
+ * Distance
5
+ * Temperature
6
+ * Direction
7
+ * Speed
8
+
9
+ The emphasis is on:
10
+ * coherent interface
11
+ * ease of translation
12
+
13
+ == Other Software
14
+
15
+ * ruby-units
16
+ * doesn't handle i18n - very difficult to integrate
17
+ * monkey patches a lot of core classes
18
+
19
+ == License
20
+
21
+ MIT License. See the included COPYING file.
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/testtask'
5
+ require 'rake/clean'
6
+
7
+ $:.unshift(File.dirname(__FILE__) + '/lib')
8
+ require 'm9t'
9
+
10
+ RDOC_OPTS = ['--quiet', '--title', 'm9t: Measurement units', '--main', 'README.rdoc', '--inline-source']
11
+ CLEAN.include 'doc'
12
+
13
+ task :default => :test
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = 'm9t'
17
+ s.summary = 'Classes for handling measurement units'
18
+ s.description = 'Classes for handling measurement units, conversions and translations'
19
+ s.version = M9t::VERSION::STRING
20
+
21
+ s.homepage = 'http://github.com/joeyates/m9t'
22
+ s.author = 'Joe Yates'
23
+ s.email = 'joe.g.yates@gmail.com'
24
+
25
+ s.files = ['README.rdoc', 'COPYING', 'Rakefile'] + FileList['{lib,test}/**/*.rb']
26
+ s.require_paths = ['lib']
27
+ s.add_dependency('i18n', '>= 0.3.5')
28
+
29
+ s.has_rdoc = true
30
+ s.rdoc_options += RDOC_OPTS
31
+ s.extra_rdoc_files = ['README.rdoc', 'COPYING']
32
+
33
+ s.test_file = 'test/test_all.rb'
34
+ end
35
+
36
+ Rake::TestTask.new do |t|
37
+ t.libs << 'test'
38
+ t.test_files = FileList['test/*_test.rb']
39
+ t.verbose = true
40
+ end
41
+
42
+ Rake::RDocTask.new do |rdoc|
43
+ rdoc.rdoc_dir = 'doc/rdoc'
44
+ rdoc.options += RDOC_OPTS
45
+ rdoc.main = 'README.rdoc'
46
+ rdoc.rdoc_files.add ['README.rdoc', 'COPYING', 'lib/**/*.rb']
47
+ end
48
+
49
+ Rake::GemPackageTask.new(spec) do |pkg|
50
+ end
@@ -0,0 +1,51 @@
1
+ #--
2
+ # Copyright (c) 2010 Joe Yates
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ # encoding: utf-8
25
+ require 'rubygems' if RUBY_VERSION < '1.9'
26
+ require 'i18n'
27
+
28
+ locales_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'locales'))
29
+ I18n.load_path += Dir.glob("#{ locales_path }/*.yml")
30
+
31
+ Dir[File.dirname(__FILE__) + '/m9t/*.rb'].each do |file|
32
+ require file
33
+ end
34
+
35
+ module M9t
36
+
37
+ module VERSION #:nodoc:
38
+ MAJOR = 0
39
+ MINOR = 1
40
+ TINY = 0
41
+
42
+ STRING = [MAJOR, MINOR, TINY].join('.')
43
+ end
44
+
45
+ class M9tError < StandardError
46
+ end
47
+
48
+ class UnitError < M9tError
49
+ end
50
+
51
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'rubygems' if RUBY_VERSION < '1.9'
3
+ require 'i18n'
4
+
5
+ module M9t
6
+
7
+ module Base
8
+
9
+ def self.included(base)
10
+
11
+ base.instance_eval do
12
+ # Returns the classes current options - see the specific class for defaults
13
+ def options
14
+ @options
15
+ end
16
+
17
+ # Reloads the class' default options
18
+ def reset_options!
19
+ @options = self::DEFAULT_OPTIONS.clone # 'self::' is necessary with ruby 1.8
20
+ end
21
+
22
+ # The name used for i18n translations
23
+ # E.g. M9t::Distance => 'distance'
24
+ def measurement_name
25
+ name.downcase.split('::')[1]
26
+ end
27
+
28
+ reset_options!
29
+ end
30
+
31
+ end
32
+
33
+ attr_reader :value, :options
34
+
35
+ def initialize(value, options = self.class.options.clone)
36
+ @value, @options = value.to_f, self.class.options.merge(options)
37
+ raise M9t::UnitError.new("Unknown units '#{ @options[:units] }'. Known: #{ self.class::KNOWN_UNITS.collect{|unit| unit.to_s}.join(', ') }") \
38
+ if not self.class::KNOWN_UNITS.find_index(@options[:units])
39
+ end
40
+
41
+ def to_s
42
+ value_in_units = self.class.send("to_#{ @options[:units] }", @value)
43
+ localized_value = I18n.localize_float(value_in_units, {:format => "%0.#{ @options[:precision] }f"})
44
+
45
+ key = 'units.' + self.class.measurement_name + '.' + @options[:units].to_s
46
+ @options[:abbreviated] ? key += '.abbreviated' : key += '.full'
47
+ unit = I18n.t(key, {:count => value_in_units})
48
+
49
+ "#{ localized_value }%s#{ unit }" % (@options[:abbreviated] ? '' : ' ')
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ require 'rubygems' if RUBY_VERSION < '1.9'
3
+ require 'i18n'
4
+
5
+ module M9t
6
+
7
+ class Direction
8
+ DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :decimals => 5}
9
+ KNOWN_UNITS = [:degrees, :compass]
10
+
11
+ # Conversions
12
+ CIRCLE = 360.0
13
+ COMPASS_SECTOR_DEGREES = CIRCLE / 16.0
14
+
15
+ include M9t::Base
16
+
17
+ class << self
18
+
19
+ def to_degrees(d)
20
+ d
21
+ end
22
+
23
+ def to_compass(d)
24
+ sector = (normalize(d) / COMPASS_SECTOR_DEGREES).round
25
+ I18n.t(self.measurement_name + '.sectors')[sector]
26
+ end
27
+
28
+ def compass(s)
29
+ sector = I18n.t(self.measurement_name + '.sectors').find_index(s)
30
+ raise "Compass direction '#{ s }' not recognised" if sector.nil?
31
+ new(sector.to_f * COMPASS_SECTOR_DEGREES)
32
+ end
33
+
34
+ def normalize(d)
35
+ case
36
+ when d < 0
37
+ normalize(d + CIRCLE)
38
+ when d >= CIRCLE
39
+ normalize(d - CIRCLE)
40
+ else
41
+ d
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ def to_s
48
+ if @options[:units] == :compass
49
+ Direction.to_compass(@value)
50
+ else
51
+ super
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ require 'rubygems' if RUBY_VERSION < '1.9'
3
+ require 'i18n'
4
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'base')
5
+
6
+ module M9t
7
+
8
+ class Distance
9
+ DEFAULT_OPTIONS = {:units => :meters, :abbreviated => false, :precision => 5}
10
+ KNOWN_UNITS = [:meters, :miles, :kilometers]
11
+
12
+ # Conversions
13
+ METERS_PER_MILE = 1609.344
14
+ METERS_PER_KILOMETER = 1000.0
15
+
16
+ include M9t::Base
17
+
18
+ class << self
19
+
20
+ # Unit convertors: convert to meters
21
+ def miles(m)
22
+ m.to_f * METERS_PER_MILE
23
+ end
24
+
25
+ def kilometers(km)
26
+ km.to_f * METERS_PER_KILOMETER
27
+ end
28
+
29
+ def to_meters(m)
30
+ m.to_f
31
+ end
32
+
33
+ def to_kilometers(m)
34
+ m.to_f / METERS_PER_KILOMETER
35
+ end
36
+
37
+ def to_miles(m)
38
+ m.to_f / METERS_PER_MILE
39
+ end
40
+
41
+ end
42
+
43
+ alias :to_meters :value
44
+
45
+ def to_kilometers
46
+ self.class.to_kilometers(@value)
47
+ end
48
+
49
+ def to_miles
50
+ self.class.to_miles(@value)
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require 'rubygems' if RUBY_VERSION < '1.9'
3
+ require 'i18n'
4
+
5
+ # Monkey patch I18n, adding:
6
+ # - handling for non-English numerical separators
7
+ module I18n
8
+
9
+ def I18n.localize_float(f, options = {})
10
+ format = options[:format] || '%f'
11
+ s = format % f
12
+ integers, decimal = s.split('.')
13
+ integers ||= ''
14
+
15
+ thousands_separator = I18n.t('numbers.thousands_separator')
16
+ integers.gsub(',', thousands_separator)
17
+
18
+ return integers if decimal.nil?
19
+
20
+ decimal_separator = I18n.t('numbers.decimal_separator')
21
+ integers + decimal_separator + decimal
22
+ end
23
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ require 'rubygems' if RUBY_VERSION < '1.9'
3
+ require 'i18n'
4
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'distance')
5
+
6
+ module M9t
7
+
8
+ class Speed
9
+ DEFAULT_OPTIONS = {:units => :meters_per_second, :abbreviated => false, :precision => 5}
10
+ KNOWN_UNITS = [:meters_per_second, :kilometers_per_hour, :miles_per_hour, :knots]
11
+
12
+ # Conversions
13
+ SECONDS_PER_HOUR = 60.0 * 60
14
+ MS_TO_KMH = SECONDS_PER_HOUR / M9t::Distance::METERS_PER_KILOMETER
15
+ MS_TO_MPH = SECONDS_PER_HOUR / M9t::Distance::METERS_PER_MILE
16
+
17
+ include M9t::Base
18
+
19
+ class << self
20
+
21
+ # Unit convertors: convert to meters per second
22
+ def kilometers_per_hour(kmh)
23
+ kmh.to_f * MS_TO_KMH
24
+ end
25
+
26
+ def miles_per_hour(mph)
27
+ mph.to_f * MS_TO_MPH
28
+ end
29
+
30
+ def to_meters_per_second(mps)
31
+ mps.to_f
32
+ end
33
+
34
+ def to_kilometers_per_hour(mps)
35
+ mps.to_f / MS_TO_KMH
36
+ end
37
+
38
+ def to_miles_per_hour(mps)
39
+ mps.to_f / MS_TO_MPH
40
+ end
41
+
42
+ end
43
+
44
+ alias :to_meters_per_second :value
45
+
46
+ def to_kilometers_per_hour
47
+ self.class.to_kilometers_per_hour(@value)
48
+ end
49
+
50
+ def to_miles_per_hour
51
+ self.class.to_miles_per_hour(@value)
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems' if RUBY_VERSION < '1.9'
5
+ require 'test/unit'
6
+ require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
7
+
8
+ class TestM9tDirection < Test::Unit::TestCase
9
+
10
+ def setup
11
+ I18n.locale = :en
12
+ M9t::Direction.reset_options!
13
+ end
14
+
15
+ # Basic use
16
+ def test_degrees
17
+ assert_equal(45, M9t::Direction.new(45).value)
18
+ end
19
+
20
+ # Class methods
21
+
22
+ # Base class
23
+
24
+ def test_measurement_name
25
+ assert_equal('direction', M9t::Direction.measurement_name)
26
+ end
27
+
28
+ def test_normalize
29
+ assert_equal(5, M9t::Direction.normalize(725))
30
+ assert_equal(5, M9t::Direction.normalize(-355))
31
+ end
32
+
33
+ def test_default_options_set
34
+ assert_not_nil(M9t::Direction.options)
35
+ end
36
+
37
+ def test_default_option_abbreviated
38
+ assert(! M9t::Direction.options[:abbreviated])
39
+ end
40
+
41
+ def test_default_option_units
42
+ assert_equal(:degrees, M9t::Direction.options[:units])
43
+ end
44
+
45
+ def test_to_degrees_identity
46
+ assert_equal(45, M9t::Direction.to_degrees(45))
47
+ end
48
+
49
+ def test_to_compass
50
+ assert_equal('N', M9t::Direction.to_compass(0))
51
+ assert_equal('N', M9t::Direction.to_compass(7)) # Quantizing
52
+ assert_equal('E', M9t::Direction.to_compass(93)) # Quantizing
53
+ assert_equal('ESE', M9t::Direction.to_compass(113)) # 16ths
54
+ I18n.locale = :it
55
+ assert_equal('O', M9t::Direction.to_compass(270))
56
+ end
57
+
58
+ def test_compass
59
+ assert_equal(0, M9t::Direction.compass('N').value)
60
+ assert_equal(247.5, M9t::Direction.compass('WSW').value)
61
+ end
62
+
63
+ # Instance methods
64
+
65
+ def test_unknown_units
66
+ assert_raises(M9t::UnitError) { M9t::Direction.new(10, {:units => :foos}) }
67
+ end
68
+
69
+ def test_to_s_not_abbreviated_singular
70
+ assert_equal '1 degree', M9t::Direction.new(1).to_s
71
+ I18n.locale = :it
72
+ assert_equal '1 grado', M9t::Direction.new(1).to_s
73
+ end
74
+
75
+ def test_to_s_not_abbreviated_plural
76
+ assert_equal '135 degrees', M9t::Direction.new(135).to_s
77
+ I18n.locale = :it
78
+ assert_equal '135 gradi', M9t::Direction.new(135).to_s
79
+ end
80
+
81
+ def test_to_s_abbreviated
82
+ assert_equal '135°', M9t::Direction.new(135, {:abbreviated => true}).to_s
83
+ end
84
+
85
+ def test_compass_units
86
+ assert_equal 'SW', M9t::Direction.new(225, {:units => :compass}).to_s
87
+ I18n.locale = :it
88
+ assert_equal 'SO', M9t::Direction.new(225, {:units => :compass}).to_s
89
+ end
90
+
91
+ def test_handles_string_leading_zero
92
+ assert_equal(10, M9t::Direction.new('010').value)
93
+ end
94
+
95
+ end
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems' if RUBY_VERSION < '1.9'
5
+ require 'test/unit'
6
+ require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
7
+
8
+ class TestM9tDistance < Test::Unit::TestCase
9
+
10
+ def setup
11
+ I18n.locale = :en
12
+ M9t::Distance.reset_options!
13
+ end
14
+
15
+ # Basic use
16
+ def test_singular
17
+ distance = M9t::Distance.new(1, :precision => 0)
18
+ I18n.locale = :en
19
+ assert_equal('1 meter', distance.to_s)
20
+ end
21
+
22
+ def test_plural
23
+ distance = M9t::Distance.new(10, :precision => 0)
24
+ I18n.locale = :en
25
+ assert_equal('10 meters', distance.to_s)
26
+ I18n.locale = :it
27
+ assert_equal('10 metri', distance.to_s)
28
+ end
29
+
30
+ # Class methods
31
+
32
+ # Base class
33
+
34
+ def test_unit_name
35
+ assert_equal('distance', M9t::Distance.measurement_name)
36
+ end
37
+
38
+ # Conversion from non-SI units
39
+ def test_kilometers
40
+ assert_equal(300.0, M9t::Distance.kilometers(0.3))
41
+ end
42
+
43
+ def test_miles
44
+ assert_equal(0.3 * 1609.344, M9t::Distance.miles(0.3))
45
+ end
46
+
47
+ # Conversion to non-SI units
48
+ def test_class_to_meters
49
+ assert_equal(0.3, M9t::Distance.to_meters(0.3))
50
+ end
51
+
52
+ def test_class_to_kilometers
53
+ assert_equal(0.0003, M9t::Distance.to_kilometers(0.3))
54
+ end
55
+
56
+ def test_default_options_set
57
+ assert_not_nil(M9t::Distance.options)
58
+ end
59
+
60
+ def test_default_option_abbreviated
61
+ assert(! M9t::Distance.options[:abbreviated])
62
+ end
63
+
64
+ def test_default_option_units
65
+ assert_equal(:meters, M9t::Distance.options[:units])
66
+ end
67
+
68
+ def test_default_option_decimals
69
+ assert_equal(5, M9t::Distance.options[:precision])
70
+ end
71
+
72
+ # Instance methods
73
+
74
+ def test_new
75
+ assert_equal(0.3, M9t::Distance.new(0.3).value)
76
+ end
77
+
78
+ def test_default_options_merged
79
+ distance = M9t::Distance.new(10, {:abbreviated => true})
80
+ assert_equal(:meters, distance.options[:units])
81
+ assert_equal(5, distance.options[:precision])
82
+ end
83
+
84
+ def test_set_default_options_get_inherited
85
+ M9t::Distance.options[:precision] = 0
86
+ distance = M9t::Distance.new(10)
87
+ assert_equal(0, distance.options[:precision])
88
+ end
89
+
90
+ def test_to_meters
91
+ assert_equal(0.3, M9t::Distance.new(0.3).to_meters)
92
+ end
93
+
94
+ def test_to_kilometers
95
+ assert_equal(0.0003, M9t::Distance.new(0.3).to_kilometers)
96
+ end
97
+
98
+ def test_to_s_singular
99
+ assert_equal('1.00000 meter', M9t::Distance.new(1).to_s)
100
+ end
101
+
102
+ def test_to_s_plural
103
+ assert_equal('0.30000 meters', M9t::Distance.new(0.3).to_s)
104
+ end
105
+
106
+ def test_to_s_plural_abbreviated
107
+ distance = M9t::Distance.new(10, {:abbreviated => true, :precision => 0})
108
+ I18n.locale = :en
109
+ assert_equal('10m', distance.to_s)
110
+ I18n.locale = :it
111
+ assert_equal('10m', distance.to_s)
112
+ end
113
+
114
+ def test_to_s_abbreviated
115
+ assert_equal('0.30000m', M9t::Distance.new(0.3, {:abbreviated => true}).to_s)
116
+ end
117
+
118
+ def test_to_s_precision
119
+ assert_equal('0.3m', M9t::Distance.new(0.3, {:precision => 1, :abbreviated => true}).to_s)
120
+ end
121
+
122
+ def test_to_s_kilometers
123
+ assert_equal('156.0 kilometers', M9t::Distance.new(156003, {:precision => 1, :units => :kilometers}).to_s)
124
+ end
125
+
126
+ def test_miles_singular
127
+ distance = M9t::Distance.new(M9t::Distance.miles(1), {:units => :miles, :precision => 0})
128
+ I18n.locale = :en
129
+ assert_equal(distance.to_s, '1 mile')
130
+ I18n.locale = :it
131
+ assert_equal(distance.to_s, '1 miglio')
132
+ end
133
+
134
+ def test_to_s_miles_plural
135
+ distance = M9t::Distance.new(10000, {:units => :miles, :precision => 1})
136
+ I18n.locale = :en
137
+ assert_equal('6.2 miles', distance.to_s)
138
+ I18n.locale = :it
139
+ assert_equal('6,2 miglia', distance.to_s)
140
+ end
141
+
142
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems' if RUBY_VERSION < '1.9'
5
+ require 'test/unit'
6
+ require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
7
+
8
+ class TestI18nMonkeyPatching < Test::Unit::TestCase
9
+
10
+ def setup
11
+ end
12
+
13
+ def test_translations_loaded
14
+ I18n.locale = :en
15
+ assert_equal('.', I18n.t('numbers.decimal_separator'))
16
+ I18n.locale = :it
17
+ assert_equal(',', I18n.t('numbers.decimal_separator'))
18
+ end
19
+
20
+ def test_localize_float_default
21
+ assert_equal('1.500000', I18n.localize_float(1.5))
22
+ end
23
+
24
+ def test_localize_float_formatted
25
+ assert_equal('1.5', I18n.localize_float(1.5, {:format => '%0.1f'}))
26
+ end
27
+
28
+ def test_localize_float_italian
29
+ I18n.locale = :it
30
+ assert_equal('1,5', I18n.localize_float(1.5, {:format => '%0.1f'}))
31
+ end
32
+
33
+ end
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems' if RUBY_VERSION < '1.9'
5
+ require 'test/unit'
6
+ require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib'), 'm9t')
7
+
8
+ class TestM9tSpeed < Test::Unit::TestCase
9
+
10
+ def setup
11
+ I18n.locale = :en
12
+ M9t::Speed.reset_options!
13
+ end
14
+
15
+ # Basic use
16
+ def test_new
17
+ assert_equal(45, M9t::Speed.new(45).value)
18
+ end
19
+
20
+ # Class methods
21
+
22
+ # Base class
23
+
24
+ def test_measurement_name
25
+ assert_equal('speed', M9t::Speed.measurement_name)
26
+ end
27
+
28
+ # input conversions
29
+
30
+ def test_class_miles_per_hour
31
+ assert_equal(45, M9t::Speed.miles_per_hour(20.1168))
32
+ end
33
+
34
+ # output conversions
35
+
36
+ def test_class_to_miles_per_hour
37
+ assert_equal(20.1168, M9t::Speed.to_miles_per_hour(45))
38
+ end
39
+
40
+ # Instance methods
41
+
42
+ # new
43
+
44
+ def test_unknown_units
45
+ assert_raises(M9t::UnitError) { M9t::Speed.new('010', {:units => :foos}) }
46
+ end
47
+
48
+ # output conversions
49
+
50
+ def test_kmh
51
+ assert_equal(12.5, M9t::Speed.new(45).to_kilometers_per_hour)
52
+ end
53
+
54
+ def test_mph
55
+ assert_equal(20.1168, M9t::Speed.new(45).to_miles_per_hour)
56
+ end
57
+
58
+ # to_s
59
+
60
+ def test_to_s
61
+ assert_equal '135.00000 meters per second', M9t::Speed.new(135).to_s
62
+ end
63
+
64
+ def test_to_s_precision
65
+ assert_equal '135 meters per second', M9t::Speed.new(135, :precision => 0).to_s
66
+ end
67
+
68
+ def test_to_s_abbreviated
69
+ assert_equal '135m/s', M9t::Speed.new(135, :abbreviated => true, :precision => 0).to_s
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.dirname(__FILE__)+'/*_test.rb'].each do |test|
2
+ require test
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: m9t
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Joe Yates
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: i18n
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 3
30
+ - 5
31
+ version: 0.3.5
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Classes for handling measurement units, conversions and translations
35
+ email: joe.g.yates@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ - COPYING
43
+ files:
44
+ - README.rdoc
45
+ - COPYING
46
+ - Rakefile
47
+ - lib/m9t.rb
48
+ - lib/m9t/speed.rb
49
+ - lib/m9t/base.rb
50
+ - lib/m9t/direction.rb
51
+ - lib/m9t/distance.rb
52
+ - lib/m9t/i18n.rb
53
+ - test/direction_test.rb
54
+ - test/i18n_test.rb
55
+ - test/distance_test.rb
56
+ - test/test_all.rb
57
+ - test/speed_test.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/joeyates/m9t
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --quiet
65
+ - --title
66
+ - "m9t: Measurement units"
67
+ - --main
68
+ - README.rdoc
69
+ - --inline-source
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.6
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Classes for handling measurement units
93
+ test_files:
94
+ - test/test_all.rb