m9t 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/m9t/distance.rb CHANGED
@@ -1,23 +1,20 @@
1
- # encoding: utf-8
2
- require 'i18n'
3
- require 'm9t/base'
1
+ require "m9t/base"
4
2
 
5
3
  module M9t
6
-
7
4
  # Represents a distance
8
5
  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
6
+ DEFAULT_OPTIONS = {
7
+ units: :meters, abbreviated: false, precision: 5
8
+ }
9
+ CONVERSIONS = {
10
+ meters: 1.0,
11
+ kilometers: 1.0 / 1000.0,
12
+ feet: 1.0 / 0.3048,
13
+ miles: 1.0 / 1609.344
15
14
  }
16
15
 
17
16
  include M9t::Base
18
17
 
19
18
  alias :to_meters :value
20
-
21
19
  end
22
-
23
20
  end
data/lib/m9t/errors.rb ADDED
@@ -0,0 +1,9 @@
1
+ module M9t
2
+ # Base class for all M9t exceptions
3
+ class M9tError < StandardError
4
+ end
5
+
6
+ # Raised when a M9t class receives an unrecogized ':units' value
7
+ class UnitError < M9t::M9tError
8
+ end
9
+ end
data/lib/m9t/i18n.rb CHANGED
@@ -1,25 +1,33 @@
1
- # encoding: utf-8
2
- require 'i18n'
1
+ require "i18n"
2
+
3
+ locales_path = File.expand_path(
4
+ File.join("..", "..", "locales"), File.dirname(__FILE__)
5
+ )
6
+ I18n.load_path += Dir.glob("#{ locales_path }/*.yml")
7
+ I18n.reload!
3
8
 
4
9
  # Monkey patch I18n
10
+ # i18n does not handle localizing numbers.
11
+ # See the following GitHub issues:
12
+ # * https://github.com/svenfuchs/i18n/issues/328
13
+ # * https://github.com/svenfuchs/i18n/issues/135
14
+ # * https://github.com/svenfuchs/i18n/issues/183
5
15
  module I18n
6
-
7
16
  # Handle non-English numerical separators
8
17
  # with I18n.locale = :it,
9
- # I18n.localize_float(5.23) => '5,23000'
18
+ # I18n.localize_float(5.23) => "5,23000"
10
19
  def I18n.localize_float(float, options = {})
11
- format = options[:format] || '%f'
20
+ format = options[:format] || "%f"
12
21
  english = format % float
13
- integers, decimal = english.split('.')
14
- integers ||= ''
22
+ integers, decimal = english.split(".")
23
+ integers ||= ""
15
24
 
16
- thousands_separator = I18n.t('numbers.thousands_separator')
17
- integers.gsub(',', thousands_separator)
25
+ thousands_separator = I18n.t("numbers.thousands_separator")
26
+ integers.gsub(",", thousands_separator)
18
27
 
19
28
  return integers if decimal.nil?
20
29
 
21
- decimal_separator = I18n.t('numbers.decimal_separator')
30
+ decimal_separator = I18n.t("numbers.decimal_separator")
22
31
  integers + decimal_separator + decimal
23
32
  end
24
33
  end
25
-
data/lib/m9t/pressure.rb CHANGED
@@ -1,23 +1,17 @@
1
- # encoding: utf-8
2
- require 'i18n'
3
- require 'm9t/base'
1
+ require "m9t/base"
4
2
 
5
3
  module M9t
6
-
7
4
  # Represents atmospheric (or other) pressure
8
5
  class Pressure
9
- DEFAULT_OPTIONS = {:units => :bar, :abbreviated => false, :precision => 5}
6
+ DEFAULT_OPTIONS = {units: :bar, abbreviated: false, precision: 5}
10
7
  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 )
8
+ bar: 1.0,
9
+ pascals: 1.0 / 0.00001,
10
+ hectopascals: 1.0 / 0.001,
11
+ kilopascals: 1.0 / 0.01,
12
+ inches_of_mercury: 1.0 / (3386.389 * 0.00001)
16
13
  }
17
14
 
18
15
  include M9t::Base
19
-
20
16
  end
