measured 2.0.0 → 2.8.2
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 +5 -5
- data/.github/workflows/ci.yml +31 -0
- data/CHANGELOG.md +60 -0
- data/README.md +68 -10
- data/Rakefile +22 -1
- data/cache/.keep +0 -0
- data/cache/length.json +2553 -0
- data/cache/volume.json +4695 -0
- data/cache/weight.json +3195 -0
- data/dev.yml +2 -6
- data/gemfiles/{activesupport-4.2.gemfile → activesupport-5.2.gemfile} +1 -1
- data/gemfiles/activesupport-6.0.gemfile +5 -0
- data/gemfiles/activesupport-6.1.gemfile +5 -0
- data/lib/measured/arithmetic.rb +10 -0
- data/lib/measured/base.rb +13 -6
- data/lib/measured/cache/json.rb +55 -0
- data/lib/measured/cache/json_writer.rb +9 -0
- data/lib/measured/cache/null.rb +16 -0
- data/lib/measured/conversion_table_builder.rb +110 -0
- data/lib/measured/cycle_detected.rb +11 -0
- data/lib/measured/measurable.rb +28 -21
- data/lib/measured/missing_conversion_path.rb +12 -0
- data/lib/measured/parser.rb +2 -1
- data/lib/measured/unit.rb +17 -27
- data/lib/measured/unit_already_added.rb +11 -0
- data/lib/measured/unit_error.rb +4 -0
- data/lib/measured/unit_system.rb +26 -21
- data/lib/measured/unit_system_builder.rb +46 -2
- data/lib/measured/units/length.rb +6 -3
- data/lib/measured/units/volume.rb +20 -0
- data/lib/measured/units/weight.rb +10 -2
- data/lib/measured/version.rb +2 -1
- data/lib/measured.rb +2 -0
- data/measured.gemspec +13 -5
- data/test/arithmetic_test.rb +36 -0
- data/test/cache/json_test.rb +43 -0
- data/test/cache/json_writer_test.rb +23 -0
- data/test/cache/null_test.rb +20 -0
- data/test/cache_consistency_test.rb +19 -0
- data/test/conversion_table_builder_test.rb +147 -0
- data/test/measurable_test.rb +28 -13
- data/test/parser_test.rb +1 -0
- data/test/support/always_true_cache.rb +14 -0
- data/test/support/fake_system.rb +1 -0
- data/test/support/subclasses.rb +7 -0
- data/test/test_helper.rb +5 -3
- data/test/unit_error_test.rb +1 -0
- data/test/unit_system_builder_test.rb +120 -2
- data/test/unit_system_test.rb +15 -0
- data/test/unit_test.rb +14 -5
- data/test/units/length_test.rb +123 -10
- data/test/units/volume_test.rb +497 -0
- data/test/units/weight_test.rb +290 -5
- metadata +49 -18
- data/.travis.yml +0 -17
- data/lib/measured/conversion_table.rb +0 -65
- data/test/conversion_table_test.rb +0 -98
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 808003a2689c157f3d3a1ef7ff2eb2f1decc242d23c40a536d9bca8c57364a79
|
|
4
|
+
data.tar.gz: 42f4a72969b5331d7cd460f87d5668b3b331001b82f351c221f7153488859be5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4019bd9f128308af57f5ce9440e6a53f05587c1589f120b711c937b70d422a865c14d2e678d4b55bb13054e24a2663b5cb6ef51bfad12e834027eb4a644d5651
|
|
7
|
+
data.tar.gz: caf36d6ad0dbbd0e187adbb0d5a2ddd2718a38ddd67920e628c1d7ad09423f9c35edcef7f05cb863fec8e2849bb0eb8ce4145496aa0749cf2aaafbcd6459ba13
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
env:
|
|
9
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
ruby:
|
|
13
|
+
- '2.6'
|
|
14
|
+
- '2.7'
|
|
15
|
+
- '3.0'
|
|
16
|
+
gemfile:
|
|
17
|
+
- Gemfile
|
|
18
|
+
- gemfiles/activesupport-5.2.gemfile
|
|
19
|
+
- gemfiles/activesupport-6.0.gemfile
|
|
20
|
+
- gemfiles/activesupport-6.1.gemfile
|
|
21
|
+
name: Ruby ${{ matrix.ruby }} ${{ matrix.gemfile }}
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v1
|
|
24
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
|
25
|
+
uses: ruby/setup-ruby@v1
|
|
26
|
+
with:
|
|
27
|
+
ruby-version: ${{ matrix.ruby }}
|
|
28
|
+
bundler-cache: true
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: |
|
|
31
|
+
bundle exec rake
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Unreleased
|
|
2
|
+
-----
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
2.8.2
|
|
7
|
+
-----
|
|
8
|
+
|
|
9
|
+
* Add cm3 and mm3 to volume. (@fekadeabdejene)
|
|
10
|
+
|
|
11
|
+
2.8.1
|
|
12
|
+
-----
|
|
13
|
+
|
|
14
|
+
* Allow format without the conversion string. (@saraid)
|
|
15
|
+
* Add special case for zero coercion to allow direct use of `sum` method. (@jmortlock)
|
|
16
|
+
* Add useful numeric methods `finite?`, `infinite?`, `zero?`, `nonzero?`, `positive?` and `negative?`. (@jmortlock)
|
|
17
|
+
|
|
18
|
+
2.8.0
|
|
19
|
+
-----
|
|
20
|
+
|
|
21
|
+
* Drop support for Ruby 2.5
|
|
22
|
+
* Use Ruby 3.0.2 for development
|
|
23
|
+
|
|
24
|
+
2.7.1
|
|
25
|
+
-----
|
|
26
|
+
|
|
27
|
+
* Fix Ruby 3.0 compatibility
|
|
28
|
+
|
|
29
|
+
2.7.0
|
|
30
|
+
-----
|
|
31
|
+
|
|
32
|
+
* Raises an exception on cyclic conversions. (@arturopie)
|
|
33
|
+
* Deduplicate strings loaded from the cache.
|
|
34
|
+
* Deduplicate parsed units.
|
|
35
|
+
|
|
36
|
+
2.6.0
|
|
37
|
+
-----
|
|
38
|
+
|
|
39
|
+
* Add `Measured::MissingConversionPath` and `Measured::UnitAlreadyAdded` as subclasses of `Measured::UnitError` to handle specific error cases. (@arturopie)
|
|
40
|
+
* Support only ActiveSupport 5.2 and above.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
2.5.2
|
|
44
|
+
-----
|
|
45
|
+
|
|
46
|
+
* Allow unit values to be declared in the unit system through aliases and not just the base unit name.
|
|
47
|
+
* Fix some deprecations in tests and CI.
|
|
48
|
+
|
|
49
|
+
2.5.1
|
|
50
|
+
----
|
|
51
|
+
|
|
52
|
+
* Get rid of most memoizations in favor of eager computations.
|
|
53
|
+
|
|
54
|
+
2.5.0
|
|
55
|
+
-----
|
|
56
|
+
|
|
57
|
+
* Add `CHANGELOG.md`.
|
|
58
|
+
* Fix some deprecations and warnings.
|
|
59
|
+
* Support Rails 6 and Ruby 2.6.
|
|
60
|
+
* Cache conversion table in JSON file for first load performance.
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Measured [](https://github.com/Shopify/measured/actions?query=workflow%3ACI)
|
|
2
2
|
|
|
3
|
-
Encapsulates measurements with their units. Provides easy conversion between units.
|
|
3
|
+
Encapsulates measurements with their units. Provides easy conversion between units. Built in support for weight, length, and volume.
|
|
4
4
|
|
|
5
5
|
Lightweight and easily extensible to include other units and conversions. Conversions done with `Rational` for precision.
|
|
6
6
|
|
|
@@ -141,22 +141,78 @@ Measured::Weight.unit_names_with_aliases
|
|
|
141
141
|
> ["g", "gram", "grams", "kg", "kilogram", "kilograms", "lb", "lbs", "ounce", "ounces", "oz", "pound", "pounds"]
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
+
String formatting:
|
|
145
|
+
```ruby
|
|
146
|
+
Measured::Weight.new("3.14", "grams").format("%.1<value>f %<unit>s")
|
|
147
|
+
> "3.1 g"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If no string is passed to the `format` method it defaults to `"%.2<value>f %<unit>s"`.
|
|
151
|
+
|
|
152
|
+
If the unit isn't the standard SI unit, it will include a conversion string.
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
Measured::Weight.new("3.14", "kg").format
|
|
156
|
+
> "3.14 kg (1000/1 g)"
|
|
157
|
+
Measured::Weight.new("3.14", "kg").format(with_conversion_string: false)
|
|
158
|
+
> "3.14 kg"
|
|
159
|
+
```
|
|
160
|
+
|
|
144
161
|
## Units and conversions
|
|
145
162
|
|
|
163
|
+
### SI units support
|
|
164
|
+
|
|
165
|
+
There is support for SI units through the use of `si_unit`. Units declared through it will have automatic support for all SI prefixes:
|
|
166
|
+
|
|
167
|
+
| Multiplying Factor | SI Prefix | Scientific Notation |
|
|
168
|
+
| --------------------------------- | --------- | --------------------- |
|
|
169
|
+
| 1 000 000 000 000 000 000 000 000 | yotta (Y) | 10^24 |
|
|
170
|
+
| 1 000 000 000 000 000 000 000 | zetta (Z) | 10^21 |
|
|
171
|
+
| 1 000 000 000 000 000 000 | exa (E) | 10^18 |
|
|
172
|
+
| 1 000 000 000 000 000 | peta (P) | 10^15 |
|
|
173
|
+
| 1 000 000 000 000 | tera (T) | 10^12 |
|
|
174
|
+
| 1 000 000 000 | giga (G) | 10^9 |
|
|
175
|
+
| 1 000 000 | mega (M) | 10^6 |
|
|
176
|
+
| 1 000 | kilo (k) | 10^3 |
|
|
177
|
+
| 0.001 | milli (m) | 10^-3 |
|
|
178
|
+
| 0.000 001 | micro (µ) | 10^-6 |
|
|
179
|
+
| 0.000 000 001 | nano (n) | 10^-9 |
|
|
180
|
+
| 0.000 000 000 001 | pico (p) | 10^-12 |
|
|
181
|
+
| 0.000 000 000 000 001 | femto (f) | 10^-15 |
|
|
182
|
+
| 0.000 000 000 000 000 001 | atto (a) | 10^-18 |
|
|
183
|
+
| 0.000 000 000 000 000 000 001 | zepto (z) | 10^-21 |
|
|
184
|
+
| 0.000 000 000 000 000 000 000 001 | yocto (y) | 10^-24 |
|
|
185
|
+
|
|
146
186
|
### Bundled unit conversion
|
|
147
187
|
|
|
148
188
|
* `Measured::Weight`
|
|
149
|
-
* g, gram, grams
|
|
150
|
-
*
|
|
189
|
+
* g, gram, grams, and all SI prefixes
|
|
190
|
+
* t, metric_ton, metric_tons
|
|
191
|
+
* slug, slugs
|
|
192
|
+
* N, newtons, newton
|
|
193
|
+
* long_ton, long_tons, weight_ton, weight_tons, 'W/T', imperial_ton, imperial_tons, displacement_ton, displacement_tons
|
|
194
|
+
* short_ton, short_tons
|
|
151
195
|
* lb, lbs, pound, pounds
|
|
152
196
|
* oz, ounce, ounces
|
|
153
197
|
* `Measured::Length`
|
|
154
|
-
* m, meter, metre, meters, metres
|
|
155
|
-
* cm, centimeter, centimetre, centimeters, centimetres
|
|
156
|
-
* mm, millimeter, millimetre, millimeters, millimetres
|
|
198
|
+
* m, meter, metre, meters, metres, and all SI prefixes
|
|
157
199
|
* in, inch, inches
|
|
158
200
|
* ft, foot, feet
|
|
159
201
|
* yd, yard, yards
|
|
202
|
+
* mi, mile, miles
|
|
203
|
+
* `Measured::Volume`
|
|
204
|
+
* l, liter, litre, liters, litres, and all SI prefixes
|
|
205
|
+
* m3, cubic_meter, cubic_meters, cubic_metre, cubic_metres
|
|
206
|
+
* ft3, cubic_foot, cubic_feet
|
|
207
|
+
* in3, cubic_inch, cubic_inches
|
|
208
|
+
* gal, imp_gal, imperial_gallon, imp_gals, imperial_gallons
|
|
209
|
+
* us_gal, us_gallon, us_gals, us_gallons
|
|
210
|
+
* qt, imp_qt, imperial_quart, imp_qts, imperial_quarts
|
|
211
|
+
* us_qt, us_quart, us_quarts
|
|
212
|
+
* pt, imp_pt, imperial_pint, imp_pts, imperial_pints
|
|
213
|
+
* us_pt, us_pint, us_pints
|
|
214
|
+
* oz, fl_oz, imp_fl_oz, imperial_fluid_ounce, imperial_fluid_ounces
|
|
215
|
+
* us_oz, us_fl_oz, us_fluid_ounce, us_fluid_ounces
|
|
160
216
|
|
|
161
217
|
You can skip these and only define your own units by doing:
|
|
162
218
|
|
|
@@ -166,7 +222,7 @@ gem 'measured', require: 'measured/base'
|
|
|
166
222
|
|
|
167
223
|
### Shortcut syntax
|
|
168
224
|
|
|
169
|
-
There is a shortcut initialization syntax for
|
|
225
|
+
There is a shortcut initialization syntax for creating instances of measurement classes that can avoid the `.new`:
|
|
170
226
|
|
|
171
227
|
```ruby
|
|
172
228
|
Measured::Weight(1, :g)
|
|
@@ -184,7 +240,7 @@ Measured::Thing = Measured.build do
|
|
|
184
240
|
|
|
185
241
|
unit :another_unit, # Add a second unit to the system
|
|
186
242
|
aliases: [:au], # All units allow aliases, as long as they are unique
|
|
187
|
-
value:
|
|
243
|
+
value: "1.5 bu" # The conversion rate to another unit
|
|
188
244
|
end
|
|
189
245
|
```
|
|
190
246
|
|
|
@@ -199,6 +255,7 @@ All units and classes are namespaced by default, but can be aliased in your appl
|
|
|
199
255
|
```ruby
|
|
200
256
|
Weight = Measured::Weight
|
|
201
257
|
Length = Measured::Length
|
|
258
|
+
Volume = Measured::Volume
|
|
202
259
|
```
|
|
203
260
|
|
|
204
261
|
## Alternatives
|
|
@@ -225,12 +282,13 @@ Existing alternatives which were considered:
|
|
|
225
282
|
|
|
226
283
|
### Gem: [unitwise](https://github.com/joshwlewis/unitwise)
|
|
227
284
|
* **Pros**
|
|
228
|
-
* Well written
|
|
285
|
+
* Well written.
|
|
229
286
|
* Conversions done with Unified Code for Units of Measure (UCUM) so highly accurate and reliable.
|
|
230
287
|
* **Cons**
|
|
231
288
|
* Lots of code. Good code, but lots of it.
|
|
232
289
|
* Many modifications to core types.
|
|
233
290
|
* ActiveRecord adapter exists but is written and maintained by a different person/org.
|
|
291
|
+
* Not actively maintained.
|
|
234
292
|
|
|
235
293
|
## Contributing
|
|
236
294
|
|
data/Rakefile
CHANGED
|
@@ -8,9 +8,12 @@ task default: :test
|
|
|
8
8
|
|
|
9
9
|
desc 'Run the test stuite'
|
|
10
10
|
Rake::TestTask.new do |t|
|
|
11
|
+
files = ARGV[1..-1]
|
|
12
|
+
files = "test/**/*_test.rb" if !files || files.length == 0
|
|
13
|
+
|
|
11
14
|
t.libs << "test"
|
|
12
15
|
t.libs << "lib/**/*"
|
|
13
|
-
t.test_files = FileList[
|
|
16
|
+
t.test_files = FileList[files]
|
|
14
17
|
t.verbose = true
|
|
15
18
|
end
|
|
16
19
|
|
|
@@ -19,3 +22,21 @@ task tag: :build do
|
|
|
19
22
|
system "git tag -a v#{ Measured::VERSION } -m 'Tagging #{ Measured::VERSION }'"
|
|
20
23
|
system "git push --tags"
|
|
21
24
|
end
|
|
25
|
+
|
|
26
|
+
task :environment do
|
|
27
|
+
require 'measured'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
namespace :cache do
|
|
31
|
+
task write: :environment do
|
|
32
|
+
class Measured::Cache::Json
|
|
33
|
+
prepend Measured::Cache::JsonWriter
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
puts "Updating cache files:"
|
|
37
|
+
|
|
38
|
+
Measured::Measurable.subclasses.each do |measurable|
|
|
39
|
+
puts " #{measurable} - #{measurable.unit_system.update_cache ? 'ok' : 'skipped'}"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/cache/.keep
ADDED
|
File without changes
|