ruby-si-units 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +22 -2
- data/lib/si_units/string.rb +1 -1
- data/lib/si_units/unit.rb +66 -35
- data/lib/si_units/version.rb +1 -1
- data/spec/units/unit_spec.rb +15 -168
- metadata +3 -5
- data/spec/units/string_spec.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efa72132c423b8eeebd9edfb7be550c997713b49
|
4
|
+
data.tar.gz: 7efa33641c700b3a2af0bc5d2665e30617e19274
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af000424f61537a2c24d6bb758ba2f7de023b4e59aa40ff16e95b4b642c377f9de52c94db2271a669e2235ad2e86eb3b929a44724313bfcef9d945f04d7d7ea6
|
7
|
+
data.tar.gz: e655905b2637defa6103898bbcc7c6773bc6ef09fd400274c03001559105c43e91674c1eb813cf6ca844f1c2ad6424ace13e82ea7013bd4a16983ade4cdabbaf
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format
|
2
|
+
--format documentation
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -28,14 +28,14 @@ Or install it yourself:
|
|
28
28
|
### Convert absolute value to a unit with prefix SI notation
|
29
29
|
|
30
30
|
```ruby
|
31
|
-
unit = SIUnits::Unit.new
|
31
|
+
unit = SIUnits::Unit.new 10000000
|
32
32
|
# => #<SIUnits::Unit:0x0000000283b8a8 @unit_value=10000000, @unit_kind="mega">
|
33
33
|
unit.best_scale
|
34
34
|
# => "10.0M"
|
35
35
|
```
|
36
36
|
|
37
37
|
```ruby
|
38
|
-
unit = SIUnits::Unit.new
|
38
|
+
unit = SIUnits::Unit.new 0.000000001
|
39
39
|
# => #<SIUnits::Unit:0x0000000272bb20 @unit_value=1.0e-09, @unit_kind="nano">
|
40
40
|
unit.best_scale
|
41
41
|
# => "1.0n"
|
@@ -50,6 +50,26 @@ unit.best_scale
|
|
50
50
|
# => #<SIUnits::Unit:0x000000017406e8 @unit_value=1.0e-09, @unit_kind="nano">
|
51
51
|
```
|
52
52
|
|
53
|
+
### Compare units
|
54
|
+
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
a = SIUnits::Unit.new 10000000
|
58
|
+
# => #<SIUnits::Unit:0x0000000283b8a8 @unit_value=10000000, @unit_kind="mega">
|
59
|
+
|
60
|
+
b = SIUnits::Unit.new 0.000000001
|
61
|
+
# => #<SIUnits::Unit:0x0000000272bb20 @unit_value=1.0e-09, @unit_kind="nano">
|
62
|
+
|
63
|
+
a > b
|
64
|
+
=> true
|
65
|
+
a < b
|
66
|
+
=> false
|
67
|
+
a <=> b
|
68
|
+
=> 1
|
69
|
+
a == b
|
70
|
+
=> false
|
71
|
+
```
|
72
|
+
|
53
73
|
## Contributing
|
54
74
|
|
55
75
|
1. Fork it
|
data/lib/si_units/string.rb
CHANGED
data/lib/si_units/unit.rb
CHANGED
@@ -7,7 +7,8 @@
|
|
7
7
|
module SIUnits
|
8
8
|
class Unit
|
9
9
|
include Comparable
|
10
|
-
|
10
|
+
|
11
|
+
attr_reader :unit_value, :unit_kind
|
11
12
|
|
12
13
|
# Definition of SI Prefix Units, with name, aliases and scale
|
13
14
|
UNITS_DEFINITION = {
|
@@ -43,52 +44,77 @@ module SIUnits
|
|
43
44
|
'yocto' => [%w{y Yocto yocto}, 1e-24],
|
44
45
|
'zero' => [%w{zero}, 0.0]
|
45
46
|
}
|
47
|
+
UNIT_REGEX = /(\d+\.?\d*)(\w+)/
|
48
|
+
|
49
|
+
# Create a new Unit object.
|
50
|
+
# @return [Unit]
|
51
|
+
# @raise [ArgumentError] if absolute value of a temperature is less than absolute zero
|
52
|
+
# @raise [ArgumentError] if no unit is specified
|
53
|
+
# @raise [ArgumentError] if an invalid unit is specified
|
54
|
+
def initialize(*options)
|
55
|
+
|
56
|
+
raise ArgumentError, "Unit can't be initialized without args" if options[0].nil?
|
57
|
+
|
58
|
+
case options[0]
|
59
|
+
when Numeric
|
60
|
+
# Conversion use a scale
|
61
|
+
@unit_value = options.first
|
62
|
+
@unit_kind = parse_unit
|
63
|
+
|
64
|
+
when String
|
65
|
+
value, prefix = *split_value(options[0])
|
66
|
+
@unit_kind = who_is_my_prefix?(prefix)
|
67
|
+
# Value is absolute, needs convert to scale of prefix
|
68
|
+
@unit_value = value.to_f * unit_scale
|
46
69
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# @param [Fixnum|Float]
|
51
|
-
# @return Unit
|
52
|
-
def initialize(unit)
|
53
|
-
@unit_value = unit
|
54
|
-
@unit_kind = parse_unit
|
70
|
+
else
|
71
|
+
raise ArgumentError, "Invalid Unit Format"
|
72
|
+
end
|
55
73
|
end
|
56
74
|
|
57
75
|
# Public call to get a unit best representation
|
58
76
|
# @return String
|
59
77
|
def best_scale
|
60
|
-
|
78
|
+
@best_scale ||= best_value_with_scale
|
61
79
|
end
|
62
80
|
|
63
|
-
#
|
81
|
+
# Comparable units
|
82
|
+
# => Compare with scale of kinds
|
64
83
|
def <=>(comparison)
|
65
|
-
UNITS_DEFINITION.
|
84
|
+
UNITS_DEFINITION[unit_kind].last <=> UNITS_DEFINITION[comparison.unit_kind].last
|
85
|
+
end
|
86
|
+
|
87
|
+
def ==(comparison)
|
88
|
+
unit_kind == comparison.unit_kind
|
66
89
|
end
|
67
90
|
|
68
|
-
# convert to a specified unit string or to the same
|
69
|
-
def convert_to(
|
70
|
-
return self if
|
71
|
-
scalar = prefix_is_defined?(prefix)
|
72
|
-
absolute_unit_value = convert_base_prefix_to_value self.unit_value, scalar
|
91
|
+
# convert to a specified unit string or to the same unit as another Unit
|
92
|
+
def convert_to(other)
|
93
|
+
return self if other.nil?
|
73
94
|
|
74
|
-
|
95
|
+
case other
|
96
|
+
when Unit
|
97
|
+
return self if other == self
|
98
|
+
target = other
|
99
|
+
when String
|
100
|
+
target = SIUnits::Unit.new(other.to_f)
|
101
|
+
else
|
102
|
+
raise ArgumentError, "Unknown target units"
|
103
|
+
end
|
75
104
|
end
|
76
105
|
|
77
|
-
|
106
|
+
alias :>> :convert_to
|
78
107
|
|
79
|
-
|
80
|
-
|
81
|
-
# => Don't raise a ZeroDivisionError, the zero is defined
|
82
|
-
def best_value_with_scale
|
83
|
-
aliase, scalar = UNITS_DEFINITION[@unit_kind]
|
84
|
-
return aliase.first if scalar == 0
|
85
|
-
[(@unit_value / scalar), aliase.first].join
|
108
|
+
def to_s
|
109
|
+
[best_scale, unit_kind].join(' ')
|
86
110
|
end
|
87
111
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
112
|
+
private
|
113
|
+
|
114
|
+
# Logic to convert the current unit value to a best form of representation
|
115
|
+
# == Just remove the "e base" from value
|
116
|
+
def best_value_with_scale
|
117
|
+
(unit_value / unit_scale).round(4)
|
92
118
|
end
|
93
119
|
|
94
120
|
# The parser
|
@@ -121,14 +147,19 @@ module SIUnits
|
|
121
147
|
end
|
122
148
|
end
|
123
149
|
|
124
|
-
|
125
|
-
# @return Fixnum with scale of prefix
|
126
|
-
# @raise ArgumentError
|
127
|
-
def prefix_is_defined?(kind)
|
150
|
+
def who_is_my_prefix?(prefix)
|
128
151
|
UNITS_DEFINITION.each { |key, value|
|
129
|
-
return
|
152
|
+
return key if value[0].include?(prefix)
|
130
153
|
}
|
131
154
|
raise ArgumentError, "Unknown prefix"
|
132
155
|
end
|
156
|
+
|
157
|
+
def unit_scale
|
158
|
+
@unit_scale ||= UNITS_DEFINITION[unit_kind].last
|
159
|
+
end
|
160
|
+
|
161
|
+
def split_value(value)
|
162
|
+
value.scan(UNIT_REGEX).flatten
|
163
|
+
end
|
133
164
|
end
|
134
165
|
end
|
data/lib/si_units/version.rb
CHANGED
data/spec/units/unit_spec.rb
CHANGED
@@ -2,202 +2,49 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
require "unit"
|
4
4
|
|
5
|
-
describe
|
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
|
5
|
+
describe SIUnits::Unit do
|
15
6
|
|
16
|
-
|
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
|
7
|
+
context "it's a pico " do
|
27
8
|
|
28
|
-
it "min limit" do
|
29
|
-
unit = SIUnits::Unit.new(1e-9)
|
30
|
-
expect(unit.best_scale).to match(/[n]/)
|
31
9
|
end
|
32
10
|
|
33
|
-
|
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
|
11
|
+
context "it's a nano " do
|
49
12
|
|
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
13
|
end
|
54
|
-
end
|
55
14
|
|
56
|
-
|
57
|
-
it "max limit" do
|
58
|
-
unit = SIUnits::Unit.new((1e-6) + 0.000001)
|
59
|
-
expect(unit.best_scale).to match(/[m]/)
|
60
|
-
end
|
15
|
+
context "it's a micro " do
|
61
16
|
|
62
|
-
it "min limit" do
|
63
|
-
unit = SIUnits::Unit.new(1e-3)
|
64
|
-
expect(unit.best_scale).to match(/[m]/)
|
65
17
|
end
|
66
18
|
|
67
|
-
|
68
|
-
unit = SIUnits::Unit.new((1e-3) + 0.001)
|
69
|
-
expect(unit.best_scale).to_not match(/[m]/)
|
70
|
-
end
|
71
|
-
end
|
19
|
+
context "it's a milli " do
|
72
20
|
|
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
21
|
end
|
78
22
|
|
79
|
-
|
80
|
-
unit = SIUnits::Unit.new(1e-2)
|
81
|
-
expect(unit.best_scale).to match(/[c]/)
|
82
|
-
end
|
23
|
+
context "it's a const " do
|
83
24
|
|
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
25
|
end
|
88
|
-
end
|
89
26
|
|
90
|
-
|
91
|
-
it "max limit" do
|
92
|
-
unit = SIUnits::Unit.new((1e-2) + 0.01)
|
93
|
-
expect(unit.best_scale).to match(/[d]/)
|
27
|
+
context "it's a kilo " do
|
94
28
|
end
|
95
29
|
|
96
|
-
|
97
|
-
unit = SIUnits::Unit.new(1e-1)
|
98
|
-
expect(unit.best_scale).to match(/[d]/)
|
99
|
-
end
|
30
|
+
context "it's a mega " do
|
100
31
|
|
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
32
|
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe "it's a const " do
|
108
33
|
|
109
|
-
it "
|
110
|
-
unit = SIUnits::Unit.new(0)
|
111
|
-
expect(unit.best_scale).to match(/zero/)
|
112
|
-
end
|
34
|
+
context "it's a giga " do
|
113
35
|
|
114
|
-
it "not a zero" do
|
115
|
-
unit = SIUnits::Unit.new(0.1)
|
116
|
-
expect(unit.best_scale).to_not match(/zero/)
|
117
36
|
end
|
118
|
-
end
|
119
37
|
|
120
|
-
|
121
|
-
|
122
|
-
unit = SIUnits::Unit.new((1e2) - 1.0)
|
123
|
-
expect(unit.best_scale).to match(/(da)/)
|
38
|
+
context "Its comparable" do
|
39
|
+
it "Compare with"
|
124
40
|
end
|
125
41
|
|
126
|
-
|
127
|
-
|
128
|
-
expect(unit.best_scale).to match(/(da)/)
|
42
|
+
context "Convert to" do
|
43
|
+
it "Convert to"
|
129
44
|
end
|
130
45
|
|
131
|
-
|
132
|
-
|
133
|
-
expect(unit.best_scale).to_not match(/da/)
|
46
|
+
context "Convert string to unit" do
|
47
|
+
it "string to unit"
|
134
48
|
end
|
135
|
-
end
|
136
49
|
|
137
|
-
describe "it's a hecto " do
|
138
|
-
it "max limit" do
|
139
|
-
unit = SIUnits::Unit.new((1e3) - 1.0)
|
140
|
-
expect(unit.best_scale).to match(/[h]/)
|
141
|
-
end
|
142
|
-
|
143
|
-
it "min limit" do
|
144
|
-
unit = SIUnits::Unit.new(1e2 + 1.0)
|
145
|
-
expect(unit.best_scale).to match(/[h]/)
|
146
|
-
end
|
147
|
-
|
148
|
-
it "not a deca" do
|
149
|
-
unit = SIUnits::Unit.new((1e2) - 1.0)
|
150
|
-
expect(unit.best_scale).to_not match(/[h]/)
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe "it's a kilo " do
|
155
|
-
it "max limit" do
|
156
|
-
unit = SIUnits::Unit.new((1e6) - 1.0)
|
157
|
-
expect(unit.best_scale).to match(/[k]/)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "min limit" do
|
161
|
-
unit = SIUnits::Unit.new(1e3 + 1.0)
|
162
|
-
expect(unit.best_scale).to match(/[k]/)
|
163
|
-
end
|
164
|
-
|
165
|
-
it "not a deca" do
|
166
|
-
unit = SIUnits::Unit.new((1e3) - 1.0)
|
167
|
-
expect(unit.best_scale).to_not match(/[k]/)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
describe "it's a mega " do
|
172
|
-
it "max limit" do
|
173
|
-
unit = SIUnits::Unit.new((1e9) - 1.0)
|
174
|
-
expect(unit.best_scale).to match(/[M]/)
|
175
|
-
end
|
176
|
-
|
177
|
-
it "min limit" do
|
178
|
-
unit = SIUnits::Unit.new(1e6 + 1.0)
|
179
|
-
expect(unit.best_scale).to match(/[M]/)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "not a deca" do
|
183
|
-
unit = SIUnits::Unit.new((1e6) - 1.0)
|
184
|
-
expect(unit.best_scale).to_not match(/[M]/)
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
describe "it's a giga " do
|
189
|
-
it "max limit" do
|
190
|
-
unit = SIUnits::Unit.new((1e12) - 1.0)
|
191
|
-
expect(unit.best_scale).to match(/[G]/)
|
192
|
-
end
|
193
|
-
|
194
|
-
it "min limit" do
|
195
|
-
unit = SIUnits::Unit.new(1e9 + 1.0)
|
196
|
-
expect(unit.best_scale).to match(/[G]/)
|
197
|
-
end
|
198
|
-
|
199
|
-
it "not a deca" do
|
200
|
-
unit = SIUnits::Unit.new((1e9) - 1.0)
|
201
|
-
expect(unit.best_scale).to_not match(/[G]/)
|
202
|
-
end
|
203
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.7
|
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-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,7 +100,6 @@ files:
|
|
100
100
|
- lib/unit.rb
|
101
101
|
- ruby-si-unit.gemspec
|
102
102
|
- spec/spec_helper.rb
|
103
|
-
- spec/units/string_spec.rb
|
104
103
|
- spec/units/unit_spec.rb
|
105
104
|
homepage: https://github.com/gnomex/ruby-si-units
|
106
105
|
licenses:
|
@@ -122,11 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
121
|
version: '0'
|
123
122
|
requirements: []
|
124
123
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
124
|
+
rubygems_version: 2.1.1
|
126
125
|
signing_key:
|
127
126
|
specification_version: 4
|
128
127
|
summary: A SI prefix unit handling library for ruby
|
129
128
|
test_files:
|
130
129
|
- spec/spec_helper.rb
|
131
|
-
- spec/units/string_spec.rb
|
132
130
|
- spec/units/unit_spec.rb
|
data/spec/units/string_spec.rb
DELETED