unitwise-193 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +21 -0
  5. data/CHANGELOG.md +44 -0
  6. data/Gemfile +10 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +297 -0
  9. data/Rakefile +21 -0
  10. data/data/base_unit.yaml +43 -0
  11. data/data/derived_unit.yaml +3542 -0
  12. data/data/prefix.yaml +121 -0
  13. data/lib/unitwise.rb +70 -0
  14. data/lib/unitwise/atom.rb +121 -0
  15. data/lib/unitwise/base.rb +58 -0
  16. data/lib/unitwise/compatible.rb +60 -0
  17. data/lib/unitwise/errors.rb +7 -0
  18. data/lib/unitwise/expression.rb +35 -0
  19. data/lib/unitwise/expression/composer.rb +41 -0
  20. data/lib/unitwise/expression/decomposer.rb +68 -0
  21. data/lib/unitwise/expression/matcher.rb +47 -0
  22. data/lib/unitwise/expression/parser.rb +58 -0
  23. data/lib/unitwise/expression/transformer.rb +37 -0
  24. data/lib/unitwise/ext.rb +2 -0
  25. data/lib/unitwise/ext/numeric.rb +45 -0
  26. data/lib/unitwise/functional.rb +117 -0
  27. data/lib/unitwise/measurement.rb +198 -0
  28. data/lib/unitwise/prefix.rb +24 -0
  29. data/lib/unitwise/scale.rb +139 -0
  30. data/lib/unitwise/search.rb +46 -0
  31. data/lib/unitwise/standard.rb +29 -0
  32. data/lib/unitwise/standard/base.rb +73 -0
  33. data/lib/unitwise/standard/base_unit.rb +21 -0
  34. data/lib/unitwise/standard/derived_unit.rb +49 -0
  35. data/lib/unitwise/standard/extras.rb +17 -0
  36. data/lib/unitwise/standard/function.rb +35 -0
  37. data/lib/unitwise/standard/prefix.rb +17 -0
  38. data/lib/unitwise/standard/scale.rb +25 -0
  39. data/lib/unitwise/term.rb +142 -0
  40. data/lib/unitwise/unit.rb +181 -0
  41. data/lib/unitwise/version.rb +3 -0
  42. data/test/support/scale_tests.rb +117 -0
  43. data/test/test_helper.rb +19 -0
  44. data/test/unitwise/atom_test.rb +129 -0
  45. data/test/unitwise/base_test.rb +6 -0
  46. data/test/unitwise/expression/decomposer_test.rb +45 -0
  47. data/test/unitwise/expression/matcher_test.rb +42 -0
  48. data/test/unitwise/expression/parser_test.rb +109 -0
  49. data/test/unitwise/ext/numeric_test.rb +54 -0
  50. data/test/unitwise/functional_test.rb +17 -0
  51. data/test/unitwise/measurement_test.rb +233 -0
  52. data/test/unitwise/prefix_test.rb +25 -0
  53. data/test/unitwise/scale_test.rb +7 -0
  54. data/test/unitwise/search_test.rb +18 -0
  55. data/test/unitwise/term_test.rb +55 -0
  56. data/test/unitwise/unit_test.rb +87 -0
  57. data/test/unitwise_test.rb +35 -0
  58. data/unitwise.gemspec +33 -0
  59. metadata +246 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 447e1bf6c829e89e1ccf811beb286147623868c7