21
-
22
17
  end
23
-
data/lib/m9t/speed.rb CHANGED
@@ -1,28 +1,25 @@
1
- # encoding: utf-8
2
- require 'i18n'
3
- require 'm9t/distance'
1
+ require "m9t/base"
2
+ require "m9t/distance"
4
3
 
5
4
  module M9t
6
-
7
5
  # Represents a speed
8
6
  class Speed
9
- DEFAULT_OPTIONS = {:units => :meters_per_second, :abbreviated => false, :precision => 5}
7
+ DEFAULT_OPTIONS = {
8
+ units: :meters_per_second, abbreviated: false, precision: 5
9
+ }
10
10
  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
11
+ KMH = M9t::Distance::CONVERSIONS[:kilometers] * SECONDS_PER_HOUR
12
+ MPH = M9t::Distance::CONVERSIONS[:miles] * SECONDS_PER_HOUR
13
+ KNOTS = KMH / 1.852
14
14
  CONVERSIONS = {
15
- :meters_per_second => 1.0,
16
- :kilometers_per_hour => KMH,
17
- :miles_per_hour => MPH,
18
- :knots => KNOTS
15
+ meters_per_second: 1.0,
16
+ kilometers_per_hour: KMH,
17
+ miles_per_hour: MPH,
18
+ knots: KNOTS
19
19
  }
20
20
 
21
21
  include M9t::Base
22
22
 
23
23
  alias :to_meters_per_second :value
24
-
25
24
  end
26
-
27
25
  end
28
-
@@ -1,17 +1,16 @@
1
- # encoding: utf-8
2
- require 'i18n'
3
- require 'm9t/base'
1
+ require "m9t/base"
4
2
 
5
3
  module M9t
6
-
7
4
  # Represents a temperature
8
5
  # Using degrees (celcius), not kelvin, as default unit
9
6
  class Temperature
