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.
Files changed (57) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +31 -0
  3. data/CHANGELOG.md +60 -0
  4. data/README.md +68 -10
  5. data/Rakefile +22 -1
  6. data/cache/.keep +0 -0
  7. data/cache/length.json +2553 -0
  8. data/cache/volume.json +4695 -0
  9. data/cache/weight.json +3195 -0
  10. data/dev.yml +2 -6
  11. data/gemfiles/{activesupport-4.2.gemfile → activesupport-5.2.gemfile} +1 -1
  12. data/gemfiles/activesupport-6.0.gemfile +5 -0
  13. data/gemfiles/activesupport-6.1.gemfile +5 -0
  14. data/lib/measured/arithmetic.rb +10 -0
  15. data/lib/measured/base.rb +13 -6
  16. data/lib/measured/cache/json.rb +55 -0
  17. data/lib/measured/cache/json_writer.rb +9 -0
  18. data/lib/measured/cache/null.rb +16 -0
  19. data/lib/measured/conversion_table_builder.rb +110 -0
  20. data/lib/measured/cycle_detected.rb +11 -0
  21. data/lib/measured/measurable.rb +28 -21
  22. data/lib/measured/missing_conversion_path.rb +12 -0
  23. data/lib/measured/parser.rb +2 -1
  24. data/lib/measured/unit.rb +17 -27
  25. data/lib/measured/unit_already_added.rb +11 -0
  26. data/lib/measured/unit_error.rb +4 -0
  27. data/lib/measured/unit_system.rb +26 -21
  28. data/lib/measured/unit_system_builder.rb +46 -2
  29. data/lib/measured/units/length.rb +6 -3
  30. data/lib/measured/units/volume.rb +20 -0
  31. data/lib/measured/units/weight.rb +10 -2
  32. data/lib/measured/version.rb +2 -1
  33. data/lib/measured.rb +2 -0
  34. data/measured.gemspec +13 -5
  35. data/test/arithmetic_test.rb +36 -0
  36. data/test/cache/json_test.rb +43 -0
  37. data/test/cache/json_writer_test.rb +23 -0
  38. data/test/cache/null_test.rb +20 -0
  39. data/test/cache_consistency_test.rb +19 -0
  40. data/test/conversion_table_builder_test.rb +147 -0
  41. data/test/measurable_test.rb +28 -13
  42. data/test/parser_test.rb +1 -0
  43. data/test/support/always_true_cache.rb +14 -0
  44. data/test/support/fake_system.rb +1 -0
  45. data/test/support/subclasses.rb +7 -0
  46. data/test/test_helper.rb +5 -3
  47. data/test/unit_error_test.rb +1 -0
  48. data/test/unit_system_builder_test.rb +120 -2
  49. data/test/unit_system_test.rb +15 -0
  50. data/test/unit_test.rb +14 -5
  51. data/test/units/length_test.rb +123 -10
  52. data/test/units/volume_test.rb +497 -0
  53. data/test/units/weight_test.rb +290 -5
  54. metadata +49 -18
  55. data/.travis.yml +0 -17
  56. data/lib/measured/conversion_table.rb +0 -65
  57. data/test/conversion_table_test.rb +0 -98
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7c468b43dd721649e5b050c0ad7f81b17e541734
4
- data.tar.gz: d7c36b859f8e80539841a8a52457f923ca175136
2
+ SHA256:
3
+ metadata.gz: 808003a2689c157f3d3a1ef7ff2eb2f1decc242d23c40a536d9bca8c57364a79
4
+ data.tar.gz: 42f4a72969b5331d7cd460f87d5668b3b331001b82f351c221f7153488859be5
5
5
  SHA512:
6
- metadata.gz: fae18c324062e6274e0b6dab29b2ed0bcdb88012d90859edbc23fad081d1c02aba34710f54d0ecb2e7eb92a1940ef382fd5c0edaf2700ba2fc0ce422e333a3e7
7
- data.tar.gz: ef22bf6cc930d8e3835095952e9aad259e32475584a0d0537f38f459e4ab141d4713f275f1c33d35e52eeabf0c7221c6192cec137827d3938287ece2e603f1da
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 [![Build Status](https://travis-ci.org/Shopify/measured.svg)](https://travis-ci.org/Shopify/measured) [![Gem Version](https://badge.fury.io/rb/measured.svg)](http://badge.fury.io/rb/measured)
1
+ # Measured [![Build Status](https://github.com/Shopify/measured/workflows/CI/badge.svg)](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
- * kg, kilogram, kilograms
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 modules inside the `Measured` namespace, similar to `BigDecimal(123)` vs `BigDecimal.new(123)`:
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: ["1.5 bu"] # The conversion rate to another unit
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 and maintained.
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['test/**/*_test.rb']
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