m9t 0.3.4 → 0.3.5

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.
@@ -0,0 +1,6 @@
1
+ /bin/
2
+ /coverage/
3
+ /Gemfile.lock
4
+ /pkg/
5
+ /vendor/bundle/
6
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format progress
3
+
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.2.0
7
+ - jruby-19mode
8
+ branches:
9
+ only:
10
+ - master
11
+ script: "bundle exec rake spec"
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env rake
2
2
  require 'bundler/gem_tasks'
3
- require 'rcov/rcovtask' if RUBY_VERSION < '1.9'
4
3
  require 'rspec/core/rake_task'
5
4
 
6
5
  task :default => :spec
@@ -8,12 +7,3 @@ task :default => :spec
8
7
  RSpec::Core::RakeTask.new do |t|
9
8
  t.pattern = 'spec/**/*_spec.rb'
10
9
  end
11
-
12
- if RUBY_VERSION < '1.9'
13
- RSpec::Core::RakeTask.new('spec:rcov') do |t|
14
- t.pattern = 'spec/**/*_spec.rb'
15
- t.rcov = true
16
- t.rcov_opts = ['--exclude', 'spec/,/gems/']
17
- end
18
- end
19
-
data/lib/m9t.rb CHANGED
@@ -22,26 +22,9 @@
22
22
  #++
23
23
 
24
24
  # encoding: utf-8
25
- require 'i18n'
26
-
27
- locales_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'locales'))
28
- I18n.load_path += Dir.glob("#{ locales_path }/*.yml")
29
- I18n.reload!
30
-
31
- libs = %w( base direction distance i18n pressure speed temperature version )
32
- libs.each do | library |
33
- require "m9t/#{ library }"
34
- end
35
-
36
- module M9t
37
-
38
- # Base class for all M9t exceptions
39
- class M9tError < StandardError
40
- end
41
-
42
- # Raised when a M9t class receives an unrecogized ':units' value
43
- class UnitError < M9tError
44
- end
45
-
25
+ libs = %w(i18n base direction distance pressure speed temperature version)
26
+ libs.each do |library|
27
+ require "m9t/#{library}"
46
28
  end
47
29
 
30
+ module M9t; end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
2
+ require 'm9t/errors'
3
+ require 'm9t/i18n'
3
4
 
4
5
  module M9t
5
6
  module Base
@@ -31,7 +32,6 @@ module M9t
31
32
  return true if legal_conversion?(from, to)
32
33
  end
33
34
  return legal_constructor?(name)
34
- super
35
35
  end
36
36
 
37
37
  private
@@ -50,7 +50,7 @@ module M9t
50
50
 
51
51
  def define_conversion(from, to)
52
52
  self.class.instance_exec do
53
- define_method("#{ from }_to_#{ to }") do |value|
53
+ define_method("#{from}_to_#{to}") do |value|
54
54
  convert(from.to_sym, to.to_sym, value)
55
55
  end
56
56
  end
@@ -64,8 +64,8 @@ module M9t
64
64
  # from the unit and returns an instance
65
65
  def define_constructor(name)
66
66
  self.class.instance_exec do
67
- define_method( name.to_sym ) do | *args |
68
- new( args[ 0 ].to_f / self::CONVERSIONS[ name ] )
67
+ define_method(name.to_sym) do | *args |
68
+ new(args[0].to_f / self::CONVERSIONS[name])
69
69
  end
70
70
  end
71
71
  end
@@ -114,7 +114,7 @@ module M9t
114
114
  attr_reader :value, :options
115
115
  alias :to_f :value
116
116
 
117
- def initialize( value )
117
+ def initialize(value)
118
118
  @value = value.to_f
119
119
  end
120
120
 
@@ -136,23 +136,28 @@ module M9t
136
136
 
137
137
  # Returns the string representation of the measurement,
138
138
  # taking into account locale, desired units and abbreviation.
139
- def to_s( options = {} )
140
- options = self.class.options.merge( options )
141
- units_error( options[ :units ] ) if not self.class::CONVERSIONS.has_key?( options[ :units ] )
142
- value_in_units = self.send("to_#{ options[:units] }")
143
- localized_value = I18n.localize_float(value_in_units, {:format => "%0.#{ options[:precision] }f"})
139
+ def to_s(options = {})
140
+ options = self.class.options.merge(options)
141
+ if not self.class::CONVERSIONS.has_key?(options[:units])
142
+ units_error(options[:units])
143
+ end
144
+ value_in_units = self.send("to_#{options[:units]}")
145
+ localized_value = I18n.localize_float(
146
+ value_in_units, {format: "%0.#{options[:precision]}f"}
147
+ )
144
148
 
