m9t 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +1 -0
- data/README.md +5 -21
- data/lib/m9t.rb +3 -2
- data/lib/m9t/base.rb +31 -34
- data/lib/m9t/i18n.rb +5 -0
- data/lib/m9t/version.rb +1 -1
- data/m9t.gemspec +3 -0
- data/spec/{unit/base_spec.rb → base_spec.rb} +1 -0
- data/spec/{unit/direction_spec.rb → direction_spec.rb} +1 -0
- data/spec/{unit/distance_spec.rb → distance_spec.rb} +1 -0
- data/spec/{unit/i18n_spec.rb → i18n_spec.rb} +1 -0
- data/spec/{unit/pressure_spec.rb → pressure_spec.rb} +1 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/{unit/speed_spec.rb → speed_spec.rb} +1 -0
- data/spec/{unit/temperature_spec.rb → temperature_spec.rb} +1 -0
- metadata +51 -52
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a5032bc2dee8145116056d99cc84fbaf492c8896
|
4
|
+
data.tar.gz: f361209c27b67471b1eb410651758141e4bf241e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 17d1116cc4b15a2b1cf7fb7191dbb01ac306f8d0748b6d638ee276263134c5e4d627d9e5e0a5f66e31732821f48c2c66d6122d4d5a23d80f46406ad13caf1b75
|
7
|
+
data.tar.gz: 6083ab4e3dc37724bf9984e62b0e3a50799e817edf000843e3dd679987e0d14314b97c718557e6043b4029c7323dbe7389a55d3bc8e463abd82e59c2db85c89e
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
[![Build Status](https://secure.travis-ci.org/joeyates/m9t.png)][Continuous Integration]
|
2
|
+
[![Source Analysis](https://codeclimate.com/github/joeyates/m9t/badges/gpa.svg)](https://codeclimate.com/github/joeyates/m9t)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/joeyates/m9t/badges/coverage.svg)](https://codeclimate.com/github/joeyates/m9t/coverage)
|
4
|
+
|
5
|
+
# m9t
|
3
6
|
|
4
7
|
*Measurements and coversions library for Ruby*
|
5
8
|
|
@@ -78,24 +81,6 @@ Methods are available for conversion between any pair of units:
|
|
78
81
|
M9t::Distance.miles_to_meters(26.21875) -> 42194.988
|
79
82
|
```
|
80
83
|
|
81
|
-
Testing
|
82
|
-
=======
|
83
|
-
|
84
|
-
Coverage
|
85
|
-
--------
|
86
|
-
|
87
|
-
ruby 1.8.x (not jruby):
|
88
|
-
|
89
|
-
```shell
|
90
|
-
$ rake rcov
|
91
|
-
```
|
92
|
-
|
93
|
-
ruby 1.9.x:
|
94
|
-
|
95
|
-
```shell
|
96
|
-
$ COVERAGE=1 rake test
|
97
|
-
```
|
98
|
-
|
99
84
|
Alternatives
|
100
85
|
============
|
101
86
|
|
@@ -118,4 +103,3 @@ Dual license:
|
|
118
103
|
|
119
104
|
- MIT License: see MIT-LICENSE.txt,
|
120
105
|
- GPL version 3: see GPL-LICENSE.txt
|
121
|
-
|
data/lib/m9t.rb
CHANGED
data/lib/m9t/base.rb
CHANGED
@@ -5,7 +5,7 @@ require 'm9t/i18n'
|
|
5
5
|
module M9t
|
6
6
|
module Base
|
7
7
|
def self.generate_conversions(klass)
|
8
|
-
klass.instance_eval do
|
8
|
+
klass.instance_eval do
|
9
9
|
def convert(from, to, value)
|
10
10
|
value / self::CONVERSIONS[from] * self::CONVERSIONS[to]
|
11
11
|
end
|
@@ -26,26 +26,21 @@ module M9t
|
|
26
26
|
super
|
27
27
|
end
|
28
28
|
|
29
|
-
def respond_to?(name,
|
29
|
+
def respond_to?(name, _include_all = false)
|
30
30
|
from, to = extract_from_and_to(name)
|
31
|
-
if from
|
32
|
-
|
33
|
-
end
|
34
|
-
return legal_constructor?(name)
|
31
|
+
return true if from && legal_conversion?(from, to)
|
32
|
+
legal_constructor?(name)
|
35
33
|
end
|
36
34
|
|
37
35
|
private
|
38
36
|
|
39
37
|
def extract_from_and_to(name)
|
40
|
-
|
41
|
-
[$1, $2]
|
42
|
-
else
|
43
|
-
nil
|
44
|
-
end
|
38
|
+
name.to_s.scan(/^(\w+)_to_(\w+)$/)[0]
|
45
39
|
end
|
46
40
|
|
47
41
|
def legal_conversion?(from, to)
|
48
|
-
self::CONVERSIONS.include?(from.to_sym)
|
42
|
+
self::CONVERSIONS.include?(from.to_sym) &&
|
43
|
+
self::CONVERSIONS.include?(to.to_sym)
|
49
44
|
end
|
50
45
|
|
51
46
|
def define_conversion(from, to)
|
@@ -64,7 +59,7 @@ module M9t
|
|
64
59
|
# from the unit and returns an instance
|
65
60
|
def define_constructor(name)
|
66
61
|
self.class.instance_exec do
|
67
|
-
define_method(name.to_sym) do
|
62
|
+
define_method(name.to_sym) do |*args|
|
68
63
|
new(args[0].to_f / self::CONVERSIONS[name])
|
69
64
|
end
|
70
65
|
end
|
@@ -81,14 +76,15 @@ module M9t
|
|
81
76
|
end
|
82
77
|
end
|
83
78
|
|
84
|
-
# Returns the
|
79
|
+
# Returns the class's current options - see the specific class for
|
80
|
+
# defaults
|
85
81
|
def options
|
86
82
|
@options
|
87
83
|
end
|
88
84
|
|
89
|
-
# Reloads the class' default options
|
85
|
+
# Reloads the class's default options
|
90
86
|
def reset_options!
|
91
|
-
@options = self::DEFAULT_OPTIONS.clone
|
87
|
+
@options = self::DEFAULT_OPTIONS.clone
|
92
88
|
end
|
93
89
|
|
94
90
|
# The name used for i18n translations
|
@@ -112,7 +108,7 @@ module M9t
|
|
112
108
|
end
|
113
109
|
|
114
110
|
attr_reader :value, :options
|
115
|
-
|
111
|
+
alias_method :to_f, :value
|
116
112
|
|
117
113
|
def initialize(value)
|
118
114
|
@value = value.to_f
|
@@ -121,16 +117,16 @@ module M9t
|
|
121
117
|
# define conversion instance methods as required
|
122
118
|
def method_missing(name, *args, &block)
|
123
119
|
to = extract_to(name)
|
124
|
-
if to
|
120
|
+
if to && legal_conversion?(to)
|
125
121
|
define_conversion(to)
|
126
122
|
return send(name)
|
127
123
|
end
|
128
124
|
super
|
129
125
|
end
|
130
126
|
|
131
|
-
def respond_to?(name,
|
127
|
+
def respond_to?(name, _include_all = false)
|
132
128
|
to = extract_to(name)
|
133
|
-
return true if to
|
129
|
+
return true if to && legal_conversion?(to)
|
134
130
|
super
|
135
131
|
end
|
136
132
|
|
@@ -138,34 +134,35 @@ module M9t
|
|
138
134
|
# taking into account locale, desired units and abbreviation.
|
139
135
|
def to_s(options = {})
|
140
136
|
options = self.class.options.merge(options)
|
141
|
-
|
137
|
+
unless self.class::CONVERSIONS.include?(options[:units])
|
142
138
|
units_error(options[:units])
|
143
139
|
end
|
144
|
-
value_in_units =
|
140
|
+
value_in_units = send("to_#{options[:units]}")
|
145
141
|
localized_value = I18n.localize_float(
|
146
|
-
value_in_units,
|
142
|
+
value_in_units, format: "%0.#{options[:precision]}f"
|
147
143
|
)
|
148
144
|
|
149
|
-
key =
|
150
|
-
|
151
|
-
unit = I18n.t(key, {count: value_in_units})
|
145
|
+
key = i18n_key(options)
|
146
|
+
unit = I18n.t(key, count: value_in_units)
|
152
147
|
|
153
148
|
"#{localized_value}%s#{unit}" % (options[:abbreviated] ? '' : ' ')
|
154
149
|
end
|
155
150
|
|
156
151
|
private
|
157
152
|
|
153
|
+
def i18n_key(options = {})
|
154
|
+
key = 'units.' + self.class.measurement_name + '.' + options[:units].to_s
|
155
|
+
options[:abbreviated] ? key += '.abbreviated' : key += '.full'
|
156
|
+
key
|
157
|
+
end
|
158
|
+
|
158
159
|
def units_error(units)
|
159
|
-
known = self.class::CONVERSIONS.keys.collect
|
160
|
-
|
160
|
+
known = self.class::CONVERSIONS.keys.collect(&:to_s).join(', ')
|
161
|
+
fail M9t::UnitError, "Unknown units '#{units}'. Known: #{known}"
|
161
162
|
end
|
162
163
|
|
163
164
|
def extract_to(name)
|
164
|
-
|
165
|
-
$1
|
166
|
-
else
|
167
|
-
nil
|
168
|
-
end
|
165
|
+
name.to_s[/^to_(\w+)$/, 1]
|
169
166
|
end
|
170
167
|
|
171
168
|
def legal_conversion?(to)
|
@@ -175,7 +172,7 @@ module M9t
|
|
175
172
|
def define_conversion(to)
|
176
173
|
self.class.instance_exec do
|
177
174
|
define_method("to_#{to}") do
|
178
|
-
self.class.convert(self.class.default_unit, to.to_sym,
|
175
|
+
self.class.convert(self.class.default_unit, to.to_sym, to_f)
|
179
176
|
end
|
180
177
|
end
|
181
178
|
end
|
data/lib/m9t/i18n.rb
CHANGED
@@ -8,6 +8,11 @@ I18n.load_path += Dir.glob("#{ locales_path }/*.yml")
|
|
8
8
|
I18n.reload!
|
9
9
|
|
10
10
|
# Monkey patch I18n
|
11
|
+
# i18n does not handle localizing numbers.
|
12
|
+
# See the following GitHub issues:
|
13
|
+
# * https://github.com/svenfuchs/i18n/issues/328
|
14
|
+
# * https://github.com/svenfuchs/i18n/issues/135
|
15
|
+
# * https://github.com/svenfuchs/i18n/issues/183
|
11
16
|
module I18n
|
12
17
|
# Handle non-English numerical separators
|
13
18
|
# with I18n.locale = :it,
|
data/lib/m9t/version.rb
CHANGED
data/m9t.gemspec
CHANGED
@@ -6,9 +6,11 @@ gemspec = Gem::Specification.new do |s|
|
|
6
6
|
s.name = 'm9t'
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.version = M9t::VERSION::STRING
|
9
|
+
s.required_ruby_version = '>= 1.9.3'
|
9
10
|
|
10
11
|
s.summary = 'Measurements and conversions library for Ruby'
|
11
12
|
s.description = 'Classes for handling basic measurement units: distance, direction, speed, temperature and pressure'
|
13
|
+
s.license = "MIT"
|
12
14
|
|
13
15
|
s.homepage = 'https://github.com/joeyates/m9t'
|
14
16
|
s.author = 'Joe Yates'
|
@@ -23,6 +25,7 @@ gemspec = Gem::Specification.new do |s|
|
|
23
25
|
s.add_dependency 'rake'
|
24
26
|
s.add_dependency 'i18n', '>= 0.3.5'
|
25
27
|
|
28
|
+
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.8'
|
26
29
|
s.add_development_dependency 'rspec', '>= 3.0.0'
|
27
30
|
s.add_development_dependency 'simplecov' if RUBY_PLATFORM != 'java'
|
28
31
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,90 +1,95 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: m9t
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.6
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Joe Yates
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: i18n
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 0.3.5
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 0.3.5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: codeclimate-test-reporter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.4.8
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.4.8
|
46
55
|
- !ruby/object:Gem::Dependency
|
47
56
|
name: rspec
|
48
57
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
58
|
requirements:
|
51
|
-
- -
|
59
|
+
- - ">="
|
52
60
|
- !ruby/object:Gem::Version
|
53
61
|
version: 3.0.0
|
54
62
|
type: :development
|
55
63
|
prerelease: false
|
56
64
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
65
|
requirements:
|
59
|
-
- -
|
66
|
+
- - ">="
|
60
67
|
- !ruby/object:Gem::Version
|
61
68
|
version: 3.0.0
|
62
69
|
- !ruby/object:Gem::Dependency
|
63
70
|
name: simplecov
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
|
-
- -
|
73
|
+
- - ">="
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: '0'
|
70
76
|
type: :development
|
71
77
|
prerelease: false
|
72
78
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
79
|
requirements:
|
75
|
-
- -
|
80
|
+
- - ">="
|
76
81
|
- !ruby/object:Gem::Version
|
77
82
|
version: '0'
|
78
|
-
description:
|
79
|
-
|
83
|
+
description: 'Classes for handling basic measurement units: distance, direction, speed,
|
84
|
+
temperature and pressure'
|
80
85
|
email: joe.g.yates@gmail.com
|
81
86
|
executables: []
|
82
87
|
extensions: []
|
83
88
|
extra_rdoc_files: []
|
84
89
|
files:
|
85
|
-
- .gitignore
|
86
|
-
- .rspec
|
87
|
-
- .travis.yml
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
88
93
|
- GPL-LICENSE.txt
|
89
94
|
- Gemfile
|
90
95
|
- MIT-LICENSE.txt
|
@@ -104,50 +109,44 @@ files:
|
|
104
109
|
- locales/en.yml
|
105
110
|
- locales/it.yml
|
106
111
|
- m9t.gemspec
|
112
|
+
- spec/base_spec.rb
|
113
|
+
- spec/direction_spec.rb
|
114
|
+
- spec/distance_spec.rb
|
115
|
+
- spec/i18n_spec.rb
|
116
|
+
- spec/pressure_spec.rb
|
107
117
|
- spec/spec_helper.rb
|
108
|
-
- spec/
|
109
|
-
- spec/
|
110
|
-
- spec/unit/distance_spec.rb
|
111
|
-
- spec/unit/i18n_spec.rb
|
112
|
-
- spec/unit/pressure_spec.rb
|
113
|
-
- spec/unit/speed_spec.rb
|
114
|
-
- spec/unit/temperature_spec.rb
|
118
|
+
- spec/speed_spec.rb
|
119
|
+
- spec/temperature_spec.rb
|
115
120
|
homepage: https://github.com/joeyates/m9t
|
116
|
-
licenses:
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
117
124
|
post_install_message:
|
118
125
|
rdoc_options: []
|
119
126
|
require_paths:
|
120
127
|
- lib
|
121
128
|
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
129
|
requirements:
|
124
|
-
- -
|
130
|
+
- - ">="
|
125
131
|
- !ruby/object:Gem::Version
|
126
|
-
version:
|
127
|
-
segments:
|
128
|
-
- 0
|
129
|
-
hash: -4097874954869017235
|
132
|
+
version: 1.9.3
|
130
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
-
none: false
|
132
134
|
requirements:
|
133
|
-
- -
|
135
|
+
- - ">="
|
134
136
|
- !ruby/object:Gem::Version
|
135
137
|
version: '0'
|
136
|
-
segments:
|
137
|
-
- 0
|
138
|
-
hash: -4097874954869017235
|
139
138
|
requirements: []
|
140
139
|
rubyforge_project: nowarning
|
141
|
-
rubygems_version:
|
140
|
+
rubygems_version: 2.5.1
|
142
141
|
signing_key:
|
143
|
-
specification_version:
|
142
|
+
specification_version: 4
|
144
143
|
summary: Measurements and conversions library for Ruby
|
145
144
|
test_files:
|
145
|
+
- spec/base_spec.rb
|
146
|
+
- spec/direction_spec.rb
|
147
|
+
- spec/distance_spec.rb
|
148
|
+
- spec/i18n_spec.rb
|
149
|
+
- spec/pressure_spec.rb
|
146
150
|
- spec/spec_helper.rb
|
147
|
-
- spec/
|
148
|
-
- spec/
|
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
|
151
|
+
- spec/speed_spec.rb
|
152
|
+
- spec/temperature_spec.rb
|