4
+ data.tar.gz: 9265687e4f7728d36c5bfaad750cc84a38bc2c9a
5
+ SHA512:
6
+ metadata.gz: 5f4d6dab4e6f77bae8c7992d7fc27b8e4eba9c4aa14703befa63816ff14f37b5351999e5060b69879c0c02e76178bcafb835eed008a95e9dc9e2ea7a2d3698cd
7
+ data.tar.gz: 147a38baa74d5c85462a8dde1fdcbec208ff83eb2d72713cc8f41e2db18e9417de0681186998067dd5134ca0b860476214f2fa92010103d64801380a4457b3a4
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1 @@
1
+ 2.2.0
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ cache: bundler
3
+ bundler_args: --without yard pry
4
+ sudo: false
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1
9
+ - 2.2
10
+ - ruby-head
11
+ - rbx-2
12
+ matrix:
13
+ include:
14
+ - rvm: jruby-19mode
15
+ env: JRUBY_OPTS="$JRUBY_OPTS --debug"
16
+ - rvm: jruby-head
17
+ env: JRUBY_OPTS="$JRUBY_OPTS --debug"
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-head
21
+ fast_finish: true
@@ -0,0 +1,44 @@
1
+ # Unitwise Changelog
2
+
3
+ All notable changes to Unitwise will be documented in this file, starting at
4
+ version 1.0.0.
5
+
6
+ Unitwise uses semantic versioning.
7
+
8
+ ## 1.0.4 - 2015-01-10
9
+
10
+ - Added Ruby 2.2 support.
11
+ - Empty strings are no longer valid units.
12
+
13
+ ## 1.0.3 - 2014-10-05
14
+
15
+ ### Added
16
+ - Unitwise.valid? for checking validity of expressions
17
+
18
+ ## 1.0.2 - 2014-09-14
19
+
20
+ ### Fixed
21
+ - Decomposer caching is now a little smarter. This resulted in a mild
22
+ performance increase.
23
+
24
+ ## 1.0.1 - 2014-08-30
25
+
26
+ ### Fixed
27
+ - Move conditional dependencies to Gemfile in order to allow proper
28
+ installation issues on rbx and jruby.
29
+
30
+ ## 1.0.0 - 2014-08-25
31
+
32
+ ### Added
33
+ - Uniwise() now accepts a Unitwise::Measurement as the first argument.
34
+ - Unitwise::Measurement now supports #round.
35
+
36
+ ### Fixed
37
+ - Respect Rationals when inspecting/printing a Unitwise::Measurement.
38
+ - Dynamically created methods from unitwise/ext now work with #respond_to?
39
+ and #methods appropriately.
40
+
41
+ ### Deprecated
42
+ - Unitwise() and Unitwise::Measurement.new() now requires two non-optional
43
+ arguments (value and unit).
44
+ - Unitwise::Measurement no longer has an implicit Integer conversion.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'coveralls', '~> 0.7'
4
+ if RUBY_VERSION >= '2.2.0'
5
+ gem 'bigdecimal', '~> 1.2.6', :platform => :mri
6
+ else
7
+ gem 'bigdecimal', '~> 1.2.5', :platform => :mri
8
+ end
9
+
10
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Josh Lewis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,297 @@
1
+ # Unitwise-193
2
+
3
+ This is a fork of Josh W Lewis's very cool Unitwise library. I've pushed it as a gem so that I can use it in a few situations where a git source isn't possible.
4
+
5
+ Currently the only differences are that I've dropped support for ruby's pre 1.9.3 so I could upgrade dependencies as they were causing conflicts with other gems.
6
+
7
+ Primarily this upgrades parslet and nori. If you can use the stock unitwise gem I highly recommend it.
8
+
9
+ Normal readme follows.
10
+
11
+ # [Unitwise](//github.com/wizarddevelopment/unitwise)
12
+
13
+ [![Gem Version](http://img.shields.io/gem/v/unitwise-193.svg?style=flat)](https://rubygems.org/gems/unitwise)
14
+ [![Build Status](http://img.shields.io/travis/wizarddevelopment/unitwise.svg?style=flat)](https://travis-ci.org/wizarddevelopment/unitwise)
15
+ [![Dependency Status](http://img.shields.io/gemnasium/wizarddevelopment/unitwise.svg?style=flat)](https://gemnasium.com/wizarddevelopment/unitwise)
16
+ [![Coverage Status](http://img.shields.io/coveralls/wizarddevelopment/unitwise.svg?style=flat)](https://coveralls.io/r/wizarddevelopment/unitwise)
17
+ [![Code Climate](http://img.shields.io/codeclimate/github/wizarddevelopment/unitwise.svg?style=flat)](https://codeclimate.com/github/wizarddevelopment/unitwise)
18
+
19
+ Unitwise is a Ruby library for unit measurement conversion and math.
20
+
21
+ For an over the top example, consider a car (2800 lb) completing the quarter
22
+ mile in 10 seconds (with uniform acceleration).
23
+
24
+ ```ruby
25
+ distance = 0.25.mile # => #<Unitwise::Measurement value=0.25 unit=mile>
26
+ time = 10.second # => #<Unitwise::Measurement value=10 unit=second>
27
+ mass = 2800.pound # => #<Unitwise::Measurement value=2800 unit=pound>
28
+
29
+ acceleration = 2.0 * distance / time ** 2
30
+ # => #<Unitwise::Measurement value=0.005 unit=[mi_us]/s2>
31
+
32
+ force = (mass * acceleration).to_lbf
33
+ # => #<Unitwise::Measurement value=2297.5084316991147 unit=lbf>
34
+
35
+ power = (force * distance / time).to_horsepower
36
+ # => #<Unitwise::Measurement value=551.4031264140402 unit=horsepower>
37
+
38
+ speed = ((2.0 * acceleration * distance) ** 0.5).convert_to("mile/hour")
39
+ # => #<Unitwise::Measurement value=180.0 unit=mile/hour>
40
+ ```
41
+
42
+ [RubyTapas](http://rubytapas.com) subscribers can also view a screencast:
43
+ [225-Unitwise](https://rubytapas.dpdcart.com/subscriber/post?id=563)
44
+
45
+ ## Rationale
46
+
47
+ Unitwise is based on the [Unified Code for Units of Measure(UCUM)](http://unitsofmeasure.org/),
48
+ which aims to maintain a cross-platform list of units and their conversions.
49
+ This gives Unitwise a few key advantages:
50
+
51
+ - An enormous list of units. At the time of writing, there are 96 metric units,
52
+ 211 non-metric units, and 24 unit prefixes. Whatever unit/units you need, they
53
+ are here.
54
+
55
+ - An accurate and up to date set of units. The units, prefixes, and conversions
56
+ are maintained by UCUM, and are imported into this library with a rake task.
57
+
58
+ One of the objectives of Unitwise was that it should comprehend any combination
59
+ of units. For instance it needed to understand that a unit of
60
+ 'kilogram.(meter/second)2' was equivalent to 'kilogram.meter.(meter/second2)'.
61
+ This resulted in two unique features:
62
+
63
+ - An expression grammar built with a PEG parser. This makes expression
64
+ parsing more efficient and allows nested parentheses. For example, this is possible: '(kilogram.(meter/second)2)2'
65
+
66
+ - Smart compatibility detection. Each unit is reduced down to its most elementary
67
+ atoms to determine compatibility with another unit. For example, it knows that
68
+ 'meter/second2' should be considered compatible with 'kilogram.foot.minute-2/pound'.
69
+
70
+ ## Usage
71
+
72
+ ### Initialization:
73
+
74
+ Measurements can be instantiated with `Unitwise()`.
75
+
76
+ ```ruby
77
+ require 'unitwise'
78
+
79
+ Unitwise(2.3, 'kilogram') # => #<Unitwise::Measurement value=2.3 unit=kilogram>
80
+ Unitwise(100, 'pound') # => #<Unitwise::Measurement value=100 unit=pound>
81
+ ```
82
+
83
+ Unitwise doesn't mess with the core library by default. However, you can
84
+ optionally require the core extensions for some handy helpers.
85
+
86
+ ```ruby
87
+ require 'unitwise/ext'
88
+
89
+ 1.convert_to('liter') # => #<Unitwise::Measurement value=1 unit=liter>
90
+ 4.teaspoon # => #<Unitwise::Measurement value=4 unit=teaspoon>
91
+ ```
92
+
93
+ ### Conversion
94
+
95
+ Unitwise is able to convert any unit within the UCUM spec to any other
96
+ compatible unit.
97
+
98
+ ```ruby
99
+ 5.0.kilometer.convert_to('mile')
100
+ # => #<Unitwise::Measurement value=3.106849747474748 unit=mile>
101
+ ```
102
+
103
+ The prettier version of `convert_to(unit)` is appending the unit code, name, etc.
104
+ to a `to_` message name.
105
+
106
+ ```ruby
107
+ Unitwise(26.2, 'mile').to_kilometer
108
+ # => #<Unitwise::Measurement value=42.164897129794255 unit=kilometer>
109
+ ```
110
+
111
+ ### Comparison
112
+
113
+ It also has the ability to compare measurements with the same or different units.
114
+
115
+ ```ruby
116
+ 12.inch == 1.foot # => true
117
+ 1.meter > 1.yard # => true
118
+ ```
119
+
120
+ Again, you have to compare compatible units. For example, comparing two
121
+ temperatures will work, comparing a mass to a length would fail.
122
+
123
+ ### SI abbreviations
124
+
125
+ You can use shorthand for SI units.
126
+
127
+ ```ruby
128
+ 1000.m == 1.km # => true
129
+ 1.ml == 0.001.l # => true
130
+ ```
131
+
132
+ ### Complex Units
133
+
134
+ Units can be combined to make more complex ones. There is nothing special about
135
+ them -- they can still be converted, compared, or operated on.
136
+
137
+ ```ruby
138
+ speed = Unitwise(60, 'mile/hour')
139
+ # => #<Unitwise::Measurement value=60 unit=mile/hour>
140
+
141
+ speed.convert_to('m/s')
142
+ # => #<Unitwise::Measurement value=26.822453644907288 unit=m/s>
143
+ ```
144
+
145
+ Exponents and parenthesis are supported as well.
146
+
147
+ ```ruby
148
+ Unitwise(1000, 'kg.s-1.(m/s)2').to_kilowatt
149
+ # => #<Unitwise::Measurement value=1.0 unit=kilowatt>
150
+ ```
151
+
152
+ ### Math
153
+
154
+ You can add or subtract compatible measurements.
155
+
156
+ ```ruby
157
+ 2.0.meter + 3.0.inch - 1.0.yard
158
+ # => #<Unitwise::Measurement value=1.1618 unit=meter>
159
+ ```
160
+
161
+ You can multiply or divide measurements and numbers.
162
+
163
+ ```ruby
164
+ 110.volt * 2
165
+ # => #<Unitwise::Measurement value=220 unit=volt>
166
+ ```
167
+
168
+ You can multiply or divide measurements with measurements.
169
+
170
+ ```ruby
171
+ 20.milligram / 1.liter
172
+ # => #<Unitwise::Measurement value=20 unit=mg/l>
173
+
174
+ ```
175
+
176
+ Exponentiation is also supported.
177
+
178
+ ```ruby
179
+ (10.cm ** 3).to_liter
180
+ # => #<Unitwise::Measurement value=1 unit=liter>
181
+ ```
182
+
183
+ ### Unit Names and Atom Codes
184
+
185
+ This library is based around the units in the UCUM specification, which is
186
+ extensive and well thought out. However, not all of our unit systems throughout
187
+ the world and history are consistent or logical. UCUM has devised a system where
188
+ each unit has a unique atom code to try and solve this. The previous code examples
189
+ don't show this, because for the most part you won't need it. Unitwise can
190
+ figure out most of the units by their name or symbol. If you find you need to
191
+ (or just want to be explicit) you use the UCUM atom codes without any
192
+ modification.
193
+
194
+ Just for example, you can see here that there are actually a few versions of inch
195
+ and foot:
196
+
197
+ ```ruby
198
+ Unitwise(1, '[ft_i]') == Unitwise(1, '[ft_us]') # => false
199
+
200
+ Unitwise(3, '[in_br]') == Unitwise(3, '[in_i]') # => false
201
+ ```
202
+
203
+ ### Available Units
204
+
205
+ If you are looking for a particular unit, you can search with a string or
206
+ Regexp.
207
+
208
+ ```ruby
209
+ Unitwise.search('fathom')
210
+ # => [ ... ]
211
+ ```
212
+
213
+ You can also get the official list from the UCUM website in XML format at
214
+ [unitsofmeasure.org/ucum-essence.xml](http://unitsofmeasure.org/ucum-essence.xml)
215
+ or a YAML version within this repo
216
+ [github.com/joshwlewis/unitwise/tree/master/data](//github.com/joshwlewis/unitwise/tree/master/data).
217
+
218
+ ### UCUM designations
219
+ UCUM defines several designations for it's units: `names`,
220
+ `primary_code`, `secondary_code`, and `symbol`. You can see them all when
221
+ inspecting an atom:
222
+
223
+ ```ruby
224
+ Unitwise::Atom.all.sample
225
+ # => #<Unitwise::Atom names=["British thermal unit"], primary_code="[Btu]", secondary_code="[BTU]", symbol="btu", scale=#<Unitwise::Scale value=1 unit=[Btu_th]>, classification="heat", property="energy", metric=false, special=false, arbitrary=false, dim="L2.M.T-2">
226
+ ```
227
+
228
+ When initializing a measurement, you can use any of the designations:
229
+
230
+ ```ruby
231
+ Unitwise(1, '[Btu]') == Unitwise(1, 'British thermal unit')
232
+ # => true
233
+ Unitwise(1, 'btu') == Unitwise(1, "[BTU]")
234
+ # => true
235
+ ```
236
+
237
+ When inspecting or printing (`to_s`) that measurement, it will remember the
238
+ desigation you used. However, if you want to print it with another designation,
239
+ that's also possible:
240
+
241
+ ```ruby
242
+ temperature = Unitwise(10, "Cel")
243
+ temperature.to_s # => "10 Cel"
244
+ temperature.to_s(:names) # => "10 degree Celsius"
245
+ temperature.to_s(:symbol) # => "10 °C"
246
+ ```
247
+
248
+ There is on caveat here. You must use the same designation for each atom in a
249
+ complex unit. Meaning you can't mix designations within a unit.
250
+
251
+ ```ruby
252
+ Unitwise(1, "m/s") # Works, both atoms use their primary_code
253
+ Unitwise(1, "meter/second") # Works, both atoms use a name
254
+ Unitwise(1, "meter/s") # Does not work, mixed designations (name and primary_code)
255
+ Unitwise(1, "meter") / Unitwise(1, "s") # Also works
256
+ ```
257
+
258
+ ## Supported Ruby Versions
259
+
260
+ This library aims to support and is tested against the following Ruby
261
+ implementations:
262
+
263
+ * Ruby 1.9.3
264
+ * Ruby 2.0.0
265
+ * Ruby 2.1.0
266
+ * [JRuby](http://jruby.org/)
267
+ * [Rubinius](http://rubini.us/)
268
+ * [Ruby Enterprise Edition](http://www.rubyenterpriseedition.com/)
269
+
270
+ If something doesn't work on one of these versions, it's a bug.
271
+
272
+ This library may inadvertently work (or seem to work) on other Ruby versions or
273
+ implementations, however support will only be provided for the implementations
274
+ listed above.
275
+
276
+ ## Installation
277
+
278
+ Add this line to your application's Gemfile:
279
+
280
+ gem 'unitwise'
281
+
282
+ And then execute:
283
+
284
+ $ bundle
285
+
286
+ Or install it yourself as:
287
+
288
+ $ gem install unitwise
289
+
290
+
291
+ ## Contributing
292
+
293
+ 1. Fork it
294
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
295
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
296
+ 4. Push to the branch (`git push origin my-new-feature`)
297
+ 5. Create new Pull Request
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = 'test/**/*_test.rb'
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ namespace :unitwise do
13
+ desc "Update Ucum Data"
14
+ task :update_standard do
15
+ require 'unitwise'
16
+ require 'unitwise/standard'
17
+ Unitwise::Standard::BaseUnit.write
18
+ Unitwise::Standard::DerivedUnit.write
19
+ Unitwise::Standard::Prefix.write
20
+ end
21
+ end