145
149
  key = 'units.' + self.class.measurement_name + '.' + options[:units].to_s
146
150
  options[:abbreviated] ? key += '.abbreviated' : key += '.full'
147
- unit = I18n.t(key, {:count => value_in_units})
151
+ unit = I18n.t(key, {count: value_in_units})
148
152
 
149
- "#{ localized_value }%s#{ unit }" % (options[:abbreviated] ? '' : ' ')
153
+ "#{localized_value}%s#{unit}" % (options[:abbreviated] ? '' : ' ')
150
154
  end
151
155
 
152
156
  private
153
157
 
154
158
  def units_error(units)
155
- raise M9t::UnitError.new("Unknown units '#{ units }'. Known: #{ self.class::CONVERSIONS.keys.collect{|unit| unit.to_s}.join(', ') }")
159
+ known = self.class::CONVERSIONS.keys.collect{|unit| unit.to_s}.join(', ')
160
+ raise M9t::UnitError.new("Unknown units '#{units}'. Known: #{known}")
156
161
  end
157
162
 
158
163
  def extract_to(name)
@@ -176,4 +181,3 @@ module M9t
176
181
  end
177
182
  end
178
183
  end
179
-
@@ -1,14 +1,13 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
2
+ require 'm9t/base'
3
3
 
4
4
  module M9t
5
-
6
5
  # Represents a geographical direction
7
6
  class Direction
8
- DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :decimals => 5}
7
+ DEFAULT_OPTIONS = {units: :degrees, abbreviated: false, decimals: 5}
9
8
  CONVERSIONS = {
10
- :degrees => 1.0,
11
- :compass => nil,
9
+ degrees: 1.0,
10
+ compass: nil,
12
11
  }
13
12
 
14
13
  # Conversions
@@ -18,7 +17,6 @@ module M9t
18
17
  include M9t::Base
19
18
 
20
19
  class << self
21
-
22
20
  # Given a value in degrees, returns the nearest (localized) compass direction
23
21
  # M9t::Directions.to_compass(42) => 'NE'
24
22
  def degrees_to_compass(degrees)
@@ -34,7 +32,7 @@ module M9t
34
32
  # M9t::Direction.compass('NE') => #<M9t::Direction:0xb76cc750 @value=45.0, @options={:units=>:degrees, :decimals=>5, :abbreviated=>false}>
35
33
  def compass(compass_direction)
36
34
  sector = I18n.t(self.measurement_name + '.sectors').find_index(compass_direction)
37
- raise "Compass direction '#{ compass_direction }' not recognised" if sector.nil?
35
+ raise "Compass direction '#{compass_direction}' not recognised" if sector.nil?
38
36
  new(sector.to_f * COMPASS_SECTOR_DEGREES)
39
37
  end
40
38
 
@@ -50,7 +48,6 @@ module M9t
50
48
  degrees
51
49
  end
52
50
  end
53
-
54
51
  end
55
52
 
56
53
  # Handles the special case where compass directions are the desired output.
@@ -65,7 +62,5 @@ module M9t
65
62
  def to_compass
66
63
  self.class.degrees_to_compass(@value)
67
64
  end
68
-
69
65
  end
70
-
71
66
  end
@@ -1,23 +1,21 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
3
2
  require 'm9t/base'
4
3
 
5
4
  module M9t
6
-
7
5
  # Represents a distance
8
6
  class Distance
