m9t 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/m9t.rb +1 -1
- data/lib/m9t/temperature.rb +47 -0
- data/locales/en.yml +7 -0
- data/locales/it.yml +7 -0
- data/test/temperature_test.rb +76 -0
- metadata +4 -2
data/lib/m9t.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
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
|
+
# Represents a temperature
|
9
|
+
# Using degrees (celcius), not kelvin, as default unit
|
10
|
+
class Temperature
|
11
|
+
DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :precision => 5}
|
12
|
+
KNOWN_UNITS = [:degrees, :kelvin, :fahrenheit]
|
13
|
+
|
14
|
+
# Conversions
|
15
|
+
ABSOLUTE_ZERO = -273.15
|
16
|
+
|
17
|
+
include M9t::Base
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
# Accepts a value in kelvin and returns the equivalent M9t::Temperature
|
22
|
+
def kelvin(kelvin, options = {})
|
23
|
+
new(kelvin.to_f + ABSOLUTE_ZERO, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Identity conversion. Simply returns the supplied number
|
27
|
+
def to_degrees(degrees)
|
28
|
+
degrees.to_f
|
29
|
+
end
|
30
|
+
|
31
|
+
# Converts degrees in kelvin
|
32
|
+
def to_kelvin(degrees)
|
33
|
+
degrees.to_f - ABSOLUTE_ZERO
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
alias :to_degrees :value
|
39
|
+
|
40
|
+
# Returns the value converted to kilometers
|
41
|
+
def to_kelvin
|
42
|
+
self.class.to_kelvin(@value)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/locales/en.yml
CHANGED
@@ -41,5 +41,12 @@ en:
|
|
41
41
|
zero: knots
|
42
42
|
one: knot
|
43
43
|
other: knots
|
44
|
+
temperature:
|
45
|
+
degrees:
|
46
|
+
full:
|
47
|
+
zero: degrees
|
48
|
+
one: degree
|
49
|
+
other: degrees
|
50
|
+
abbreviated: °C
|
44
51
|
direction:
|
45
52
|
sectors: ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
|
data/locales/it.yml
CHANGED
@@ -41,5 +41,12 @@ it:
|
|
41
41
|
zero: nodi
|
42
42
|
one: nodo
|
43
43
|
other: nodi
|
44
|
+
temperature:
|
45
|
+
degrees:
|
46
|
+
full:
|
47
|
+
zero: gradi
|
48
|
+
one: grado
|
49
|
+
other: gradi
|
50
|
+
abbreviated: °C
|
44
51
|
direction:
|
45
52
|
sectors: ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSO', 'SO', 'OSO', 'O', 'ONO', 'NO', 'NNO', 'N']
|
@@ -0,0 +1,76 @@
|
|
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 TestM9tTemperature < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def setup
|
11
|
+
I18n.locale = :en
|
12
|
+
M9t::Temperature.reset_options!
|
13
|
+
end
|
14
|
+
|
15
|
+
# Basic use
|
16
|
+
def test_new
|
17
|
+
assert_equal(45, M9t::Temperature.new(45).value)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Class methods
|
21
|
+
|
22
|
+
# Base class
|
23
|
+
|
24
|
+
def test_measurement_name
|
25
|
+
assert_equal('temperature', M9t::Temperature.measurement_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
# conversion constants
|
29
|
+
|
30
|
+
def test_knot_conversion
|
31
|
+
assert_in_delta(1.9438, M9t::Speed::MS_TO_KNOTS, 0.0001)
|
32
|
+
end
|
33
|
+
|
34
|
+
# input conversions
|
35
|
+
|
36
|
+
def test_class_kelvin
|
37
|
+
assert_in_delta(-273.15, M9t::Temperature.kelvin(0).value, 0.0001)
|
38
|
+
end
|
39
|
+
|
40
|
+
# output conversions
|
41
|
+
|
42
|
+
def test_class_to_kelvin
|
43
|
+
assert_in_delta(273.15, M9t::Temperature.to_kelvin(0), 0.0001)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Instance methods
|
47
|
+
|
48
|
+
# new
|
49
|
+
|
50
|
+
def test_unknown_units
|
51
|
+
assert_raises(M9t::UnitError) { M9t::Temperature.new(1, {:units => :foos}) }
|
52
|
+
end
|
53
|
+
|
54
|
+
# output conversions
|
55
|
+
|
56
|
+
def test_kmh
|
57
|
+
assert_equal(373.15, M9t::Temperature.new(100).to_kelvin)
|
58
|
+
end
|
59
|
+
|
60
|
+
# to_s
|
61
|
+
|
62
|
+
def test_to_s
|
63
|
+
assert_equal '100.00000 degrees', M9t::Temperature.new(100).to_s
|
64
|
+
I18n.locale = :it
|
65
|
+
assert_equal '100,00000 gradi', M9t::Temperature.new(100).to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_to_s_precision
|
69
|
+
assert_equal '135 degrees', M9t::Temperature.new(135, :precision => 0).to_s
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_to_s_abbreviated
|
73
|
+
assert_equal '135°C', M9t::Temperature.new(135, :abbreviated => true, :precision => 0).to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 8
|
9
|
+
version: 0.1.8
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joe Yates
|
@@ -50,9 +50,11 @@ files:
|
|
50
50
|
- lib/m9t/direction.rb
|
51
51
|
- lib/m9t/distance.rb
|
52
52
|
- lib/m9t/i18n.rb
|
53
|
+
- lib/m9t/temperature.rb
|
53
54
|
- test/direction_test.rb
|
54
55
|
- test/i18n_test.rb
|
55
56
|
- test/distance_test.rb
|
57
|
+
- test/temperature_test.rb
|
56
58
|
- test/test_all.rb
|
57
59
|
- test/speed_test.rb
|
58
60
|
- locales/it.yml
|