unitwise 0.2.2 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59c17c683ada5799884a58c772a51eb20b59a5b8
4
- data.tar.gz: f8fb3b7099821b43a090d3253edb6e93a728dad3
3
+ metadata.gz: b743603a818807533ed423c0317ac33b05976d78
4
+ data.tar.gz: ac6d5fb45058fecc3575e771f2e605cbc3e4afe0
5
5
  SHA512:
6
- metadata.gz: 7ebe5b4426d7adf2a107fa4ab122ebffffcc79468b342e9592b06e0dc434fe90582db96a5e563c1c755bdd8b66fa26c5146c59879c8e66c9e2d697a9d1e4762f
7
- data.tar.gz: 685a1a81769b575066d608572038ebaf43eab8c04bf9e635971ec7ff8080fee248ea1f549ba1d9e94d95af8032959f5080fcbec091846e346cb4e8d28fa7b8dc
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
- ```ruby
18
+ Instantiate measurements with `Unitwise()`
19
19
 
20
+ ```ruby
20
21
  require 'unitwise'
21
22
 
22
- 2.3.kilogram # => <Unitwise::Measurement 2.3 kilogram>
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
- 4.convert('pound') # => <Unitwise::Measurement 4 pound>
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
- 26.2.mile.kilometer # => <Unitwise::Measurement 42.164897129794255 kilometer>
53
+ The prettier version of `convert(unit)` is just calling the unit name method:
33
54
 
34
- 5.kilometer.convert('mile') # => <Unitwise::Measurement 3.106849747474748 mile>
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
- ```ruby
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
- Note that you can also use SI abbreviations for units instead of names (i.e. ms for millisecond).
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
- ## UCUM Atom Codes
134
+ ### Unit Compatibility
65
135
 
66
- There are several units that share names in the UCUM specification. There are a few versions of inch and foot, for example. So, specifying `1.foot` may not always be appropriate. You may have to use a UCUM Atom code instead of the unit name:
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
- ```ruby
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
- 1.convert('[ft_i]') == 1.convert('[ft_us]') # => false
145
+ ### Unit Names and Atom Codes
71
146
 
72
- 3.convert('[in_br]') == 3.convert('[in_i]') # => false
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
- ## Compound Units
159
+ ```ruby
160
+ 1.convert('[ft_i]') == 1.convert('[ft_us]') # => false
77
161
 
78
- You can create compound units by multiplying or dividing measurements, or by using a compound string.
162
+ 3.convert('[in_br]') == 3.convert('[in_i]') # => false
163
+ ```
79
164
 
80
- ```ruby
165
+ ### List of available units
81
166
 
82
- 20.mile / 1.hour == 20.convert("mile/hour") # => true
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
- 4.convert("kg.(m/s)2") == 4.joule # => true
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
@@ -38,7 +38,9 @@ module Unitwise
38
38
  end
39
39
 
40
40
  def slugs
41
- names.map(&:to_slug)
41
+ names.map do |n|
42
+ n.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
43
+ end
42
44
  end
43
45
 
44
46
  end
data/lib/unitwise/ext.rb CHANGED
@@ -1,2 +1 @@
1
- require 'unitwise/ext/numeric'
2
- require 'unitwise/ext/string'
1
+ require 'unitwise/ext/numeric'
@@ -1,3 +1,3 @@
1
1
  module Unitwise
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'test_helper'
2
-
2
+ require 'unitwise/ext'
3
3
  describe Numeric do
4
4
 
5
5
  describe "#convert" do
@@ -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 /unitwise$/
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.2.2
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: 2013-12-16 00:00:00.000000000 Z
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
@@ -1,5 +0,0 @@
1
- class String
2
- def to_slug
3
- self.downcase.strip.gsub(/\s/, '_').gsub(/\W/, '')
4
- end
5
- end
@@ -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