ruby-si-units 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/si_units/string.rb +1 -6
- data/lib/si_units/unit.rb +61 -36
- data/lib/si_units/version.rb +2 -2
- data/lib/unit.rb +3 -3
- data/spec/units/unit_spec.rb +6 -40
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dd439cebc308b9ac78f93d5032f9b35451b04f5
|
4
|
+
data.tar.gz: 40936a1ce7817bb55c05103d17f533b568bc2d40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5872b30732861c26fc4f149256a64071d0a91ca4fee2eadfaf7a5b516c5b3e091aaecdf6dd863db1c59031662985910acf157213a401db0ccad384b71188047
|
7
|
+
data.tar.gz: b77c8c05ec713741d5ff8b55f4eae9d3366c7f35a4c7fb011707cc6b548c6f3e6ae9fda8de35f4af5629183d568224bdff4f3fbcbc079648a28c33cbb4e69d5d
|
data/lib/si_units/string.rb
CHANGED
@@ -2,12 +2,7 @@ class String
|
|
2
2
|
# make a string into a unit
|
3
3
|
# @return (see RubyUnits::Unit#initialize)
|
4
4
|
def to_unit
|
5
|
-
|
6
|
-
SIUnits::Unit.new(unit_reduced).convert_to(prefix)
|
7
|
-
end
|
8
|
-
|
9
|
-
def split_value(value)
|
10
|
-
value.scan(/(\d+\.?\d*)(\w+)/).flatten
|
5
|
+
SIUnits::Unit.new(self)
|
11
6
|
end
|
12
7
|
end
|
13
8
|
|
data/lib/si_units/unit.rb
CHANGED
@@ -8,7 +8,7 @@ module SIUnits
|
|
8
8
|
class Unit
|
9
9
|
include Comparable
|
10
10
|
|
11
|
-
attr_reader :
|
11
|
+
attr_reader :value, :kind
|
12
12
|
|
13
13
|
# Definition of SI Prefix Units, with name, aliases and scale
|
14
14
|
UNITS_DEFINITION = {
|
@@ -44,7 +44,7 @@ module SIUnits
|
|
44
44
|
'yocto' => [%w{y Yocto yocto}, 1e-24],
|
45
45
|
'zero' => [%w{zero}, 0.0]
|
46
46
|
}
|
47
|
-
UNIT_REGEX =
|
47
|
+
UNIT_REGEX = /\d+[\.?]\d+|\w+/
|
48
48
|
|
49
49
|
# Create a new Unit object.
|
50
50
|
# @return [Unit]
|
@@ -58,14 +58,14 @@ module SIUnits
|
|
58
58
|
case options[0]
|
59
59
|
when Numeric
|
60
60
|
# Conversion use a scale
|
61
|
-
@
|
62
|
-
@
|
61
|
+
@value = options.first
|
62
|
+
@kind = parse_unit
|
63
63
|
|
64
64
|
when String
|
65
65
|
value, prefix = *split_value(options[0])
|
66
|
-
@
|
66
|
+
@kind = who_is_my_prefix?(prefix)
|
67
67
|
# Value is absolute, needs convert to scale of prefix
|
68
|
-
@
|
68
|
+
@value = value.to_f * scale
|
69
69
|
|
70
70
|
else
|
71
71
|
raise ArgumentError, "Invalid Unit Format"
|
@@ -81,11 +81,11 @@ module SIUnits
|
|
81
81
|
# Comparable units
|
82
82
|
# => Compare with scale of kinds
|
83
83
|
def <=>(comparison)
|
84
|
-
UNITS_DEFINITION[
|
84
|
+
UNITS_DEFINITION[kind].last <=> UNITS_DEFINITION[comparison.kind].last
|
85
85
|
end
|
86
86
|
|
87
87
|
def ==(comparison)
|
88
|
-
|
88
|
+
kind == comparison.kind
|
89
89
|
end
|
90
90
|
|
91
91
|
# convert to a specified unit string or to the same unit as another Unit
|
@@ -106,7 +106,7 @@ module SIUnits
|
|
106
106
|
alias :>> :convert_to
|
107
107
|
|
108
108
|
def to_s
|
109
|
-
[best_scale,
|
109
|
+
[best_scale, kind].join(' ')
|
110
110
|
end
|
111
111
|
|
112
112
|
private
|
@@ -114,7 +114,7 @@ module SIUnits
|
|
114
114
|
# Logic to convert the current unit value to a best form of representation
|
115
115
|
# == Just remove the "e base" from value
|
116
116
|
def best_value_with_scale
|
117
|
-
(
|
117
|
+
(value / scale).round(4)
|
118
118
|
end
|
119
119
|
|
120
120
|
# The parser
|
@@ -122,40 +122,65 @@ module SIUnits
|
|
122
122
|
# @return String with the name of representation
|
123
123
|
# @raise ArgumentError
|
124
124
|
def parse_unit
|
125
|
-
case @
|
126
|
-
when 0
|
127
|
-
|
128
|
-
when 1e-
|
129
|
-
|
130
|
-
when 1e-
|
131
|
-
|
132
|
-
when 1e-
|
133
|
-
|
134
|
-
when 1e-
|
135
|
-
|
136
|
-
when 1e-
|
137
|
-
|
138
|
-
when
|
139
|
-
|
140
|
-
when
|
141
|
-
|
142
|
-
when
|
143
|
-
|
144
|
-
when
|
145
|
-
|
146
|
-
|
125
|
+
case @value
|
126
|
+
when 0
|
127
|
+
return "zero"
|
128
|
+
when 1e-24..1e-21
|
129
|
+
return "yocto"
|
130
|
+
when 1e-21..1e-18
|
131
|
+
return "atto"
|
132
|
+
when 1e-18..1e-15
|
133
|
+
return "femto"
|
134
|
+
when 1e-15..1e-12
|
135
|
+
return "pico"
|
136
|
+
when 1e-12..1e-9
|
137
|
+
return "nano"
|
138
|
+
when 1e-9..1e-6
|
139
|
+
return "micro"
|
140
|
+
when 1e-6..1e-3
|
141
|
+
return "milli"
|
142
|
+
when 1e-3..1e-2
|
143
|
+
return "centi"
|
144
|
+
when 1e-2..1e-1
|
145
|
+
return "deci"
|
146
|
+
when 1e-1..1e1
|
147
|
+
return "1"
|
148
|
+
when 1e1..1e2
|
149
|
+
return "deca"
|
150
|
+
when 1e2..1e3
|
151
|
+
return "hecto"
|
152
|
+
when 1e3..1e6
|
153
|
+
return "kilo"
|
154
|
+
when 1e6..1e9
|
155
|
+
return "mega"
|
156
|
+
when 1e9..1e12
|
157
|
+
return "giga"
|
158
|
+
when 1e12..1e15
|
159
|
+
return "tera"
|
160
|
+
when 1e15..1e18
|
161
|
+
return "peta"
|
162
|
+
when 1e18..1e21
|
163
|
+
return "exa"
|
164
|
+
when 1e21..1e24
|
165
|
+
return "zetta"
|
166
|
+
else
|
167
|
+
raise ArgumentError, "Unit out of range"
|
147
168
|
end
|
148
169
|
end
|
149
170
|
|
150
171
|
def who_is_my_prefix?(prefix)
|
151
|
-
|
172
|
+
|
173
|
+
prefix unless UNITS_DEFINITION.has_key?(prefix)
|
174
|
+
|
175
|
+
UNITS_DEFINITION.each do |key, value|
|
152
176
|
return key if value[0].include?(prefix)
|
153
|
-
|
177
|
+
end
|
178
|
+
|
154
179
|
raise ArgumentError, "Unknown prefix"
|
155
180
|
end
|
156
181
|
|
157
|
-
def
|
158
|
-
@
|
182
|
+
def scale
|
183
|
+
@scale ||= UNITS_DEFINITION[kind].last
|
159
184
|
end
|
160
185
|
|
161
186
|
def split_value(value)
|
data/lib/si_units/version.rb
CHANGED
data/lib/unit.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require_relative 'si_units/string'
|
2
|
+
require_relative 'si_units/unit'
|
3
|
+
require_relative 'si_units/version'
|
data/spec/units/unit_spec.rb
CHANGED
@@ -4,47 +4,13 @@ require "unit"
|
|
4
4
|
|
5
5
|
describe SIUnits::Unit do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
context "it's a nano " do
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
context "it's a micro " do
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
context "it's a milli " do
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
context "it's a const " do
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
context "it's a kilo " do
|
28
|
-
end
|
29
|
-
|
30
|
-
context "it's a mega " do
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
context "it's a giga " do
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
context "Its comparable" do
|
39
|
-
it "Compare with"
|
40
|
-
end
|
7
|
+
end
|
41
8
|
|
42
|
-
context "Convert to" do
|
43
|
-
it "Convert to"
|
44
|
-
end
|
45
9
|
|
46
|
-
|
47
|
-
|
48
|
-
|
10
|
+
describe "Convert to" do
|
11
|
+
it "Convert to"
|
12
|
+
end
|
49
13
|
|
14
|
+
describe "Convert string to unit" do
|
15
|
+
it "string to unit"
|
50
16
|
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.8
|
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-
|
11
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|