ruby-si-units 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/Gemfile.lock +4 -1
- data/README.md +56 -8
- data/lib/si_units/string.rb +13 -0
- data/lib/si_units/unit.rb +21 -3
- data/lib/si_units/version.rb +1 -1
- data/lib/unit.rb +1 -1
- data/ruby-si-unit.gemspec +2 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/support/units_shared_example.rb +17 -0
- data/spec/units/unit_spec.rb +209 -0
- metadata +35 -4
- data/lib/si_units/distance.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f466356845980e98060a4f9570ff9b06f8dad80
|
4
|
+
data.tar.gz: 8a30b37d30f7f7ec6194b5c51539538b82d4eff3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7d9d08987f2e2a58989a4c93049154d7755854a0d7ddd471e973da475dc0fc4b23045a7b28c9fadacfbcf4c8446c9bebddfb32b4640d5acc37a3edb5debd8dc
|
7
|
+
data.tar.gz: f3b4c8c2aecd4a40db6ef7f75a1df804df99ee1326eba977df997e8e6425d47c5b9d9a44a39e0126fd09d5ae5d7d1c30603c7f534b0d1978c3812ed4c91b6c2a
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-si-units (0.0.
|
4
|
+
ruby-si-units (0.0.4)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -14,6 +14,7 @@ GEM
|
|
14
14
|
coderay (~> 1.0.5)
|
15
15
|
method_source (~> 0.8)
|
16
16
|
slop (~> 3.4)
|
17
|
+
rake (10.1.0)
|
17
18
|
rspec (2.14.1)
|
18
19
|
rspec-core (~> 2.14.0)
|
19
20
|
rspec-expectations (~> 2.14.0)
|
@@ -29,6 +30,8 @@ PLATFORMS
|
|
29
30
|
|
30
31
|
DEPENDENCIES
|
31
32
|
awesome_print (~> 1.1.0)
|
33
|
+
bundler (~> 1.3.5)
|
32
34
|
pry (~> 0.9.12.2)
|
35
|
+
rake (~> 10.1.0)
|
33
36
|
rspec (~> 2.14.1)
|
34
37
|
ruby-si-units!
|
data/README.md
CHANGED
@@ -1,14 +1,62 @@
|
|
1
|
-
SI Prefix Units
|
2
|
-
====================
|
1
|
+
# SI Prefix Units
|
3
2
|
A SI prefix unit handling library for ruby
|
4
3
|
|
5
|
-
|
6
|
-
|
4
|
+
[](https://travis-ci.org/gnomex/ruby-si-units)
|
5
|
+
|
6
|
+
## How it work's ?
|
7
7
|
<p>
|
8
|
-
|
8
|
+
Makes a parce of an absolute value and identifies the scale of the unit that belongs SI, returning the unit in better representation along with the unit symbol prefixed
|
9
9
|
</p>
|
10
10
|
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add in your application's Gemfile:
|
14
|
+
|
15
|
+
`gem "ruby-si-units", "~> 0.0.3"`
|
16
|
+
|
17
|
+
And using blunder, execute:
|
18
|
+
|
19
|
+
`$ bundle install`
|
20
|
+
|
21
|
+
Or install it yourself:
|
22
|
+
|
23
|
+
`$ gem install ruby-si-units`
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Convert absolute value to a unit with prefix SI notation
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
unit = SIUnits::Unit.new(10000000)
|
32
|
+
# => #<SIUnits::Unit:0x0000000283b8a8 @unit_value=10000000, @unit_kind="mega">
|
33
|
+
unit.best_scale
|
34
|
+
# => "10.0M"
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
unit = SIUnits::Unit.new(0.000000001)
|
39
|
+
# => #<SIUnits::Unit:0x0000000272bb20 @unit_value=1.0e-09, @unit_kind="nano">
|
40
|
+
unit.best_scale
|
41
|
+
# => "1.0n"
|
42
|
+
```
|
43
|
+
### Convert string to unit
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
"10.0k".to_unit
|
47
|
+
# => #<SIUnits::Unit:0x0000000175da90 @unit_value=10000.0, @unit_kind="kilo">
|
48
|
+
|
49
|
+
"1n".to_unit
|
50
|
+
# => #<SIUnits::Unit:0x000000017406e8 @unit_value=1.0e-09, @unit_kind="nano">
|
51
|
+
```
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
11
60
|
|
12
|
-
Another useful resources
|
13
|
-
|
14
|
-
* (ruby-units)[https://github.com/olbrich/ruby-units]
|
61
|
+
## Another useful resources
|
62
|
+
* [ruby-units](https://github.com/olbrich/ruby-units)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class String
|
2
|
+
# make a string into a unit
|
3
|
+
# @return (see RubyUnits::Unit#initialize)
|
4
|
+
def to_unit
|
5
|
+
unit_reduced, prefix = *split_value(self)
|
6
|
+
SIUnits::Unit.new(unit_reduced.to_f).convert_to(prefix)
|
7
|
+
end
|
8
|
+
|
9
|
+
def split_value(value)
|
10
|
+
value.scan(/(\d+\.?\d*)(\w+)/).flatten
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
data/lib/si_units/unit.rb
CHANGED
@@ -24,7 +24,7 @@ module SIUnits
|
|
24
24
|
'kilo' => [%w{k kilo}, 1e3],
|
25
25
|
'hecto' => [%w{h Hecto hecto}, 1e2],
|
26
26
|
'deca' => [%w{da Deca deca deka}, 1e1],
|
27
|
-
'1' => [%w{const},
|
27
|
+
'1' => [%w{const}, 1],
|
28
28
|
'deci' => [%w{d Deci deci}, 1e-1],
|
29
29
|
'centi' => [%w{c Centi centi}, 1e-2],
|
30
30
|
'milli' => [%w{m Milli milli}, 1e-3],
|
@@ -50,6 +50,15 @@ module SIUnits
|
|
50
50
|
UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
|
51
51
|
end
|
52
52
|
|
53
|
+
# convert to a specified unit string or to the same units as another Unit
|
54
|
+
def convert_to(prefix)
|
55
|
+
return self if prefix.nil?
|
56
|
+
scalar = prefix_is_defined?(prefix)
|
57
|
+
absolute_unit_value = convert_base_prefix_to_value self.unit_value, scalar
|
58
|
+
|
59
|
+
SIUnits::Unit.new(absolute_unit_value)
|
60
|
+
end
|
61
|
+
|
53
62
|
private
|
54
63
|
|
55
64
|
def best_value_with_scale
|
@@ -57,6 +66,10 @@ module SIUnits
|
|
57
66
|
[(@unit_value / scalar), aliase.first].join
|
58
67
|
end
|
59
68
|
|
69
|
+
def convert_base_prefix_to_value(value, scalar)
|
70
|
+
value * scalar
|
71
|
+
end
|
72
|
+
|
60
73
|
def parse_unit
|
61
74
|
case @unit_value
|
62
75
|
when 1e-15..1e-12 then return "pico"
|
@@ -65,8 +78,7 @@ module SIUnits
|
|
65
78
|
when 1e-6..1e-3 then return "milli"
|
66
79
|
when 1e-3..1e-2 then return "centi"
|
67
80
|
when 1e-2..1e-1 then return "deci"
|
68
|
-
when 1e-1..
|
69
|
-
when 0e1..1e1 then return "1"
|
81
|
+
when 1e-1..1e1 then return "1"
|
70
82
|
when 1e1..1e2 then return "deca"
|
71
83
|
when 1e2..1e3 then return "hecto"
|
72
84
|
when 1e3..1e6 then return "kilo"
|
@@ -76,5 +88,11 @@ module SIUnits
|
|
76
88
|
end
|
77
89
|
end
|
78
90
|
|
91
|
+
def prefix_is_defined?(kind)
|
92
|
+
UNITS_DEFINITION.each { |key, value|
|
93
|
+
return value.last if value[0].include?(kind)
|
94
|
+
}
|
95
|
+
raise ArgumentError, "Unknown prefix"
|
96
|
+
end
|
79
97
|
end
|
80
98
|
end
|
data/lib/si_units/version.rb
CHANGED
data/lib/unit.rb
CHANGED
data/ruby-si-unit.gemspec
CHANGED
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "https://github.com/gnomex/ruby-si-units"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
+
spec.add_development_dependency("bundler", "~> 1.3.5")
|
17
|
+
spec.add_development_dependency("rake", "~> 10.1.0")
|
16
18
|
spec.add_development_dependency("awesome_print", "~> 1.1.0")
|
17
19
|
spec.add_development_dependency("pry", "~> 0.9.12.2")
|
18
20
|
spec.add_development_dependency("rspec", "~> 2.14.1")
|
data/spec/spec_helper.rb
CHANGED
@@ -4,6 +4,9 @@
|
|
4
4
|
# loaded once.
|
5
5
|
#
|
6
6
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
|
9
|
+
|
7
10
|
RSpec.configure do |config|
|
8
11
|
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
12
|
config.run_all_when_everything_filtered = true
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
shared_examples_for "is unit of" do |unit, expected_prefix|
|
5
|
+
|
6
|
+
it "is max limit on range" do
|
7
|
+
expect(:unit.to_s).to eql(expected_prefix)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is min limit on range" do
|
11
|
+
expect(:unit.to_s).to eql(expected_prefix)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "out of unit range" do
|
15
|
+
expect(:unit.to_s).to_not eql(expected_prefix)
|
16
|
+
end
|
17
|
+
end
|
data/spec/units/unit_spec.rb
CHANGED
@@ -1,2 +1,211 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require 'spec_helper'
|
3
|
+
require "unit"
|
4
|
+
|
5
|
+
describe "it's a pico " do
|
6
|
+
it "max limit" do
|
7
|
+
unit = SIUnits::Unit.new((1e-15) + 0.0000000000001)
|
8
|
+
expect(unit.best_scale).to match(/[p]/)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "min limit" do
|
12
|
+
unit = SIUnits::Unit.new(1e-12)
|
13
|
+
expect(unit.best_scale).to match(/[p]/)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "not a pico" do
|
17
|
+
unit = SIUnits::Unit.new((1e-12) + 0.0000000000001)
|
18
|
+
expect(unit.best_scale).to_not match(/[p]/)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "it's a nano " do
|
23
|
+
it "max limit" do
|
24
|
+
unit = SIUnits::Unit.new((1e-12) + 0.00000000001)
|
25
|
+
expect(unit.best_scale).to match(/[n]/)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "min limit" do
|
29
|
+
unit = SIUnits::Unit.new(1e-9)
|
30
|
+
expect(unit.best_scale).to match(/[n]/)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "not a nano" do
|
34
|
+
unit = SIUnits::Unit.new((1e-9) + 0.00000000001)
|
35
|
+
expect(unit.best_scale).to_not match(/[n]/)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "it's a micro " do
|
40
|
+
it "max limit" do
|
41
|
+
unit = SIUnits::Unit.new((1e-9) + 0.00000000001)
|
42
|
+
expect(unit.best_scale).to match(/[u]/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "min limit" do
|
46
|
+
unit = SIUnits::Unit.new(1e-6)
|
47
|
+
expect(unit.best_scale).to match(/[u]/)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "not a micro" do
|
51
|
+
unit = SIUnits::Unit.new((1e-6) + 0.000001)
|
52
|
+
expect(unit.best_scale).to_not match(/[u]/)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "it's a milli " do
|
57
|
+
it "max limit" do
|
58
|
+
unit = SIUnits::Unit.new((1e-6) + 0.000001)
|
59
|
+
expect(unit.best_scale).to match(/[m]/)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "min limit" do
|
63
|
+
unit = SIUnits::Unit.new(1e-3)
|
64
|
+
expect(unit.best_scale).to match(/[m]/)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "not a milli" do
|
68
|
+
unit = SIUnits::Unit.new((1e-3) + 0.001)
|
69
|
+
expect(unit.best_scale).to_not match(/[m]/)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "it's a centi " do
|
74
|
+
it "max limit" do
|
75
|
+
unit = SIUnits::Unit.new((1e-3) + 0.001)
|
76
|
+
expect(unit.best_scale).to match(/[c]/)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "min limit" do
|
80
|
+
unit = SIUnits::Unit.new(1e-2)
|
81
|
+
expect(unit.best_scale).to match(/[c]/)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "not a centi" do
|
85
|
+
unit = SIUnits::Unit.new((1e-2) + 0.001)
|
86
|
+
expect(unit.best_scale).to_not match(/[c]/)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "it's a deci " do
|
91
|
+
it "max limit" do
|
92
|
+
unit = SIUnits::Unit.new((1e-2) + 0.01)
|
93
|
+
expect(unit.best_scale).to match(/[d]/)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "min limit" do
|
97
|
+
unit = SIUnits::Unit.new(1e-1)
|
98
|
+
expect(unit.best_scale).to match(/[d]/)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "not a deci" do
|
102
|
+
unit = SIUnits::Unit.new((1e-1) + 0.01)
|
103
|
+
expect(unit.best_scale).to_not match(/[d]/)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "it's a const " do
|
108
|
+
xit "max limit" do
|
109
|
+
unit = SIUnits::Unit.new((1e-1) + 0.1)
|
110
|
+
expect(unit.best_scale).to match(/const/)
|
111
|
+
end
|
112
|
+
|
113
|
+
xit "zero" do
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
xit "min limit" do
|
118
|
+
unit = SIUnits::Unit.new(1e1)
|
119
|
+
expect(unit.best_scale).to match(/cost/)
|
120
|
+
end
|
121
|
+
|
122
|
+
xit "not a const" do
|
123
|
+
unit = SIUnits::Unit.new((1e-2) + 0.00000000001)
|
124
|
+
expect(unit.best_scale).to_not match(/const/)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "it's a deca " do
|
129
|
+
it "max limit" do
|
130
|
+
unit = SIUnits::Unit.new((1e2) - 1.0)
|
131
|
+
expect(unit.best_scale).to match(/(da)/)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "min limit" do
|
135
|
+
unit = SIUnits::Unit.new(1e1 + 1.0)
|
136
|
+
expect(unit.best_scale).to match(/(da)/)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "not a deca" do
|
140
|
+
unit = SIUnits::Unit.new((1e1) - 1.0)
|
141
|
+
expect(unit.best_scale).to_not match(/da/)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "it's a hecto " do
|
146
|
+
it "max limit" do
|
147
|
+
unit = SIUnits::Unit.new((1e3) - 1.0)
|
148
|
+
expect(unit.best_scale).to match(/[h]/)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "min limit" do
|
152
|
+
unit = SIUnits::Unit.new(1e2 + 1.0)
|
153
|
+
expect(unit.best_scale).to match(/[h]/)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "not a deca" do
|
157
|
+
unit = SIUnits::Unit.new((1e2) - 1.0)
|
158
|
+
expect(unit.best_scale).to_not match(/[h]/)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "it's a kilo " do
|
163
|
+
it "max limit" do
|
164
|
+
unit = SIUnits::Unit.new((1e6) - 1.0)
|
165
|
+
expect(unit.best_scale).to match(/[k]/)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "min limit" do
|
169
|
+
unit = SIUnits::Unit.new(1e3 + 1.0)
|
170
|
+
expect(unit.best_scale).to match(/[k]/)
|
171
|
+
end
|
172
|
+
|
173
|
+
it "not a deca" do
|
174
|
+
unit = SIUnits::Unit.new((1e3) - 1.0)
|
175
|
+
expect(unit.best_scale).to_not match(/[k]/)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "it's a mega " do
|
180
|
+
it "max limit" do
|
181
|
+
unit = SIUnits::Unit.new((1e9) - 1.0)
|
182
|
+
expect(unit.best_scale).to match(/[M]/)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "min limit" do
|
186
|
+
unit = SIUnits::Unit.new(1e6 + 1.0)
|
187
|
+
expect(unit.best_scale).to match(/[M]/)
|
188
|
+
end
|
189
|
+
|
190
|
+
it "not a deca" do
|
191
|
+
unit = SIUnits::Unit.new((1e6) - 1.0)
|
192
|
+
expect(unit.best_scale).to_not match(/[M]/)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "it's a giga " do
|
197
|
+
it "max limit" do
|
198
|
+
unit = SIUnits::Unit.new((1e12) - 1.0)
|
199
|
+
expect(unit.best_scale).to match(/[G]/)
|
200
|
+
end
|
201
|
+
|
202
|
+
it "min limit" do
|
203
|
+
unit = SIUnits::Unit.new(1e9 + 1.0)
|
204
|
+
expect(unit.best_scale).to match(/[G]/)
|
205
|
+
end
|
206
|
+
|
207
|
+
it "not a deca" do
|
208
|
+
unit = SIUnits::Unit.new((1e9) - 1.0)
|
209
|
+
expect(unit.best_scale).to_not match(/[G]/)
|
210
|
+
end
|
211
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-si-units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenner Kliemann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.5
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 10.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.1.0
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: awesome_print
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -61,16 +89,18 @@ extra_rdoc_files: []
|
|
61
89
|
files:
|
62
90
|
- .gitignore
|
63
91
|
- .rspec
|
92
|
+
- .travis.yml
|
64
93
|
- Gemfile
|
65
94
|
- Gemfile.lock
|
66
95
|
- LICENSE
|
67
96
|
- README.md
|
68
|
-
- lib/si_units/
|
97
|
+
- lib/si_units/string.rb
|
69
98
|
- lib/si_units/unit.rb
|
70
99
|
- lib/si_units/version.rb
|
71
100
|
- lib/unit.rb
|
72
101
|
- ruby-si-unit.gemspec
|
73
102
|
- spec/spec_helper.rb
|
103
|
+
- spec/support/units_shared_example.rb
|
74
104
|
- spec/units/unit_spec.rb
|
75
105
|
homepage: https://github.com/gnomex/ruby-si-units
|
76
106
|
licenses:
|
@@ -92,10 +122,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
122
|
version: '0'
|
93
123
|
requirements: []
|
94
124
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.0.
|
125
|
+
rubygems_version: 2.0.7
|
96
126
|
signing_key:
|
97
127
|
specification_version: 4
|
98
128
|
summary: A SI prefix unit handling library for ruby
|
99
129
|
test_files:
|
100
130
|
- spec/spec_helper.rb
|
131
|
+
- spec/support/units_shared_example.rb
|
101
132
|
- spec/units/unit_spec.rb
|
data/lib/si_units/distance.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
module SIUnits
|
2
|
-
class Distance
|
3
|
-
include Comparable
|
4
|
-
|
5
|
-
UNIT_REGEX = /(\d+)(\w+)/
|
6
|
-
|
7
|
-
attr_reader :value, :unit, :distance
|
8
|
-
|
9
|
-
def initialize(distance)
|
10
|
-
@distance = distance
|
11
|
-
@value, @unit = *split_value(distance)
|
12
|
-
end
|
13
|
-
|
14
|
-
def <=>(comparison)
|
15
|
-
u1 = Unit.new(@unit)
|
16
|
-
u2 = Unit.new(comparison.unit)
|
17
|
-
|
18
|
-
if u1 > u2
|
19
|
-
return 1
|
20
|
-
elsif u1 < u2
|
21
|
-
return -1
|
22
|
-
elsif u1 == u2
|
23
|
-
@value.to_i <=> comparison.value.to_i
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def split_value(value)
|
30
|
-
value.scan(UNIT_REGEX).flatten
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|