m9t 0.1.0 → 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.
@@ -1,20 +1,23 @@
1
1
  == m9t - Various units of measurement
2
2
 
3
- This package aims to handle the basic units of measure:
4
- * Distance
5
- * Temperature
6
- * Direction
7
- * Speed
3
+ This package handles the basic units of measure:
4
+ * distance,
5
+ * direction,
6
+ * speed,
7
+ * temperature,
8
+ * weight.
8
9
 
9
10
  The emphasis is on:
10
- * coherent interface
11
- * ease of translation
11
+ * coherent interface,
12
+ * ease of translation (using i18n).
12
13
 
13
14
  == Other Software
14
15
 
15
16
  * ruby-units
16
- * doesn't handle i18n - very difficult to integrate
17
- * monkey patches a lot of core classes
17
+ * Doesn't handle i18n:
18
+ * The library depends heavily on English string representations of units.
19
+ * Monkey patches a lot of core classes:
20
+ * Adds methods to e.g. Object.
18
21
 
19
22
  == License
20
23
 
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
22
22
  s.author = 'Joe Yates'
23
23
  s.email = 'joe.g.yates@gmail.com'
24
24
 
25
- s.files = ['README.rdoc', 'COPYING', 'Rakefile'] + FileList['{lib,test}/**/*.rb']
25
+ s.files = ['README.rdoc', 'COPYING', 'Rakefile'] + FileList['{lib,test}/**/*.rb'] + FileList['locales/**/*.{rb,yml}']
26
26
  s.require_paths = ['lib']
27
27
  s.add_dependency('i18n', '>= 0.3.5')
28
28
 
data/lib/m9t.rb CHANGED
@@ -37,7 +37,7 @@ module M9t
37
37
  module VERSION #:nodoc:
38
38
  MAJOR = 0
39
39
  MINOR = 1
40
- TINY = 0
40
+ TINY = 2
41
41
 
42
42
  STRING = [MAJOR, MINOR, TINY].join('.')
43
43
  end
@@ -6,7 +6,7 @@ module M9t
6
6
 
7
7
  module Base
8
8
 
9
- def self.included(base)
9
+ def self.included(base) #:nodoc:
10
10
 
11
11
  base.instance_eval do
12
12
  # Returns the classes current options - see the specific class for defaults
@@ -32,12 +32,16 @@ module M9t
32
32
 
33
33
  attr_reader :value, :options
34
34
 
35
+ # ==== Parameters
36
+ # * +options+ - See individual classes for options
35
37
  def initialize(value, options = self.class.options.clone)
36
38
  @value, @options = value.to_f, self.class.options.merge(options)
37
39
  raise M9t::UnitError.new("Unknown units '#{ @options[:units] }'. Known: #{ self.class::KNOWN_UNITS.collect{|unit| unit.to_s}.join(', ') }") \
38
40
  if not self.class::KNOWN_UNITS.find_index(@options[:units])
39
41
  end
40
42
 
43
+ # Returns the string representation of the measurement,
44
+ # taking into account locale, desired units and abbreviation.
41
45
  def to_s
42
46
  value_in_units = self.class.send("to_#{ @options[:units] }", @value)
43
47
  localized_value = I18n.localize_float(value_in_units, {:format => "%0.#{ @options[:precision] }f"})
@@ -16,21 +16,28 @@ module M9t
16
16
 
17
17
  class << self
18
18
 
19
+ # Identity conversion. Simply returns the supplied number
19
20
  def to_degrees(d)
20
- d
21
+ d.to_f
21
22
  end
22
23
 
24
+ # Given a value in degrees, returns the nearest (localized) compass direction
25
+ # M9t::Directions.to_compass(42) => 'NE'
23
26
  def to_compass(d)
24
27
  sector = (normalize(d) / COMPASS_SECTOR_DEGREES).round
25
28
  I18n.t(self.measurement_name + '.sectors')[sector]
26
29
  end
27
30
 
31
+ # Accepts a localized compass direction (e.g. 'N') and returns the equivalent M9t::Direction
32
+ # M9t::Direction.compass('NE') => #<M9t::Direction:0xb76cc750 @value=45.0, @options={:units=>:degrees, :decimals=>5, :abbreviated=>false}>
28
33
  def compass(s)
29
34
  sector = I18n.t(self.measurement_name + '.sectors').find_index(s)
30
35
  raise "Compass direction '#{ s }' not recognised" if sector.nil?
31
36
  new(sector.to_f * COMPASS_SECTOR_DEGREES)
32
37
  end
33
38
 
39
+ # Reduce directions in degrees to the range [0, 360)
40
+ # M9t::Direction.normalize(1000) => 280.0
34
41
  def normalize(d)
