m9t 0.1.2 → 0.1.3

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/lib/m9t.rb CHANGED
@@ -37,14 +37,16 @@ module M9t
37
37
  module VERSION #:nodoc:
38
38
  MAJOR = 0
39
39
  MINOR = 1
40
- TINY = 2
40
+ TINY = 3
41
41
 
42
42
  STRING = [MAJOR, MINOR, TINY].join('.')
43
43
  end
44
44
 
45
+ # Base class for all M9t exceptions
45
46
  class M9tError < StandardError
46
47
  end
47
48
 
49
+ # Raised when a M9t class receives an unrecogized ':units' value
48
50
  class UnitError < M9tError
49
51
  end
50
52
 
data/lib/m9t/base.rb CHANGED
@@ -36,8 +36,7 @@ module M9t
36
36
  # * +options+ - See individual classes for options
37
37
  def initialize(value, options = self.class.options.clone)
38
38
  @value, @options = value.to_f, self.class.options.merge(options)
39
- raise M9t::UnitError.new("Unknown units '#{ @options[:units] }'. Known: #{ self.class::KNOWN_UNITS.collect{|unit| unit.to_s}.join(', ') }") \
40
- if not self.class::KNOWN_UNITS.find_index(@options[:units])
39
+ units_error(@options[:units]) if not self.class::KNOWN_UNITS.find_index(@options[:units])
41
40
  end
42
41
 
43
42
  # Returns the string representation of the measurement,
@@ -53,6 +52,11 @@ module M9t
53
52
  "#{ localized_value }%s#{ unit }" % (@options[:abbreviated] ? '' : ' ')
54
53
  end
55
54
 
55
+ private
56
+
57
+ def units_error(units)
58
+ raise M9t::UnitError.new("Unknown units '#{ units }'. Known: #{ self.class::KNOWN_UNITS.collect{|unit| unit.to_s}.join(', ') }")
59
+ end
56
60
  end
57
61
 
58
62
  end
data/lib/m9t/direction.rb CHANGED
@@ -4,6 +4,7 @@ require 'i18n'
4
4
 
5
5
  module M9t
6
6
 
7
+ # Represents a geographical direction
7
8
  class Direction
8
9
  DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :decimals => 5}
9
10
  KNOWN_UNITS = [:degrees, :compass]
@@ -17,35 +18,35 @@ module M9t
17
18
  class << self
18
19
 
19
20
  # Identity conversion. Simply returns the supplied number
20
- def to_degrees(d)
21
- d.to_f
21
+ def to_degrees(degrees)
22
+ degrees.to_f
22
23
  end
23
24
 
24
25
  # Given a value in degrees, returns the nearest (localized) compass direction
25
26
  # M9t::Directions.to_compass(42) => 'NE'
26
- def to_compass(d)
27
- sector = (normalize(d) / COMPASS_SECTOR_DEGREES).round
27
+ def to_compass(degrees)
28
+ sector = (normalize(degrees) / COMPASS_SECTOR_DEGREES).round
28
29
  I18n.t(self.measurement_name + '.sectors')[sector]
29
30
  end
30
31
 
31
32
  # Accepts a localized compass direction (e.g. 'N') and returns the equivalent M9t::Direction
32
33
  # M9t::Direction.compass('NE') => #<M9t::Direction:0xb76cc750 @value=45.0, @options={:units=>:degrees, :decimals=>5, :abbreviated=>false}>
33
- def compass(s)
34
- sector = I18n.t(self.measurement_name + '.sectors').find_index(s)
35
- raise "Compass direction '#{ s }' not recognised" if sector.nil?
34
+ def compass(compass_direction)
35
+ sector = I18n.t(self.measurement_name + '.sectors').find_index(compass_direction)
36
+ raise "Compass direction '#{ compass_direction }' not recognised" if sector.nil?
36
37
  new(sector.to_f * COMPASS_SECTOR_DEGREES)
37
38
  end
38
39
 
39
40
  # Reduce directions in degrees to the range [0, 360)
40
41
  # M9t::Direction.normalize(1000) => 280.0
41
- def normalize(d)
42
+ def normalize(degrees)
42
43
  case
43
- when d < 0
44
- normalize(d + CIRCLE)
45
- when d >= CIRCLE
46
- normalize(d - CIRCLE)
44
+ when degrees < 0
45
+ normalize(degrees + CIRCLE)
46
+ when degrees >= CIRCLE
47
+ normalize(degrees - CIRCLE)
47
48
  else
48
- d
49
+ degrees
49
50
  end
50
51
  end
51
52
 
data/lib/m9t/distance.rb CHANGED
@@ -5,6 +5,7 @@ require File.join(File.expand_path(File.dirname(__FILE__)), 'base')
5
5
 
6
6
  module M9t
7
7
 
8
+ # Represents a distance
8
9
  class Distance
9
10
  DEFAULT_OPTIONS = {:units => :meters, :abbreviated => false, :precision => 5}
10
11
  KNOWN_UNITS = [:meters, :miles, :kilometers]
@@ -23,23 +24,23 @@ module M9t
23
24
  end
24
25
 
25
26
  # 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
+ def miles(miles, options = {})
28
+ new(miles.to_f * METERS_PER_MILE, options)
28
29
  end
29
30
 
30
31
  # Identity conversion. Simply returns the supplied number
31
- def to_meters(m)
32
- m.to_f
32
+ def to_meters(meters)
33
+ meters.to_f
33
34
  end
34
35
 
35
36
  # Converts meters into kilometers
36
- def to_kilometers(m)
37
- m.to_f / METERS_PER_KILOMETER
37
+ def to_kilometers(meters)
38
+ meters.to_f / METERS_PER_KILOMETER
38
39
  end
39
40
 
40
41
  # Converts meters into miles
41
- def to_miles(m)
42
- m.to_f / METERS_PER_MILE
42
+ def to_miles(meters)
43
+ meters.to_f / METERS_PER_MILE
43
44
  end
44
45
 
45
46
  end
data/lib/m9t/i18n.rb CHANGED
@@ -8,10 +8,10 @@ module I18n
8
8
  # Handle non-English numerical separators
9
9
  # with I18n.locale = :it,
10
10
  # I18n.localize_float(5.23) => '5,23000'
11
- def I18n.localize_float(f, options = {})
11
+ def I18n.localize_float(float, options = {})
12
12
  format = options[:format] || '%f'
13
- s = format % f
14
- integers, decimal = s.split('.')
13
+ english = format % float
14
+ integers, decimal = english.split('.')
15
15
  integers ||= ''
16
16
 
17
17
  thousands_separator = I18n.t('numbers.thousands_separator')
data/lib/m9t/speed.rb CHANGED
@@ -5,6 +5,7 @@ require File.join(File.expand_path(File.dirname(__FILE__)), 'distance')
5
5
 
6
6
  module M9t
7
7
 
8
+ # Represents a speed
8
9
  class Speed
9
10
  DEFAULT_OPTIONS = {:units => :meters_per_second, :abbreviated => false, :precision => 5}
10
11
  KNOWN_UNITS = [:meters_per_second, :kilometers_per_hour, :miles_per_hour, :knots]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joe Yates