9
- DEFAULT_OPTIONS = {:units => :meters, :abbreviated => false, :precision => 5}
10
- CONVERSIONS = {
11
- :meters => 1.0,
12
- :kilometers => 1.0 / 1000.0,
13
- :feet => 1.0 / 0.3048,
14
- :miles => 1.0 / 1609.344
7
+ DEFAULT_OPTIONS = {
8
+ units: :meters, abbreviated: false, precision: 5
9
+ }
10
+ CONVERSIONS = {
11
+ meters: 1.0,
12
+ kilometers: 1.0 / 1000.0,
13
+ feet: 1.0 / 0.3048,
14
+ miles: 1.0 / 1609.344
15
15
  }
16
16
 
17
17
  include M9t::Base
18
18
 
19
19
  alias :to_meters :value
20
-
21
20
  end
22
-
23
21
  end
@@ -0,0 +1,7 @@
1
+ # Base class for all M9t exceptions
2
+ class M9t::M9tError < StandardError
3
+ end
4
+
5
+ # Raised when a M9t class receives an unrecogized ':units' value
6
+ class M9t::UnitError < M9t::M9tError
7
+ end
@@ -1,9 +1,14 @@
1
1
  # encoding: utf-8
2
2
  require 'i18n'
3
3
 
4
+ locales_path = File.expand_path(
5
+ File.join('..', '..', 'locales'), File.dirname(__FILE__)
6
+ )
7
+ I18n.load_path += Dir.glob("#{ locales_path }/*.yml")
8
+ I18n.reload!
9
+
4
10
  # Monkey patch I18n
5
11
  module I18n
6
-
7
12
  # Handle non-English numerical separators
8
13
  # with I18n.locale = :it,
9
14
  # I18n.localize_float(5.23) => '5,23000'
@@ -22,4 +27,3 @@ module I18n
22
27
  integers + decimal_separator + decimal
23
28
  end
24
29
  end
25
-
@@ -1,23 +1,18 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
3
2
  require 'm9t/base'
4
3
 
5
4
  module M9t
6
-
7
5
  # Represents atmospheric (or other) pressure
8
6
  class Pressure
9
- DEFAULT_OPTIONS = {:units => :bar, :abbreviated => false, :precision => 5}
7
+ DEFAULT_OPTIONS = {units: :bar, abbreviated: false, precision: 5}
10
8
  CONVERSIONS = {
11
- :bar => 1.0,
12
- :pascals => 1.0 / 0.00001,
13
- :hectopascals => 1.0 / 0.001,
14
- :kilopascals => 1.0 / 0.01,
15
- :inches_of_mercury => 1.0 / ( 3386.389 * 0.00001 )
9
+ bar: 1.0,
10
+ pascals: 1.0 / 0.00001,
11
+ hectopascals: 1.0 / 0.001,
12
+ kilopascals: 1.0 / 0.01,
13
+ inches_of_mercury: 1.0 / (3386.389 * 0.00001)
16
14
  }
17
15
 
18
16
  include M9t::Base
19
-
20
17
  end
21
-
22
18
  end
23
-
@@ -1,28 +1,26 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
2
+ require 'm9t/base'
3
3
  require 'm9t/distance'
4
4
 
5
5
  module M9t
6
-
7
6
  # Represents a speed
8
7
  class Speed
9
- DEFAULT_OPTIONS = {:units => :meters_per_second, :abbreviated => false, :precision => 5}
8
+ DEFAULT_OPTIONS = {
9
+ units: :meters_per_second, abbreviated: false, precision: 5
10
+ }
10
11
  SECONDS_PER_HOUR = 60.0 * 60
11
- KMH = M9t::Distance::CONVERSIONS[:kilometers] * SECONDS_PER_HOUR
12
- MPH = M9t::Distance::CONVERSIONS[:miles] * SECONDS_PER_HOUR
13
- KNOTS = KMH / 1.852
12
+ KMH = M9t::Distance::CONVERSIONS[:kilometers] * SECONDS_PER_HOUR
13
+ MPH = M9t::Distance::CONVERSIONS[:miles] * SECONDS_PER_HOUR
14
+ KNOTS = KMH / 1.852
14
15
  CONVERSIONS = {
15
- :meters_per_second => 1.0,
16
- :kilometers_per_hour => KMH,
17
- :miles_per_hour => MPH,
18
- :knots => KNOTS
16
+ meters_per_second: 1.0,
17
+ kilometers_per_hour: KMH,
18
+ miles_per_hour: MPH,
19
+ knots: KNOTS
19
20
  }
20
21
 
21
22
  include M9t::Base
22
23
 
23
24
  alias :to_meters_per_second :value
24
-
25
25
  end
26
-
27
26
  end
28
-
@@ -1,17 +1,17 @@
1
1
  # encoding: utf-8
2
- require 'i18n'
3
2
  require 'm9t/base'
4
3
 
5
4
  module M9t
6
-
7
5
  # Represents a temperature
8
6
  # Using degrees (celcius), not kelvin, as default unit
9
7
  class Temperature
10
- DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :precision => 5}
11
- CONVERSIONS = {
12
- :degrees => 1.0,
13
- :kelvin => nil,
14
- :fahrenheit => nil
8
+ DEFAULT_OPTIONS = {
9
+ units: :degrees, abbreviated: false, precision: 5
10
+ }
11
+ CONVERSIONS = {
12
+ degrees: 1.0,
13
+ kelvin: nil,
14
+ fahrenheit: nil
15
15
  }
16
16
 
17
17
  # Conversions
@@ -20,14 +20,13 @@ module M9t
20
20
  include M9t::Base
21
21
 
22
22
  class << self
23
-
24
23
  # Accepts a value in kelvin and returns the equivalent M9t::Temperature
25
- def kelvin( kelvin )
26
- new( kelvin.to_f + ABSOLUTE_ZERO )
24
+ def kelvin(kelvin)
25
+ new(kelvin.to_f + ABSOLUTE_ZERO)
27
26
  end
28
27
 
29
- def fahrenheit( fahrenheit )
30
- new( fahrenheit_to_degrees( fahrenheit ) )
28
+ def fahrenheit(fahrenheit)
29
+ new(fahrenheit_to_degrees(fahrenheit))
31
30
  end
32
31
 
33
32
  # Converts degrees to kelvin
@@ -47,9 +46,8 @@ module M9t
47
46
 
48
47
  # Converts Fahrenheit to degrees
49
48
  def fahrenheit_to_degrees(fahrenheit)
50
- fahrenheit.to_f - 32 * 5.0 / 9.0
49
+ (fahrenheit.to_f - 32) * 5.0 / 9.0
51
50
  end
52
-
53
51
  end
54
52
 
55
53
  alias :to_degrees :value
@@ -62,8 +60,5 @@ module M9t
62
60
  def to_fahrenheit
63
61
  self.class.to_fahrenheit(@value)
64
62
  end
65
-
66
63
  end
67
-
68
64
  end
69
-
@@ -1,12 +1,9 @@
1
1
  module M9t
2
-
3
2
  module VERSION #:nodoc:
4
3
  MAJOR = 0
5
4
  MINOR = 3
6
- TINY = 4
5
+ TINY = 5
7
6
 
8
7
  STRING = [MAJOR, MINOR, TINY].join('.')
9
8
  end
10
-
11
9
  end
12
-
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.unshift( File.dirname(__FILE__) + '/lib' )
2
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
3
3
  require 'm9t/version'
4
4
 
5
5
  gemspec = Gem::Specification.new do |s|
@@ -14,8 +14,8 @@ gemspec = Gem::Specification.new do |s|
14
14
  s.author = 'Joe Yates'
15
15
  s.email = 'joe.g.yates@gmail.com'
16
16
 
17
- s.files = `git ls-files`.lines.map( &:chomp! ).reject { | f | f[ 0 .. 0 ] == '.' }
18
- s.test_files = `git ls-files`.lines.map( &:chomp! ).select { | f | f =~ /_test.rb$/ }
17
+ s.files = `git ls-files`.lines.map(&:chomp!)
18
+ s.test_files = `git ls-files spec`.lines.map(&:chomp!)
19
19
  s.require_paths = ['lib']
20
20
 
21
21
  s.rubyforge_project = 'nowarning'
@@ -23,13 +23,6 @@ gemspec = Gem::Specification.new do |s|
23
23
  s.add_dependency 'rake'
24
24
  s.add_dependency 'i18n', '>= 0.3.5'
25
25
 
26
- s.add_development_dependency 'rspec'
27
- if RUBY_VERSION < '1.9'
28
- if RUBY_PLATFORM != 'java'
29
- s.add_development_dependency 'rcov'
30
- end
31
- else
32
- s.add_development_dependency 'simplecov'
33
- end
26
+ s.add_development_dependency 'rspec', '>= 3.0.0'
27
+ s.add_development_dependency 'simplecov' if RUBY_PLATFORM != 'java'
34
28
  end
35
-
@@ -1,23 +1,17 @@
1
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
2
1
  require 'rspec'
3
- require 'rspec/autorun'
4
2
 
5
- if RUBY_VERSION > '1.9'
3
+ if RUBY_PLATFORM != 'java'
6
4
  require 'simplecov'
7
- if ENV['COVERAGE']
8
- SimpleCov.start do
9
- add_filter '/spec/'
10
- end
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
11
7
  end
12
8
  end
13
9
 
14
10
  require 'm9t'
15
11
 
16
12
  RSpec.configure do |config|
17
- config.treat_symbols_as_metadata_keys_with_true_values = true
18
13
  config.run_all_when_everything_filtered = true
19
14
  config.filter_run :focus
20
15
 
21
16
  config.order = 'random'
22
17
  end
23
-
@@ -1,15 +1,15 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/base'
3
3
 
4
4
  class SomeMeasurement
5
5
  DEFAULT_OPTIONS = {
6
- :units => :foos,
7
- :abbreviated => false,
8
- :decimals => 1
6
+ units: :foos,
7
+ abbreviated: false,
8
+ decimals: 1
9
9
  }
10
10
  CONVERSIONS = {
11
- :foos => 1.0,
12
- :bars => 1 / 42.0
11
+ foos: 1.0,
12
+ bars: 1 / 42.0
13
13
  }
14
14
 
15
15
  include M9t::Base
@@ -18,6 +18,16 @@ end
18
18
  class SomeDerivedMeasurement < SomeMeasurement; end
19
19
 
20
20
  describe M9t::Base do
21
+ describe '.respond_to?' do
22
+ it 'is true for conversion between known units' do
23
+ expect(SomeMeasurement.respond_to?(:foos_to_bars)).to be_truthy
24
+ end
25
+
26
+ it 'is false for unknown units' do
27
+ expect(SomeMeasurement.respond_to?(:bazs_to_bars)).to be_falsey
28
+ end
29
+ end
30
+
21
31
  describe '.method_missing' do
22
32
  it 'handles conversion between known units' do
23
33
  expect(SomeMeasurement.foos_to_bars(3.0)).to be_a(Float)
@@ -30,6 +40,24 @@ describe M9t::Base do
30
40
  end
31
41
  end
32
42
 
43
+ describe '#respond_to?' do
44
+ subject { SomeMeasurement.new(3.0) }
45
+
46
+ it 'is true for conversion to known units' do
47
+ expect(subject.respond_to?(:to_bars)).to be_truthy
48
+ end
49
+
50
+ it 'is false for unknown units' do
51
+ expect(subject.respond_to?(:to_bazs)).to be_falsey
52
+ end
53
+
54
+ context 'for unrecognized calls' do
55
+ it 'calls super' do
56
+ expect(subject.respond_to?(:ciao)).to be_falsey
57
+ end
58
+ end
59
+ end
60
+
33
61
  describe '#method_missing' do
34
62
  subject { SomeMeasurement.new(3.0) }
35
63
 
@@ -50,4 +78,3 @@ describe M9t::Base do
50
78
  end
51
79
  end
52
80
  end
53
-
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/direction'
3
3
 
4
4
  describe M9t::Direction do
5
5
  before do
@@ -19,7 +19,7 @@ describe M9t::Direction do
19
19
 
20
20
  context 'abbreviated' do
21
21
  it 'is false' do
22
- expect(M9t::Direction.options[:abbreviated]).to be_false
22
+ expect(M9t::Direction.options[:abbreviated]).to be_falsey
23
23
  end
24
24
  end
25
25
 
@@ -156,7 +156,7 @@ describe M9t::Direction do
156
156
  subject { M9t::Direction.new(135) }
157
157
 
158
158
  it 'uses the symbol' do
159
- expect(subject.to_s(:abbreviated => true)).to eq('135°')
159
+ expect(subject.to_s(abbreviated: true)).to eq('135°')
160
160
  end
161
161
  end
162
162
 
@@ -164,12 +164,12 @@ describe M9t::Direction do
164
164
  subject { M9t::Direction.new(225) }
165
165
 
166
166
  it 'works' do
167
- expect(subject.to_s(:units => :compass)).to eq('SW')
167
+ expect(subject.to_s(units: :compass)).to eq('SW')
168
168
  end
169
169
 
170
170
  it 'translates' do
171
171
  I18n.locale = :it
172
- expect(subject.to_s(:units => :compass)).to eq('SO')
172
+ expect(subject.to_s(units: :compass)).to eq('SO')
173
173
  end
174
174
  end
175
175
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/distance'
3
3
 
4
4
  describe M9t::Distance do
5
5
  context 'class methods' do
@@ -13,7 +13,7 @@ describe M9t::Distance do
13
13
  end
14
14
 
15
15
  it 'not abbreviated' do
16
- expect(M9t::Distance.options[:abbreviated]).to be_false
16
+ expect(M9t::Distance.options[:abbreviated]).to be_falsey
17
17
  end
18
18
 
19
19
  it 'units: meters' do
@@ -76,32 +76,32 @@ describe M9t::Distance do
76
76
 
77
77
  it 'handles singluar' do
78
78
  distance = M9t::Distance.new(1)
79
- expect(distance.to_s(:precision => 0)).to eq('1 meter')
79
+ expect(distance.to_s(precision: 0)).to eq('1 meter')
80
80
  end
81
81
 
82
82
  it 'handles plural' do
83
83
  distance = M9t::Distance.new(10)
84
- expect(distance.to_s(:precision => 0)).to eq('10 meters')
84
+ expect(distance.to_s(precision: 0)).to eq('10 meters')
85
85
  end
86
86
 
87
87
  it 'handles abbreviation' do
88
88
  distance = M9t::Distance.new(0.3)
89
- expect(distance.to_s(:abbreviated => true)).to eq('0.30000m')
89
+ expect(distance.to_s(abbreviated: true)).to eq('0.30000m')
90
90
  end
91
91
 
92
92
  specify 'units: kilometers' do
93
93
  distance = M9t::Distance.new( 156003 )
94
- expect(distance.to_s(:precision => 1, :units => :kilometers)).to eq('156.0 kilometers')
94
+ expect(distance.to_s(precision: 1, units: :kilometers)).to eq('156.0 kilometers')
95
95
  end
96
96
 
97
97
  specify 'units: miles, singular' do
98
98
  marathon = M9t::Distance.miles(26.21875)
99
- expect(marathon.to_s(:units => :miles, :precision => 0)).to eq('26 miles')
99
+ expect(marathon.to_s(units: :miles, precision: 0)).to eq('26 miles')
100
100
  end
101
101
 
102
102
  specify 'units: miles, plural' do
103
103
  ten_km = M9t::Distance.new(10000)
104
- expect(ten_km.to_s(:units => :miles, :precision => 1)).to eq('6.2 miles')
104
+ expect(ten_km.to_s(units: :miles, precision: 1)).to eq('6.2 miles')
105
105
  end
106
106
 
107
107
  context 'i18n' do
@@ -111,17 +111,17 @@ describe M9t::Distance do
111
111
 
112
112
  specify 'precision: 0' do
113
113
  distance = M9t::Distance.new(10)
114
- expect(distance.to_s(:precision => 0)).to eq('10 metri')
114
+ expect(distance.to_s(precision: 0)).to eq('10 metri')
115
115
  end
116
116
 
117
117
  specify 'units: miles' do
118
118
  marathon = M9t::Distance.miles(26.21875)
119
- expect(marathon.to_s(:units => :miles, :precision => 0)).to eq('26 miglia')
119
+ expect(marathon.to_s(units: :miles, precision: 0)).to eq('26 miglia')
120
120
  end
121
121
 
122
122
  specify 'units: miles, precision: 1' do
123
123
  ten_km = M9t::Distance.new(10000)
124
- expect(ten_km.to_s(:units => :miles, :precision => 1)).to eq('6,2 miglia')
124
+ expect(ten_km.to_s(units: :miles, precision: 1)).to eq('6,2 miglia')
125
125
  end
126
126
  end
127
127
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/i18n'
3
3
 
4
4
  describe I18n do
5
5
  before do
@@ -49,7 +49,7 @@ describe I18n do
49
49
 
50
50
  it 'accepts a format indicator' do
51
51
  I18n.locale = :en
52
- expect(I18n.localize_float(1.5, {:format => '%0.1f'})).to eq('1.5')
52
+ expect(I18n.localize_float(1.5, {format: '%0.1f'})).to eq('1.5')
53
53
  end
54
54
  end
55
55
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/pressure'
3
3
 
4
4
  describe M9t::Pressure do
5
5
  context 'class methods' do
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/speed'
3
3
 
4
4
  describe M9t::Speed do
5
5
  context 'conversion constants' do
@@ -12,7 +12,7 @@ describe M9t::Speed do
12
12
  it 'gives an error for unknown units' do
13
13
  speed = M9t::Speed.new(10)
14
14
  expect {
15
- speed.to_s( :units => :foos )
15
+ speed.to_s(units: :foos)
16
16
  }.to raise_error(M9t::UnitError)
17
17
  end
18
18
  end
@@ -83,21 +83,21 @@ describe M9t::Speed do
83
83
 
84
84
  specify 'precision' do
85
85
  I18n.locale = :en
86
- expect(subject.to_s(:precision => 0)).to eq('135 meters per second')
86
+ expect(subject.to_s(precision: 0)).to eq('135 meters per second')
87
87
  end
88
88
 
89
89
  specify 'abbreviated' do
90
- expect(subject.to_s(:abbreviated => true, :precision => 0)).to eq('135m/s')
90
+ expect(subject.to_s(abbreviated: true, precision: 0)).to eq('135m/s')
91
91
  end
92
92
 
93
93
  specify 'units: knots, en' do
94
94
  I18n.locale = :en
95
- expect(subject.to_s(:units => :knots, :precision => 0)).to eq('262 knots')
95
+ expect(subject.to_s(units: :knots, precision: 0)).to eq('262 knots')
96
96
  end
97
97
 
98
98
  specify 'units: knots, it' do
99
99
  I18n.locale = :it
100
- expect(subject.to_s(:units => :knots, :precision => 0)).to eq('262 nodi')
100
+ expect(subject.to_s(units: :knots, precision: 0)).to eq('262 nodi')
101
101
  end
102
102
  end
103
103
  end
@@ -1,7 +1,12 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require 'm9t/temperature'
3
3
 
4
4
  describe M9t::Temperature do
5
+ CONVERSIONS = [
6
+ [:kelvin, 100, 373.15],
7
+ [:fahrenheit, 100, 212.0],
8
+ ]
9
+
5
10
  context 'constants' do
6
11
  specify 'absolute zero' do
7
12
  expect(M9t::Temperature::ABSOLUTE_ZERO).to be_within(0.0001).of(-273.15)
@@ -13,7 +18,7 @@ describe M9t::Temperature do
13
18
  expect(M9t::Temperature.options[:units]).to eq(:degrees)
14
19
  end
15
20
 
16
- specify 'precision: 2' do
21
+ specify 'precision: 5' do
17
22
  expect(M9t::Temperature.options[:precision]).to eq(5)
18
23
  end
19
24
  end
@@ -32,39 +37,44 @@ describe M9t::Temperature do
32
37
  end
33
38
 
34
39
  context 'conversion factories' do
35
- [
36
- [:kelvin, -273.15],
37
- [:fahrenheit, -17.77777778],
38
- ].each do |unit, expected|
39
- specify unit do
40
- expect(M9t::Temperature.send(unit, 0.0).value).to be_within(expected.abs / 1000.0).of(expected)
40
+ CONVERSIONS.each do |unit, degrees, other|
41
+ specify ".#{unit}" do
42
+ expect(M9t::Temperature.send(unit, other).value).
43
+ to be_within(degrees.abs / 1000.0).of(degrees)
41
44
  end
42
45
  end
43
46
  end
44
47
 
45
48
  context 'conversions' do
46
- [
47
- [:kelvin, 273.15],
48
- [:fahrenheit, 32.0],
49
- ].each do |unit, expected|
50
- method = :"to_#{unit}"
51
- specify method do
52
- expect(M9t::Temperature.send(method, 0)).to eq(expected)
49
+ context 'from degrees' do
50
+ CONVERSIONS.each do |unit, degrees, other|
51
+ method = :"to_#{unit}"
52
+ specify method do
53
+ expect(M9t::Temperature.send(method, degrees)).to eq(other)
54
+ end
55
+ end
56
+ end
57
+
58
+ context 'to degrees' do
59
+ CONVERSIONS.each do |unit, degrees, other|
60
+ method = :"#{unit}_to_degrees"
61
+ specify method do
62
+ expect(M9t::Temperature.send(method, other)).to eq(degrees)
63
+ end
53
64
  end
54
65
  end
55
66
  end
56
67
  end
57
68
 
58
69
  context 'conversions' do
59
- subject { M9t::Temperature.new(100) }
60
-
61
- [
62
- [:kelvin, 373.15],
63
- [:fahrenheit, 212.0],
64
- ].each do |unit, expected|
65
- method = :"to_#{unit}"
66
- specify method do
67
- expect(subject.send(method)).to be_within(expected.abs / 1000.0).of(expected)
70
+ context 'from degrees' do
71
+ CONVERSIONS.each do |unit, degrees, other|
72
+ subject { M9t::Temperature.new(degrees) }
73
+
74
+ method = :"to_#{unit}"
75
+ specify method do
76
+ expect(subject.send(method)).to be_within(other.abs / 1000.0).of(other)
77
+ end
68
78
  end
69
79
  end
70
80
  end
@@ -84,27 +94,32 @@ describe M9t::Temperature do
84
94
 
85
95
  specify 'precision' do
86
96
  I18n.locale = :en
87
- expect(subject.to_s(:precision => 0)).to eq('135 degrees')
97
+ expect(subject.to_s(precision: 0)).to eq('135 degrees')
88
98
  end
89
99
 
90
100
  specify 'abbreviated, en' do
91
101
  I18n.locale = :en
92
- expect(subject.to_s(:abbreviated => true, :precision => 0)).to eq('135°C')
102
+ expect(subject.to_s(abbreviated: true, precision: 0)).
103
+ to eq('135°C')
93
104
  end
94
105
 
95
106
  specify 'kelvin, en' do
96
107
  I18n.locale = :en
97
- expect(subject.to_s(:units => :kelvin, :precision => 2)).to eq('408.15 kelvin')
108
+ expect(subject.to_s(units: :kelvin, precision: 2)).
109
+ to eq('408.15 kelvin')
98
110
  end
99
111
 
100
112
  specify 'kelvin, it' do
101
113
  I18n.locale = :it
102
- expect(subject.to_s(:units => :kelvin, :precision => 2)).to eq('408,15 kelvin')
114
+ expect(subject.to_s(units: :kelvin, precision: 2)).
115
+ to eq('408,15 kelvin')
103
116
  end
104
117
 
105
118
  specify 'abbreviated, kelvin, it' do
106
119
  I18n.locale = :it
107
- expect(subject.to_s(:units => :kelvin, :abbreviated => true, :precision => 2)).to eq('408,15K')
120
+ expect(
121
+ subject.to_s(units: :kelvin, abbreviated: true, precision: 2)
122
+ ).to eq('408,15K')
108
123
  end
109
124
  end
110
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m9t
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-25 00:00:00.000000000 Z
12
+ date: 2015-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 3.0.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 3.0.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: simplecov
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +82,9 @@ executables: []
82
82
  extensions: []
83
83
  extra_rdoc_files: []
84
84
  files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .travis.yml
85
88
  - GPL-LICENSE.txt
86
89
  - Gemfile
87
90
  - MIT-LICENSE.txt
@@ -91,6 +94,7 @@ files:
91
94
  - lib/m9t/base.rb
92
95
  - lib/m9t/direction.rb
93
96
  - lib/m9t/distance.rb
97
+ - lib/m9t/errors.rb
94
98
  - lib/m9t/i18n.rb
95
99
  - lib/m9t/pressure.rb
96
100
  - lib/m9t/speed.rb
@@ -122,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
126
  version: '0'
123
127
  segments:
124
128
  - 0
125
- hash: -2793566875705204263
129
+ hash: -4097874954869017235
126
130
  required_rubygems_version: !ruby/object:Gem::Requirement
127
131
  none: false
128
132
  requirements:
@@ -131,11 +135,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
135
  version: '0'
132
136
  segments:
133
137
  - 0
134
- hash: -2793566875705204263
138
+ hash: -4097874954869017235
135
139
  requirements: []
136
140
  rubyforge_project: nowarning
137
- rubygems_version: 1.8.23
141
+ rubygems_version: 1.8.23.2
138
142
  signing_key:
139
143
  specification_version: 3
140
144
  summary: Measurements and conversions library for Ruby
141
- test_files: []
145
+ test_files:
146
+ - spec/spec_helper.rb
147
+ - spec/unit/base_spec.rb
148
+ - spec/unit/direction_spec.rb
149
+ - spec/unit/distance_spec.rb
150
+ - spec/unit/i18n_spec.rb
151
+ - spec/unit/pressure_spec.rb
152
+ - spec/unit/speed_spec.rb
153
+ - spec/unit/temperature_spec.rb