35
42
  case
36
43
  when d < 0
@@ -44,6 +51,7 @@ module M9t
44
51
 
45
52
  end
46
53
 
54
+ # Handles the special case where compass directions are the desired output.
47
55
  def to_s
48
56
  if @options[:units] == :compass
49
57
  Direction.to_compass(@value)
@@ -17,23 +17,27 @@ module M9t
17
17
 
18
18
  class << self
19
19
 
20
- # Unit convertors: convert to meters
21
- def miles(m)
22
- m.to_f * METERS_PER_MILE
20
+ # Accepts a value in kilometers and returns the equivalent M9t::Distance
21
+ def kilometers(km, options = {})
22
+ new(km.to_f * METERS_PER_KILOMETER, options)
23
23
  end
24
24
 
25
- def kilometers(km)
26
- km.to_f * METERS_PER_KILOMETER
25
+ # Accepts a value in miles and returns the equivalent M9t::Distance
26
+ def miles(m, options = {})
27
+ new(m.to_f * METERS_PER_MILE, options)
27
28
  end
28
29
 
30
+ # Identity conversion. Simply returns the supplied number
29
31
  def to_meters(m)
30
32
  m.to_f
31
33
  end
32
34
 
35
+ # Converts meters into kilometers
33
36
  def to_kilometers(m)
34
37
  m.to_f / METERS_PER_KILOMETER
35
38
  end
36
39
 
40
+ # Converts meters into miles
37
41
  def to_miles(m)
38
42
  m.to_f / METERS_PER_MILE
39
43
  end
@@ -42,10 +46,12 @@ module M9t
42
46
 
43
47
  alias :to_meters :value
44
48
 
49
+ # Returns the value converted to kilometers
45
50
  def to_kilometers
46
51
  self.class.to_kilometers(@value)
47
52
  end
48
53
 
54
+ # Returns the value converted to miles
49
55
  def to_miles
50
56
  self.class.to_miles(@value)
51
57
  end
@@ -2,10 +2,12 @@
2
2
  require 'rubygems' if RUBY_VERSION < '1.9'
3
3
  require 'i18n'
4
4
 
5
- # Monkey patch I18n, adding:
6
- # - handling for non-English numerical separators
5
+ # Monkey patch I18n
7
6
  module I18n
8
7
 
8
+ # Handle non-English numerical separators
9
+ # with I18n.locale = :it,
10
+ # I18n.localize_float(5.23) => '5,23000'
9
11
  def I18n.localize_float(f, options = {})
10
12
  format = options[:format] || '%f'
11
13
  s = format % f
@@ -18,23 +18,27 @@ module M9t
18
18
 
19
19
  class << self
20
20
 
21
- # Unit convertors: convert to meters per second
22
- def kilometers_per_hour(kmh)
23
- kmh.to_f * MS_TO_KMH
21
+ # Accepts kilometers per hour and returns a M9t::Speed instance
22
+ def kilometers_per_hour(kmh, options = {})
23
+ new(kmh.to_f * MS_TO_KMH, options)
24
24
  end
25
25
 
26
- def miles_per_hour(mph)
27
- mph.to_f * MS_TO_MPH
26
+ # Accepts miles per hour and returns a M9t::Speed instance
27
+ def miles_per_hour(mph, options = {})
28
+ new(mph.to_f * MS_TO_MPH, options)
28
29
  end
29
30
 
31
+ # Identity conversion. Simply returns the supplied number
30
32
  def to_meters_per_second(mps)
31
33
  mps.to_f
32
34
  end
33
35
 
36
+ # Converts meters per second to kilometers per hour
34
37
  def to_kilometers_per_hour(mps)
35
38
  mps.to_f / MS_TO_KMH
36
39
  end
37
40
 
41
+ # Converts meters per second to miles per hour
38
42
  def to_miles_per_hour(mps)
39
43
  mps.to_f / MS_TO_MPH
40
44
  end
@@ -43,10 +47,12 @@ module M9t
43
47
 
44
48
  alias :to_meters_per_second :value
45
49
 
50
+ # Returns the value converted to kilometers per hour
46
51
  def to_kilometers_per_hour
47
52
  self.class.to_kilometers_per_hour(@value)
48
53
  end
49
54
 
55
+ # Returns the value converted to miles per hour
50
56
  def to_miles_per_hour
51
57
  self.class.to_miles_per_hour(@value)
52
58
  end
