unitwise 1.0.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +10 -0
- data/README.md +29 -23
- data/lib/unitwise/base.rb +1 -1
- data/lib/unitwise/ext.rb +3 -0
- data/lib/unitwise/ext/numeric.rb +4 -2
- data/lib/unitwise/scale.rb +6 -1
- data/lib/unitwise/term.rb +6 -2
- data/lib/unitwise/version.rb +1 -1
- data/test/unitwise/measurement_test.rb +8 -0
- metadata +28 -49
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cdeefee8c44bb010c6198248b58819010f753d6f
|
4
|
+
data.tar.gz: 3e7283137b22b8bf7bf621b8585ce29680ce993e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1727d64bdd2929aa882a8150ca3f735a9e95035aebc06fb466e87108f4ba5bf9a3b4d2f734b0cebae0ef37fb9125616c2a307f61951802094fe0db7f656728f1
|
7
|
+
data.tar.gz: 9c516ce0638721abfe37b8a5f3236d39214896b96c46b813e9a8507027020c40748d9f64e24dcf16dd9228e790fcc6a47c3529fcccbc8f196510a7b5b1e8c142
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,16 @@ version 1.0.0.
|
|
5
5
|
|
6
6
|
Unitwise uses semantic versioning.
|
7
7
|
|
8
|
+
## 1.1.0 - 2015-09-10
|
9
|
+
|
10
|
+
- `require unitwise/ext` has been deprecated as it is a performance drag and
|
11
|
+
violates Ruby best practices. Use `require unitwise` instead. Any use of the
|
12
|
+
Numeric helpers like `1.meter`, `2.to_foot` will need to change to
|
13
|
+
`Unitwise(1, 'meter')`, and `Unitwise(2, 'foot')`.
|
14
|
+
- `#to_s` should no longer return the unexpected ' 1' suffix for dimless measurements.
|
15
|
+
- `#to_s(mode)` will fall back to using the atom's `primary_code` if the mode
|
16
|
+
isn't available.
|
17
|
+
|
8
18
|
## 1.0.4 - 2015-01-10
|
9
19
|
|
10
20
|
- Added Ruby 2.2 support.
|
data/README.md
CHANGED
@@ -13,9 +13,11 @@ For an over the top example, consider a car (2800 lb) completing the quarter
|
|
13
13
|
mile in 10 seconds (with uniform acceleration).
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
require 'unitwise'
|
17
|
+
|
18
|
+
distance = Unitwise(0.25, 'mile') # => #<Unitwise::Measurement value=0.25 unit=mile>
|
19
|
+
time = Unitwise(10, 'second') # => #<Unitwise::Measurement value=10 unit=second>
|
20
|
+
mass = Unitwise(2800, 'pound') # => #<Unitwise::Measurement value=2800 unit=pound>
|
19
21
|
|
20
22
|
acceleration = 2.0 * distance / time ** 2
|
21
23
|
# => #<Unitwise::Measurement value=0.005 unit=[mi_us]/s2>
|
@@ -71,23 +73,13 @@ Unitwise(2.3, 'kilogram') # => #<Unitwise::Measurement value=2.3 unit=kilogram>
|
|
71
73
|
Unitwise(100, 'pound') # => #<Unitwise::Measurement value=100 unit=pound>
|
72
74
|
```
|
73
75
|
|
74
|
-
Unitwise doesn't mess with the core library by default. However, you can
|
75
|
-
optionally require the core extensions for some handy helpers.
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
require 'unitwise/ext'
|
79
|
-
|
80
|
-
1.convert_to('liter') # => #<Unitwise::Measurement value=1 unit=liter>
|
81
|
-
4.teaspoon # => #<Unitwise::Measurement value=4 unit=teaspoon>
|
82
|
-
```
|
83
|
-
|
84
76
|
### Conversion
|
85
77
|
|
86
78
|
Unitwise is able to convert any unit within the UCUM spec to any other
|
87
79
|
compatible unit.
|
88
80
|
|
89
81
|
```ruby
|
90
|
-
5.0
|
82
|
+
Unitwise(5.0, 'kilometer').convert_to('mile')
|
91
83
|
# => #<Unitwise::Measurement value=3.106849747474748 unit=mile>
|
92
84
|
```
|
93
85
|
|
@@ -104,8 +96,8 @@ Unitwise(26.2, 'mile').to_kilometer
|
|
104
96
|
It also has the ability to compare measurements with the same or different units.
|
105
97
|
|
106
98
|
```ruby
|
107
|
-
12
|
108
|
-
1
|
99
|
+
Unitwise(12, 'inch') == Unitwise(1, 'foot') # => true
|
100
|
+
Unitwise(1, 'meter') > Unitwise(1, 'yard') # => true
|
109
101
|
```
|
110
102
|
|
111
103
|
Again, you have to compare compatible units. For example, comparing two
|
@@ -116,8 +108,8 @@ temperatures will work, comparing a mass to a length would fail.
|
|
116
108
|
You can use shorthand for SI units.
|
117
109
|
|
118
110
|
```ruby
|
119
|
-
1000
|
120
|
-
1
|
111
|
+
Unitwise(1000, 'm') == Unitwise(1, 'km') # => true
|
112
|
+
Unitwise(1, 'ml') == Unitwise(0.001, 'l') # => true
|
121
113
|
```
|
122
114
|
|
123
115
|
### Complex Units
|
@@ -145,29 +137,28 @@ Unitwise(1000, 'kg.s-1.(m/s)2').to_kilowatt
|
|
145
137
|
You can add or subtract compatible measurements.
|
146
138
|
|
147
139
|
```ruby
|
148
|
-
2.0
|
140
|
+
Unitwise(2.0, 'meter') + Unitwise(3.0, 'inch') - Unitwise(1.0, 'yard')
|
149
141
|
# => #<Unitwise::Measurement value=1.1618 unit=meter>
|
150
142
|
```
|
151
143
|
|
152
144
|
You can multiply or divide measurements and numbers.
|
153
145
|
|
154
146
|
```ruby
|
155
|
-
110
|
147
|
+
Unitwise(110, 'volt') * 2
|
156
148
|
# => #<Unitwise::Measurement value=220 unit=volt>
|
157
149
|
```
|
158
150
|
|
159
151
|
You can multiply or divide measurements with measurements.
|
160
152
|
|
161
153
|
```ruby
|
162
|
-
20
|
154
|
+
Unitwise(20, 'milligram') / Unitwise(1, 'liter')
|
163
155
|
# => #<Unitwise::Measurement value=20 unit=mg/l>
|
164
|
-
|
165
156
|
```
|
166
157
|
|
167
158
|
Exponentiation is also supported.
|
168
159
|
|
169
160
|
```ruby
|
170
|
-
(10
|
161
|
+
(Unitwise(10, 'cm') ** 3).to_liter
|
171
162
|
# => #<Unitwise::Measurement value=1 unit=liter>
|
172
163
|
```
|
173
164
|
|
@@ -246,6 +237,21 @@ Unitwise(1, "meter/s") # Does not work, mixed designations (name and primar
|
|
246
237
|
Unitwise(1, "meter") / Unitwise(1, "s") # Also works
|
247
238
|
```
|
248
239
|
|
240
|
+
## Core extensions (deprecated)
|
241
|
+
|
242
|
+
Unitwise doesn't mess with the core library by default. However, you can
|
243
|
+
optionally require the core extensions for some handy helpers. I find these
|
244
|
+
fun for doing calculations in an irb or pry session, but use them in your
|
245
|
+
application at your own risk. These extensions have been known to negatively
|
246
|
+
impact performance, and will be removed in a future version.
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
require 'unitwise/ext'
|
250
|
+
|
251
|
+
4.teaspoon # => #<Unitwise::Measurement value=4 unit=teaspoon>
|
252
|
+
1.convert_to('liter') # => #<Unitwise::Measurement value=1 unit=liter>
|
253
|
+
```
|
254
|
+
|
249
255
|
## Supported Ruby Versions
|
250
256
|
|
251
257
|
This library aims to support and is tested against the following Ruby
|
data/lib/unitwise/base.rb
CHANGED
data/lib/unitwise/ext.rb
CHANGED
data/lib/unitwise/ext/numeric.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Unitwise extends Numeric to add these dyanmic method conveniences: `1.meter`,
|
2
|
-
# `26.2.to_mile`, and `4.convert_to("Joule")`. These overrides are optional
|
3
|
-
#
|
2
|
+
# `26.2.to_mile`, and `4.convert_to("Joule")`. These overrides are optional
|
3
|
+
# and may be enabled with `require 'unitwise/ext'`. These methods are known to
|
4
|
+
# reduce performance, as well as violate Ruby best practices. They are
|
5
|
+
# deprecated and will be removed in a future version.
|
4
6
|
class Numeric
|
5
7
|
# Converts numeric to a measurement
|
6
8
|
# @param unit [Unitwise::Unit, String] The unit to use in the measurement
|
data/lib/unitwise/scale.rb
CHANGED
@@ -116,7 +116,12 @@ module Unitwise
|
|
116
116
|
# Convert to a simple string representing the scale.
|
117
117
|
# @api public
|
118
118
|
def to_s(mode = nil)
|
119
|
-
|
119
|
+
unit_string = unit.to_s(mode)
|
120
|
+
if unit_string && unit_string != '1'
|
121
|
+
"#{simplified_value} #{unit_string}"
|
122
|
+
else
|
123
|
+
simplified_value.to_s
|
124
|
+
end
|
120
125
|
end
|
121
126
|
|
122
127
|
def inspect
|
data/lib/unitwise/term.rb
CHANGED
@@ -114,8 +114,12 @@ module Unitwise
|
|
114
114
|
end
|
115
115
|
|
116
116
|
def to_s(mode = :primary_code)
|
117
|
-
[
|
118
|
-
(
|
117
|
+
[
|
118
|
+
(factor if factor != 1),
|
119
|
+
(prefix.send(mode) if prefix),
|
120
|
+
(atom.send(mode) if atom),
|
121
|
+
(exponent if exponent != 1)
|
122
|
+
].compact.join('')
|
119
123
|
end
|
120
124
|
|
121
125
|
private
|
data/lib/unitwise/version.rb
CHANGED
@@ -228,6 +228,14 @@ describe Unitwise::Measurement do
|
|
228
228
|
temp.to_s(:primary_code).must_equal("25 Cel")
|
229
229
|
temp.to_s(:symbol).must_equal("25 °C")
|
230
230
|
end
|
231
|
+
it "should fallback when there is no value for the provided mode" do
|
232
|
+
vol = described_class.new(12, "teaspoon")
|
233
|
+
vol.to_s(:symbol).must_equal("12 [tsp_us]")
|
234
|
+
end
|
235
|
+
it "should not return '1 1' for dimless measurements" do
|
236
|
+
dimless = described_class.new(1, "1")
|
237
|
+
dimless.to_s.must_equal("1")
|
238
|
+
end
|
231
239
|
end
|
232
240
|
|
233
241
|
end
|
metadata
CHANGED
@@ -1,158 +1,139 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Josh Lewis
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2015-
|
11
|
+
date: 2015-09-10 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: liner
|
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.2'
|
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.2'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: signed_multiset
|
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.2'
|
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.2'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: memoizable
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- - ~>
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0.4'
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- - ~>
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0.4'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: parslet
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- - ~>
|
59
|
+
- - "~>"
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: 1.5.0
|
70
62
|
type: :runtime
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- - ~>
|
66
|
+
- - "~>"
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: 1.5.0
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: nokogiri
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- - ~>
|
73
|
+
- - "~>"
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: 1.5.10
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- - ~>
|
80
|
+
- - "~>"
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: 1.5.10
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: pry
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
|
-
- - ~>
|
87
|
+
- - "~>"
|
100
88
|
- !ruby/object:Gem::Version
|
101
89
|
version: '0.9'
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
|
-
- - ~>
|
94
|
+
- - "~>"
|
108
95
|
- !ruby/object:Gem::Version
|
109
96
|
version: '0.9'
|
110
97
|
- !ruby/object:Gem::Dependency
|
111
98
|
name: minitest
|
112
99
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
100
|
requirements:
|
115
|
-
- - ~>
|
101
|
+
- - "~>"
|
116
102
|
- !ruby/object:Gem::Version
|
117
103
|
version: '5.0'
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
107
|
requirements:
|
123
|
-
- - ~>
|
108
|
+
- - "~>"
|
124
109
|
- !ruby/object:Gem::Version
|
125
110
|
version: '5.0'
|
126
111
|
- !ruby/object:Gem::Dependency
|
127
112
|
name: rake
|
128
113
|
requirement: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
114
|
requirements:
|
131
|
-
- - ~>
|
115
|
+
- - "~>"
|
132
116
|
- !ruby/object:Gem::Version
|
133
117
|
version: '10.0'
|
134
118
|
type: :development
|
135
119
|
prerelease: false
|
136
120
|
version_requirements: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
121
|
requirements:
|
139
|
-
- - ~>
|
122
|
+
- - "~>"
|
140
123
|
- !ruby/object:Gem::Version
|
141
124
|
version: '10.0'
|
142
125
|
- !ruby/object:Gem::Dependency
|
143
126
|
name: nori
|
144
127
|
requirement: !ruby/object:Gem::Requirement
|
145
|
-
none: false
|
146
128
|
requirements:
|
147
|
-
- - ~>
|
129
|
+
- - "~>"
|
148
130
|
- !ruby/object:Gem::Version
|
149
131
|
version: '2.3'
|
150
132
|
type: :development
|
151
133
|
prerelease: false
|
152
134
|
version_requirements: !ruby/object:Gem::Requirement
|
153
|
-
none: false
|
154
135
|
requirements:
|
155
|
-
- - ~>
|
136
|
+
- - "~>"
|
156
137
|
- !ruby/object:Gem::Version
|
157
138
|
version: '2.3'
|
158
139
|
description: Physical quantity and units of measure conversion and math library
|
@@ -162,9 +143,9 @@ executables: []
|
|
162
143
|
extensions: []
|
163
144
|
extra_rdoc_files: []
|
164
145
|
files:
|
165
|
-
- .gitignore
|
166
|
-
- .ruby-version
|
167
|
-
- .travis.yml
|
146
|
+
- ".gitignore"
|
147
|
+
- ".ruby-version"
|
148
|
+
- ".travis.yml"
|
168
149
|
- CHANGELOG.md
|
169
150
|
- Gemfile
|
170
151
|
- LICENSE.txt
|
@@ -222,27 +203,26 @@ files:
|
|
222
203
|
homepage: http://github.com/joshwlewis/unitwise
|
223
204
|
licenses:
|
224
205
|
- MIT
|
206
|
+
metadata: {}
|
225
207
|
post_install_message:
|
226
208
|
rdoc_options: []
|
227
209
|
require_paths:
|
228
210
|
- lib
|
229
211
|
required_ruby_version: !ruby/object:Gem::Requirement
|
230
|
-
none: false
|
231
212
|
requirements:
|
232
|
-
- -
|
213
|
+
- - ">="
|
233
214
|
- !ruby/object:Gem::Version
|
234
215
|
version: '0'
|
235
216
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
236
|
-
none: false
|
237
217
|
requirements:
|
238
|
-
- -
|
218
|
+
- - ">="
|
239
219
|
- !ruby/object:Gem::Version
|
240
220
|
version: '0'
|
241
221
|
requirements: []
|
242
222
|
rubyforge_project:
|
243
|
-
rubygems_version:
|
223
|
+
rubygems_version: 2.4.5
|
244
224
|
signing_key:
|
245
|
-
specification_version:
|
225
|
+
specification_version: 4
|
246
226
|
summary: Convert between and perform mathematical operations on physical quantities
|
247
227
|
and units of measure defined by the Unified Code for Units of Measure.
|
248
228
|
test_files:
|
@@ -262,4 +242,3 @@ test_files:
|
|
262
242
|
- test/unitwise/term_test.rb
|
263
243
|
- test/unitwise/unit_test.rb
|
264
244
|
- test/unitwise_test.rb
|
265
|
-
has_rdoc:
|