m9t 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/m9t.rb +3 -1
- data/lib/m9t/base.rb +6 -2
- data/lib/m9t/direction.rb +14 -13
- data/lib/m9t/distance.rb +9 -8
- data/lib/m9t/i18n.rb +3 -3
- data/lib/m9t/speed.rb +1 -0
- metadata +2 -2
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 =
|
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
|
-
|
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(
|
21
|
-
|
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(
|
27
|
-
sector = (normalize(
|
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(
|
34
|
-
sector = I18n.t(self.measurement_name + '.sectors').find_index(
|
35
|
-
raise "Compass direction '#{
|
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(
|
42
|
+
def normalize(degrees)
|
42
43
|
case
|
43
|
-
when
|
44
|
-
normalize(
|
45
|
-
when
|
46
|
-
normalize(
|
44
|
+
when degrees < 0
|
45
|
+
normalize(degrees + CIRCLE)
|
46
|
+
when degrees >= CIRCLE
|
47
|
+
normalize(degrees - CIRCLE)
|
47
48
|
else
|
48
|
-
|
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(
|
27
|
-
new(
|
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(
|
32
|
-
|
32
|
+
def to_meters(meters)
|
33
|
+
meters.to_f
|
33
34
|
end
|
34
35
|
|
35
36
|
# Converts meters into kilometers
|
36
|
-
def to_kilometers(
|
37
|
-
|
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(
|
42
|
-
|
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(
|
11
|
+
def I18n.localize_float(float, options = {})
|
12
12
|
format = options[:format] || '%f'
|
13
|
-
|
14
|
-
integers, decimal =
|
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]
|