@@ -0,0 +1,45 @@
1
+ # encoding: utf8
2
+ en:
3
+ numbers:
4
+ thousands_separator: ','
5
+ decimal_separator: '.'
6
+ units:
7
+ distance:
8
+ meters:
9
+ full:
10
+ zero: meters
11
+ one: meter
12
+ other: meters
13
+ abbreviated: m
14
+ kilometers:
15
+ full:
16
+ zero: kilometers
17
+ one: kilometer
18
+ other: kilometers
19
+ abbreviated: km
20
+ miles:
21
+ full:
22
+ zero: miles
23
+ one: mile
24
+ other: miles
25
+ direction:
26
+ degrees:
27
+ full:
28
+ zero: degrees
29
+ one: degree
30
+ other: degrees
31
+ abbreviated: °
32
+ speed:
33
+ meters_per_second:
34
+ full:
35
+ zero: meters per second
36
+ one: meter per second
37
+ other: meters per second
38
+ abbreviated: m/s
39
+ knots:
40
+ full:
41
+ zero: knots
42
+ one: knot
43
+ other: knots
44
+ direction:
45
+ sectors: ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
@@ -0,0 +1,45 @@
1
+ # encoding: utf8
2
+ it:
3
+ numbers:
4
+ thousands_separator: '.'
5
+ decimal_separator: ','
6
+ units:
7
+ distance:
8
+ meters:
9
+ full:
10
+ zero: metri
11
+ one: metro
12
+ other: metri
13
+ abbreviated: m
14
+ kilometers:
15
+ full:
16
+ zero: chilometri
17
+ one: chilometro
18
+ other: chilometri
19
+ abbreviated: km
20
+ miles:
21
+ full:
22
+ zero: miglia
23
+ one: miglio
24
+ other: miglia
25
+ direction:
26
+ degrees:
27
+ full:
28
+ zero: gradi
29
+ one: grado
30
+ other: gradi
31
+ abbreviated: °
32
+ speed:
33
+ meters_per_second:
34
+ full:
35
+ zero: metri al second
36
+ one: metro al second
37
+ other: metri al second
38
+ abbreviated: m/s
39
+ knots:
40
+ full:
41
+ zero: nodi
42
+ one: nodo
43
+ other: nodi
44
+ direction:
45
+ sectors: ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSO', 'SO', 'OSO', 'O', 'ONO', 'NO', 'NNO', 'N']
@@ -48,8 +48,8 @@ class TestM9tDirection < Test::Unit::TestCase
48
48
 
49
49
  def test_to_compass
50
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
51
+ assert_equal('NE', M9t::Direction.to_compass(42)) # Quantizing up
52
+ assert_equal('E', M9t::Direction.to_compass(93)) # Quantizing down
53
53
  assert_equal('ESE', M9t::Direction.to_compass(113)) # 16ths
54
54
  I18n.locale = :it
55
55
  assert_equal('O', M9t::Direction.to_compass(270))
@@ -37,11 +37,11 @@ class TestM9tDistance < Test::Unit::TestCase
37
37
 
38
38
  # Conversion from non-SI units
39
39
  def test_kilometers
40
- assert_equal(300.0, M9t::Distance.kilometers(0.3))
40
+ assert_equal(300.0, M9t::Distance.kilometers(0.3).value)
41
41
  end
42
42
 
43
43
  def test_miles
44
- assert_equal(0.3 * 1609.344, M9t::Distance.miles(0.3))
44
+ assert_equal(0.3 * 1609.344, M9t::Distance.miles(0.3).value)
45
45
  end
46
46
 
47
47
  # Conversion to non-SI units
@@ -124,7 +124,7 @@ class TestM9tDistance < Test::Unit::TestCase
124
124
  end
125
125
 
126
126
  def test_miles_singular
127
- distance = M9t::Distance.new(M9t::Distance.miles(1), {:units => :miles, :precision => 0})
127
+ distance = M9t::Distance.miles(1, {:units => :miles, :precision => 0})
128
128
  I18n.locale = :en
129
129
  assert_equal(distance.to_s, '1 mile')
130
130
  I18n.locale = :it
@@ -28,7 +28,7 @@ class TestM9tSpeed < Test::Unit::TestCase
28
28
  # input conversions
29
29
 
30
30
  def test_class_miles_per_hour
31
- assert_equal(45, M9t::Speed.miles_per_hour(20.1168))
31
+ assert_equal(45, M9t::Speed.miles_per_hour(20.1168).value)
32
32
  end
33
33
 
34
34
  # output conversions
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joe Yates
@@ -55,6 +55,8 @@ files:
55
55
  - test/distance_test.rb
56
56
  - test/test_all.rb
57
57
  - test/speed_test.rb
58
+ - locales/it.yml
59
+ - locales/en.yml
58
60
  has_rdoc: true
59
61
  homepage: http://github.com/joeyates/m9t
60
62
  licenses: []