10
- DEFAULT_OPTIONS = {:units => :degrees, :abbreviated => false, :precision => 5}
11
- CONVERSIONS = {
12
- :degrees => 1.0,
13
- :kelvin => nil,
14
- :fahrenheit => nil
7
+ DEFAULT_OPTIONS = {
8
+ units: :degrees, abbreviated: false, precision: 5
9
+ }
10
+ CONVERSIONS = {
11
+ degrees: 1.0,
12
+ kelvin: nil,
13
+ fahrenheit: nil
15
14
  }
16
15
 
17
16
  # Conversions
@@ -20,14 +19,13 @@ module M9t
20
19
  include M9t::Base
21
20
 
22
21
  class << self
23
-
24
22
  # Accepts a value in kelvin and returns the equivalent M9t::Temperature
25
- def kelvin( kelvin )
26
- new( kelvin.to_f + ABSOLUTE_ZERO )
23
+ def kelvin(kelvin)
24
+ new(kelvin.to_f + ABSOLUTE_ZERO)
27
25
  end
28
26
 
29
- def fahrenheit( fahrenheit )
30
- new( fahrenheit_to_degrees( fahrenheit ) )
27
+ def fahrenheit(fahrenheit)
28
+ new(fahrenheit_to_degrees(fahrenheit))
31
29
  end
32
30
 
33
31
  # Converts degrees to kelvin
@@ -47,9 +45,8 @@ module M9t
47
45
 
48
46
  # Converts Fahrenheit to degrees
49
47
  def fahrenheit_to_degrees(fahrenheit)
50
- fahrenheit.to_f - 32 * 5.0 / 9.0
48
+ (fahrenheit.to_f - 32) * 5.0 / 9.0
51
49
  end
52
-
53
50
  end
54
51
 
55
52
  alias :to_degrees :value
@@ -62,8 +59,5 @@ module M9t
62
59
  def to_fahrenheit
63
60
  self.class.to_fahrenheit(@value)
64
61
  end
65
-
66
62
  end
67
-
68
63
  end
69
-
data/lib/m9t/version.rb CHANGED
@@ -1,12 +1,9 @@
1
1
  module M9t
2
-
3
2
  module VERSION #:nodoc:
4
- MAJOR = 0
5
- MINOR = 3
6
- TINY = 2
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ TINY = 0
7
6
 
8
- STRING = [MAJOR, MINOR, TINY].join('.')
7
+ STRING = [MAJOR, MINOR, TINY].join(".")
9
8
  end
10
-
11
9
  end
12
-
data/locales/de.yml ADDED
@@ -0,0 +1,68 @@
1
+ # encoding: utf8
2
+ de:
3
+ numbers:
4
+ thousands_separator: '.'
5
+ decimal_separator: ','
6
+ units:
7
+ distance:
8
+ meters:
9
+ full:
10
+ zero: Meter
11
+ one: Meter
12
+ other: Meter
13
+ abbreviated: m
14
+ kilometers:
15
+ full:
16
+ zero: Kilometer
17
+ one: Kilometer
18
+ other: Kilometer
19
+ abbreviated: km
20
+ miles:
21
+ full:
22
+ zero: Meilen
23
+ one: Meile
24
+ other: Meilen
25
+ feet:
26
+ full:
27
+ one: Fuß
28
+ other: Fuß
29
+ abbreviated: ft
30
+ direction:
31
+ degrees:
32
+ full:
33
+ zero: Grad
34
+ one: Grad
35
+ other: Grad
36
+ abbreviated: °
37
+ speed:
38
+ meters_per_second:
39
+ full:
40
+ zero: Meter pro Sekunde
41
+ one: Meter pro Sekunde
42
+ other: Meter pro Sekunde
43
+ abbreviated: m/s
44
+ kilometers_per_hour:
45
+ full:
46
+ one: Kilometer pro Stunde
47
+ other: Kilometer pro Stunde
48
+ abbreviated: km/h
49
+ knots:
50
+ full:
51
+ zero: Knoten
52
+ one: Knoten
53
+ other: Knoten
54
+ temperature:
55
+ degrees:
56
+ full:
57
+ zero: Grad
58
+ one: Grad
59
+ other: Grad
60
+ abbreviated: °C
61
+ kelvin:
62
+ full:
63
+ zero: Kelvin
64
+ one: Kelvin
65
+ other: Kelvin
66
+ abbreviated: K
67
+ direction:
68
+ sectors: ['N', 'NNO', 'NO', 'ONO', 'O', 'OSO', 'SO', 'SSO', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
data/locales/en.yml CHANGED
@@ -22,6 +22,7 @@ en:
22
22
  zero: miles
23
23
  one: mile
24
24
  other: miles
25
+ abbreviated: mi
25
26
  feet:
26
27
  full:
27
28
  one: foot
@@ -35,6 +36,12 @@ en:
35
36
  other: degrees
36
37
  abbreviated: °
37
38
  speed:
39
+ miles_per_hour:
40
+ full:
41
+ zero: miles per hour
42
+ one: mile per hour
43
+ other: miles per hour
44
+ abbreviated: mph
38
45
  meters_per_second:
39
46
  full:
40
47
  zero: meters per second
@@ -52,6 +59,12 @@ en:
52
59
  one: knot
53
60
  other: knots
54
61
  temperature:
62
+ fahrenheit:
63
+ full:
64
+ zero: degrees
65
+ one: degree
66
+ other: degrees
67
+ abbreviated: °F
55
68
  degrees:
56
69
  full:
57
70
  zero: degrees
data/m9t.gemspec CHANGED
@@ -1,28 +1,30 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.unshift( File.dirname(__FILE__) + '/lib' )
3
- require 'm9t/version'
1
+ $LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
2
+ require "m9t/version"
4
3
 
5
4
  gemspec = Gem::Specification.new do |s|
6
- s.name = 'm9t'
5
+ s.name = "m9t"
7
6
  s.platform = Gem::Platform::RUBY
8
7
  s.version = M9t::VERSION::STRING
8
+ s.required_ruby_version = ">= 2.4.0"
9
9
 
10
- s.summary = 'Measurements and conversions library for Ruby'
11
- s.description = 'Classes for handling basic measurement units: distance, direction, speed, temperature and pressure'
10
+ s.summary = "Measurements and conversions library for Ruby"
11
+ s.description = "Classes for handling basic measurement units: distance, direction, speed, temperature and pressure"
12
+ s.license = "MIT"
12
13
 
13
- s.homepage = 'https://github.com/joeyates/m9t'
14
- s.author = 'Joe Yates'
15
- s.email = 'joe.g.yates@gmail.com'
14
+ s.homepage = "https://github.com/joeyates/m9t"
15
+ s.author = "Joe Yates"
16
+ s.email = "joe.g.yates@gmail.com"
16
17
 
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$/ }
19
- s.require_paths = ['lib']
18
+ s.files = `git ls-files`.lines.map(&:chomp!)
19
+ s.test_files = `git ls-files spec`.lines.map(&:chomp!)
20
+ s.require_paths = ["lib"]
20
21
 
21
- s.rubyforge_project = 'nowarning'
22
+ s.rubyforge_project = "nowarning"
22
23
 
23
- s.add_dependency 'rake'
24
- s.add_dependency 'i18n', '>= 0.3.5'
24
+ s.add_dependency "rake"
25
+ s.add_dependency "i18n", ">= 0.3.5"
25
26
 
26
- s.add_development_dependency 'rcov' if RUBY_VERSION < '1.9'
27
- s.add_development_dependency 'simplecov' if RUBY_VERSION > '1.9'
27
+ s.add_development_dependency "codeclimate-test-reporter", "~> 0.4.8"
28
+ s.add_development_dependency "rspec", ">= 3.0.0"
29
+ s.add_development_dependency "simplecov" if RUBY_PLATFORM != "java"
28
30
  end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,79 @@
1
+ require "m9t/base"
2
+
3
+ class SomeMeasurement
4
+ DEFAULT_OPTIONS = {
5
+ units: :foos,
6
+ abbreviated: false,
7
+ decimals: 1
8
+ }
9
+ CONVERSIONS = {
10
+ foos: 1.0,
11
+ bars: 1 / 42.0
12
+ }
13
+
14
+ include M9t::Base
15
+ end
16
+
17
+ class SomeDerivedMeasurement < SomeMeasurement; end
18
+
19
+ describe M9t::Base do
20
+ describe ".respond_to?" do
21
+ it "is true for conversion between known units" do
22
+ expect(SomeMeasurement.respond_to?(:foos_to_bars)).to be_truthy
23
+ end
24
+
25
+ it "is false for unknown units" do
26
+ expect(SomeMeasurement.respond_to?(:bazs_to_bars)).to be_falsey
27
+ end
28
+ end
29
+
30
+ describe ".method_missing" do
31
+ it "handles conversion between known units" do
32
+ expect(SomeMeasurement.foos_to_bars(3.0)).to be_a(Float)
33
+ end
34
+
35
+ it "fails for unknown units" do
36
+ expect {
37
+ SomeMeasurement.bazs_to_bars(3.0)
38
+ }.to raise_error(NoMethodError)
39
+ end
40
+ end
41
+
42
+ describe "#respond_to?" do
43
+ subject { SomeMeasurement.new(3.0) }
44
+
45
+ it "is true for conversion to known units" do
46
+ expect(subject.respond_to?(:to_bars)).to be_truthy
47
+ end
48
+
49
+ it "is false for unknown units" do
50
+ expect(subject.respond_to?(:to_bazs)).to be_falsey
51
+ end
52
+
53
+ context "for unrecognized calls" do
54
+ it "calls super" do
55
+ expect(subject.respond_to?(:ciao)).to be_falsey
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "#method_missing" do
61
+ subject { SomeMeasurement.new(3.0) }
62
+
63
+ it "handles conversion to known units" do
64
+ expect(subject.to_bars).to be_a(Float)
65
+ end
66
+
67
+ it "fails for unknown units" do
68
+ expect {
69
+ subject.to_bazs(3.0)
70
+ }.to raise_error(NoMethodError)
71
+ end
72
+ end
73
+
74
+ describe "derived class" do
75
+ it "inherits options" do
76
+ expect(SomeDerivedMeasurement.options).to eq(SomeMeasurement.options)
77
+ end
78
+ end
79
+ end