unitwise 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +107 -20
- data/lib/unitwise.rb +8 -1
- data/lib/unitwise/base.rb +3 -1
- data/lib/unitwise/ext.rb +1 -2
- data/lib/unitwise/version.rb +1 -1
- data/test/unitwise/ext/numeric_test.rb +1 -1
- data/test/unitwise_test.rb +9 -1
- metadata +2 -5
- data/lib/unitwise/ext/string.rb +0 -5
- data/test/unitwise/ext/string_test.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b743603a818807533ed423c0317ac33b05976d78
|
4
|
+
data.tar.gz: ac6d5fb45058fecc3575e771f2e605cbc3e4afe0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39a648b7693240696ff80404452c1b19bd67569dea17a394d84a4b9cf69e4874f3645ca7e83bb4149ce0304a973adfb9f55ed110a9833036e3c418ecaf0b3e97
|
7
|
+
data.tar.gz: ba6432d868b98e2d0c93d1a821b400a54e9e837e09ee8a13ca41986ccf61a9e1a1586186f04e7cfb40297c6b07ae923109ca6593b634abad2057060148c15af0
|
data/README.md
CHANGED
@@ -15,42 +15,113 @@ Unitwise supports a vast number of units. At the time of writing, it supports 95
|
|
15
15
|
|
16
16
|
### Initialization:
|
17
17
|
|
18
|
-
|
18
|
+
Instantiate measurements with `Unitwise()`
|
19
19
|
|
20
|
+
```ruby
|
20
21
|
require 'unitwise'
|
21
22
|
|
22
|
-
2.3
|
23
|
+
Unitwise(2.3, 'kilogram') # => <Unitwise::Measurement 2.3 kilogram>
|
24
|
+
Unitwise('pound') # => <Unitwise::Measurement 1 pound>
|
25
|
+
```
|
26
|
+
|
27
|
+
or require the core extensions for some syntactic sugar.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'unitwise/ext'
|
23
31
|
|
24
|
-
|
32
|
+
1.convert(liter)
|
33
|
+
# => <Unitwise::Measurement 1 liter>
|
25
34
|
|
35
|
+
4.teaspoon
|
36
|
+
# => <Unitwise::Measurement 4 teaspoon>
|
26
37
|
```
|
27
38
|
|
28
39
|
### Conversion
|
29
40
|
|
41
|
+
Obviously, Unitwise handles simple unit conversion. You can convert to any
|
42
|
+
compatible unit (Unitwise won't let you convert say inches to pounds) with the
|
43
|
+
`convert(unit)` method.
|
44
|
+
|
30
45
|
```ruby
|
46
|
+
distance = Unitwise(5, 'kilometer')
|
47
|
+
# => <Unitwise::Measurement 5 kilometer>
|
48
|
+
|
49
|
+
distance.convert('mile')
|
50
|
+
# => <Unitwise::Measurement 3.106849747474748 mile>
|
51
|
+
```
|
31
52
|
|
32
|
-
|
53
|
+
The prettier version of `convert(unit)` is just calling the unit name method:
|
33
54
|
|
34
|
-
|
55
|
+
```ruby
|
56
|
+
distance = 26.2.mile
|
57
|
+
# => <Unitwise::Measurement 26.2 mile>
|
35
58
|
|
59
|
+
distance.kilometer
|
60
|
+
# => <Unitwise::Measurement 42.164897129794255 kilometer>
|
36
61
|
```
|
37
62
|
|
38
63
|
### Comparison
|
39
64
|
|
40
|
-
|
65
|
+
It also has the ability to compare measurements with the same or different units.
|
41
66
|
|
67
|
+
```ruby
|
42
68
|
12.inch == 1.foot # => true
|
43
69
|
|
44
70
|
1.meter > 1.yard # => true
|
71
|
+
```
|
72
|
+
|
73
|
+
Again, you have to compare compatible units. Dissimilar units will fail.
|
74
|
+
For example, comparing two temperatures will work, comparing a mass to a length would fail.
|
75
|
+
|
76
|
+
### SI abbreviations
|
77
|
+
|
78
|
+
You can use shorthand for SI units.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
1.m # => <Unitwise::Measurement 1 meter>
|
82
|
+
1.ml #=> <Unitwise::Measurement 1 milliliter>
|
83
|
+
```
|
84
|
+
|
85
|
+
### Complex Units
|
86
|
+
|
87
|
+
Units can be combined to make more complex ones. There is nothing special about
|
88
|
+
them -- they can still be converted, compared, or operated on.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
speed = Unitwise(60, 'mile/hour')
|
92
|
+
# => <Unitwise::Measurement 60 mile/hour>
|
45
93
|
|
94
|
+
speed.convert('m/s')
|
95
|
+
# => <Unitwise::Measurement 26.822453644907288 m/s>
|
96
|
+
```
|
97
|
+
|
98
|
+
Exponents and parenthesis are supported as well.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
Unitwise(1000, 'kg.s-1.(m/s)2').watt
|
102
|
+
# => <Unitwise::Measurement 1000 watt>
|
46
103
|
```
|
47
104
|
|
48
105
|
### Math
|
49
106
|
|
50
|
-
|
107
|
+
You can add or subtract compatible measurements.
|
51
108
|
|
52
109
|
```ruby
|
110
|
+
2.meter + 3.inch - 1.yard
|
111
|
+
# => <Unitwise::Measurement 1.1618 meter>
|
112
|
+
```
|
113
|
+
|
114
|
+
You can multiply or divide measurements and numbers.
|
53
115
|
|
116
|
+
```ruby
|
117
|
+
110.volt * 2
|
118
|
+
=> <Unitwise::Measurement 220 volt>
|
119
|
+
```
|
120
|
+
|
121
|
+
You can multiply or divide measurements with measurements. Here is a fun example
|
122
|
+
from Physics 101
|
123
|
+
|
124
|
+
```ruby
|
54
125
|
m = 20.kg # => <Unitwise::Measurement 20 kg>
|
55
126
|
|
56
127
|
a = 10.m / 1.s2 # => <Unitwise::Measurement 10 m/s2>
|
@@ -58,32 +129,48 @@ a = 10.m / 1.s2 # => <Unitwise::Measurement 10 m/s2>
|
|
58
129
|
f = m * a # => <Unitwise::Measurement 50 kg.m/s2>
|
59
130
|
|
60
131
|
f.newton # => <Unitwise::Measurement 50 newton>
|
61
|
-
|
62
132
|
```
|
63
133
|
|
64
|
-
|
134
|
+
### Unit Compatibility
|
65
135
|
|
66
|
-
|
136
|
+
Unitwise is fairly intelligent about unit compatibility. It boils each unit down
|
137
|
+
to it's basic composition to determine if they are compatible. For instance,
|
138
|
+
energy (say a Joule, which can be expressed as kg*m2/s2) would have the
|
139
|
+
components mass<sup>1</sup>, length<sup>2</sup>, and
|
140
|
+
time<sup>-2</sup>. Any unit that could be reduced to this same composition
|
141
|
+
would be considered compatible.
|
67
142
|
|
68
|
-
|
143
|
+
I've extracted this datatype into it's own gem ([SignedMultiset](//github.com/joshwlewis/signed_multiset)) if you find this construct interesting.
|
69
144
|
|
70
|
-
|
145
|
+
### Unit Names and Atom Codes
|
71
146
|
|
72
|
-
|
147
|
+
This library is based around the units in the UCUM specification, which is
|
148
|
+
extensive and well thought out. However, not all of our unit systems throughout
|
149
|
+
the world and history are consistent or logical. UCUM has devised a system where
|
150
|
+
each unit has a unique atom code to try and solve this. The previous code examples
|
151
|
+
don't show this, because for the most part you won't need it. Unitwise can
|
152
|
+
figure out most of the units by their name or symbol. If you find you need to
|
153
|
+
(or just want to be explicit) you use the UCUM atom codes without any
|
154
|
+
modification.
|
73
155
|
|
74
|
-
|
156
|
+
Just as an example, you can see here that there are actually a few versions of inch
|
157
|
+
and foot:
|
75
158
|
|
76
|
-
|
159
|
+
```ruby
|
160
|
+
1.convert('[ft_i]') == 1.convert('[ft_us]') # => false
|
77
161
|
|
78
|
-
|
162
|
+
3.convert('[in_br]') == 3.convert('[in_i]') # => false
|
163
|
+
```
|
79
164
|
|
80
|
-
|
165
|
+
### List of available units
|
81
166
|
|
82
|
-
|
167
|
+
- You can get the official list from the UCUM website in XML format.
|
168
|
+
[unitsofmeasure.org/ucum-essence.xml](http://unitsofmeasure.org/ucum-essence.xml)
|
83
169
|
|
84
|
-
|
170
|
+
- Unitwise occasionally converts the above XML into YAML for use by this
|
171
|
+
library.
|
172
|
+
[github.com/joshwlewis/unitwise/tree/master/data](//github.com/joshwlewis/unitwise/tree/master/data)
|
85
173
|
|
86
|
-
```
|
87
174
|
|
88
175
|
## Installation
|
89
176
|
|
data/lib/unitwise.rb
CHANGED
@@ -11,7 +11,6 @@ require 'unitwise/term'
|
|
11
11
|
require 'unitwise/unit'
|
12
12
|
require 'unitwise/function'
|
13
13
|
require 'unitwise/errors'
|
14
|
-
require 'unitwise/ext'
|
15
14
|
|
16
15
|
module Unitwise
|
17
16
|
def self.path
|
@@ -23,3 +22,11 @@ module Unitwise
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
25
|
+
def Unitwise(first_arg, last_arg=nil)
|
26
|
+
if last_arg
|
27
|
+
Unitwise::Measurement.new(first_arg, last_arg)
|
28
|
+
else
|
29
|
+
Unitwise::Measurement.new(1, first_arg)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
data/lib/unitwise/base.rb
CHANGED
data/lib/unitwise/ext.rb
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
require 'unitwise/ext/numeric'
|
2
|
-
require 'unitwise/ext/string'
|
1
|
+
require 'unitwise/ext/numeric'
|
data/lib/unitwise/version.rb
CHANGED
data/test/unitwise_test.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
describe Unitwise do
|
4
|
+
describe '()' do
|
5
|
+
it "should accept a number and string" do
|
6
|
+
Unitwise(2, 'm/s').must_be_instance_of Unitwise::Measurement
|
7
|
+
end
|
8
|
+
it "should accept a lonely string" do
|
9
|
+
Unitwise('kg').must_be_instance_of Unitwise::Measurement
|
10
|
+
end
|
11
|
+
end
|
4
12
|
it "should have a path" do
|
5
|
-
Unitwise.path.must_match
|
13
|
+
Unitwise.path.must_match(/unitwise$/)
|
6
14
|
end
|
7
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: signed_multiset
|
@@ -138,7 +138,6 @@ files:
|
|
138
138
|
- lib/unitwise/expression/transformer.rb
|
139
139
|
- lib/unitwise/ext.rb
|
140
140
|
- lib/unitwise/ext/numeric.rb
|
141
|
-
- lib/unitwise/ext/string.rb
|
142
141
|
- lib/unitwise/function.rb
|
143
142
|
- lib/unitwise/functional.rb
|
144
143
|
- lib/unitwise/measurement.rb
|
@@ -162,7 +161,6 @@ files:
|
|
162
161
|
- test/unitwise/expression/matcher_test.rb
|
163
162
|
- test/unitwise/expression/parser_test.rb
|
164
163
|
- test/unitwise/ext/numeric_test.rb
|
165
|
-
- test/unitwise/ext/string_test.rb
|
166
164
|
- test/unitwise/function_test.rb
|
167
165
|
- test/unitwise/measurement_test.rb
|
168
166
|
- test/unitwise/prefix_test.rb
|
@@ -203,7 +201,6 @@ test_files:
|
|
203
201
|
- test/unitwise/expression/matcher_test.rb
|
204
202
|
- test/unitwise/expression/parser_test.rb
|
205
203
|
- test/unitwise/ext/numeric_test.rb
|
206
|
-
- test/unitwise/ext/string_test.rb
|
207
204
|
- test/unitwise/function_test.rb
|
208
205
|
- test/unitwise/measurement_test.rb
|
209
206
|
- test/unitwise/prefix_test.rb
|
data/lib/unitwise/ext/string.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe String do
|
4
|
-
describe '#to_slug' do
|
5
|
-
it "should convert 'Pascal' to 'pascal'" do
|
6
|
-
"Pascal".to_slug.must_equal 'pascal'
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should convert 'degree Celsius' to 'degree_celsius'" do
|
10
|
-
"degree Celsius".to_slug.must_equal 'degree_celsius'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|