unit_measurements_us_complete 5.17.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.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/main.yml +31 -0
  3. data/.github/workflows/pages.yml +31 -0
  4. data/.gitignore +12 -0
  5. data/.rspec +3 -0
  6. data/.yardopts +2 -0
  7. data/CHANGELOG.md +645 -0
  8. data/Gemfile +7 -0
  9. data/Gemfile.lock +56 -0
  10. data/LICENSE.md +21 -0
  11. data/README.md +511 -0
  12. data/Rakefile +10 -0
  13. data/lib/unit_measurements/arithmetic.rb +225 -0
  14. data/lib/unit_measurements/base.rb +177 -0
  15. data/lib/unit_measurements/cache.rb +173 -0
  16. data/lib/unit_measurements/comparison.rb +64 -0
  17. data/lib/unit_measurements/configuration.rb +64 -0
  18. data/lib/unit_measurements/conversion.rb +92 -0
  19. data/lib/unit_measurements/errors/blank_quantity_error.rb +21 -0
  20. data/lib/unit_measurements/errors/blank_unit_error.rb +21 -0
  21. data/lib/unit_measurements/errors/missing_primitive_unit_error.rb +24 -0
  22. data/lib/unit_measurements/errors/parse_error.rb +37 -0
  23. data/lib/unit_measurements/errors/primitive_unit_already_set_error.rb +22 -0
  24. data/lib/unit_measurements/errors/unit_already_defined_error.rb +37 -0
  25. data/lib/unit_measurements/errors/unit_error.rb +36 -0
  26. data/lib/unit_measurements/extras/conversion_methods.rb +87 -0
  27. data/lib/unit_measurements/extras/numeric_methods.rb +85 -0
  28. data/lib/unit_measurements/formatter.rb +58 -0
  29. data/lib/unit_measurements/math.rb +120 -0
  30. data/lib/unit_measurements/measurement.rb +539 -0
  31. data/lib/unit_measurements/normalizer.rb +155 -0
  32. data/lib/unit_measurements/parser.rb +220 -0
  33. data/lib/unit_measurements/unit.rb +243 -0
  34. data/lib/unit_measurements/unit_group.rb +297 -0
  35. data/lib/unit_measurements/unit_group_builder.rb +224 -0
  36. data/lib/unit_measurements/unit_groups/acceleration.rb +36 -0
  37. data/lib/unit_measurements/unit_groups/all.rb +56 -0
  38. data/lib/unit_measurements/unit_groups/amount_of_substance.rb +13 -0
  39. data/lib/unit_measurements/unit_groups/angular_acceleration.rb +14 -0
  40. data/lib/unit_measurements/unit_groups/angular_velocity.rb +25 -0
  41. data/lib/unit_measurements/unit_groups/area.rb +49 -0
  42. data/lib/unit_measurements/unit_groups/catalytic_activity.rb +13 -0
  43. data/lib/unit_measurements/unit_groups/density.rb +33 -0
  44. data/lib/unit_measurements/unit_groups/dynamic_viscosity.rb +22 -0
  45. data/lib/unit_measurements/unit_groups/electric_charge.rb +20 -0
  46. data/lib/unit_measurements/unit_groups/electric_conductance.rb +15 -0
  47. data/lib/unit_measurements/unit_groups/electric_current.rb +19 -0
  48. data/lib/unit_measurements/unit_groups/electric_dipole_moment.rb +13 -0
  49. data/lib/unit_measurements/unit_groups/electric_potential.rb +17 -0
  50. data/lib/unit_measurements/unit_groups/electric_quadrupole_moment.rb +14 -0
  51. data/lib/unit_measurements/unit_groups/electrical_capacitance.rb +15 -0
  52. data/lib/unit_measurements/unit_groups/electrical_elastance.rb +13 -0
  53. data/lib/unit_measurements/unit_groups/electrical_inductance.rb +15 -0
  54. data/lib/unit_measurements/unit_groups/electrical_resistance.rb +16 -0
  55. data/lib/unit_measurements/unit_groups/energy.rb +58 -0
  56. data/lib/unit_measurements/unit_groups/force.rb +47 -0
  57. data/lib/unit_measurements/unit_groups/frequency.rb +16 -0
  58. data/lib/unit_measurements/unit_groups/illuminance.rb +18 -0
  59. data/lib/unit_measurements/unit_groups/information_entropy.rb +15 -0
  60. data/lib/unit_measurements/unit_groups/kinetic_viscosity.rb +18 -0
  61. data/lib/unit_measurements/unit_groups/length.rb +67 -0
  62. data/lib/unit_measurements/unit_groups/luminance.rb +21 -0
  63. data/lib/unit_measurements/unit_groups/luminous_flux.rb +11 -0
  64. data/lib/unit_measurements/unit_groups/luminous_intensity.rb +13 -0
  65. data/lib/unit_measurements/unit_groups/magnetic_field.rb +13 -0
  66. data/lib/unit_measurements/unit_groups/magnetic_flux.rb +15 -0
  67. data/lib/unit_measurements/unit_groups/magnetic_induction.rb +13 -0
  68. data/lib/unit_measurements/unit_groups/magnetomotive_force.rb +13 -0
  69. data/lib/unit_measurements/unit_groups/mass_flow_rate.rb +49 -0
  70. data/lib/unit_measurements/unit_groups/plane_angle.rb +30 -0
  71. data/lib/unit_measurements/unit_groups/power.rb +54 -0
  72. data/lib/unit_measurements/unit_groups/pressure.rb +60 -0
  73. data/lib/unit_measurements/unit_groups/quantity.rb +14 -0
  74. data/lib/unit_measurements/unit_groups/radiation_absorbed_dose.rb +14 -0
  75. data/lib/unit_measurements/unit_groups/radiation_equivalent_dose.rb +13 -0
  76. data/lib/unit_measurements/unit_groups/radiation_exposure.rb +15 -0
  77. data/lib/unit_measurements/unit_groups/radioactivity.rb +14 -0
  78. data/lib/unit_measurements/unit_groups/solid_angle.rb +18 -0
  79. data/lib/unit_measurements/unit_groups/sound_level.rb +13 -0
  80. data/lib/unit_measurements/unit_groups/temperature.rb +19 -0
  81. data/lib/unit_measurements/unit_groups/time.rb +29 -0
  82. data/lib/unit_measurements/unit_groups/torque.rb +40 -0
  83. data/lib/unit_measurements/unit_groups/velocity.rb +37 -0
  84. data/lib/unit_measurements/unit_groups/volume.rb +67 -0
  85. data/lib/unit_measurements/unit_groups/volumetric_flow_rate.rb +35 -0
  86. data/lib/unit_measurements/unit_groups/weight.rb +55 -0
  87. data/lib/unit_measurements/version.rb +8 -0
  88. data/lib/unit_measurements.rb +7 -0
  89. data/unit_measurements.gemspec +43 -0
  90. data/units.md +843 -0
  91. metadata +216 -0
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ **Copyright (c) 2023 Harshal LADHE**
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,511 @@
1
+ # Unit Measurements
2
+
3
+ A library that encapsulate measurements and their units in Ruby.
4
+
5
+ [![Ruby](https://github.com/shivam091/unit_measurements/actions/workflows/main.yml/badge.svg)](https://github.com/shivam091/unit_measurements/actions/workflows/main.yml)
6
+ [![Gem Version](https://badge.fury.io/rb/unit_measurements.svg)](https://badge.fury.io/rb/unit_measurements)
7
+ [![Gem Downloads](https://img.shields.io/gem/dt/unit_measurements.svg)](http://rubygems.org/gems/unit_measurements)
8
+ [![Maintainability](https://api.codeclimate.com/v1/badges/b8aec9bffa356d108784/maintainability)](https://codeclimate.com/github/shivam091/unit_measurements/maintainability)
9
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/b8aec9bffa356d108784/test_coverage)](https://codeclimate.com/github/shivam091/unit_measurements/test_coverage)
10
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/shivam091/unit_measurements/blob/main/LICENSE.md)
11
+
12
+ **[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**
13
+
14
+ ## Introduction
15
+
16
+ Many technical applications need use of specialized calculations at some point of time.
17
+ Frequently, these calculations require unit conversions to ensure accurate
18
+ results. Needless to say, this is a pain to properly keep track of, and is prone
19
+ to numerous errors.
20
+
21
+ ## Solution
22
+
23
+ The `unit_measurements` gem is designed to simplify the handling of units for scientific calculations.
24
+
25
+ ## Key Features
26
+
27
+ 1. **Flexible Measurement Conversion:** Easily convert measurements between compatible units, reducing the likelihood of errors in scientific calculations.
28
+ 2. **Extensible Unit Groups:** Effortlessly build own unit groups with specific units and conversions tailored to your needs.
29
+ 3. **Built-in Unit Groups:** Comes bundled with a wide range of standard [unit groups](https://github.com/shivam091/unit_measurements/blob/main/units.md),
30
+ covering various units.
31
+ 4. **String Parsing Capabilities:** Effortlessly parse strings representing complex, fractional, mixed fractional, scientific numbers, and ratios directly
32
+ saving you the hassle of manually extracting and converting them.
33
+ 5. **Comprehensive Documentation:** Well-organized and descriptive [documentation](https://shivam091.github.io/unit_measurements) for quick reference and implementation guidance.
34
+ 6. **Caching Support:** Option to cache conversion factors for improved performance.
35
+ 7. **Configurable Options:** Fine-tune behavior with configurable options, including caching for enhanced performance.
36
+ 8. **Error Handling:** Robust error handling ensures stability and reliability during conversions.
37
+
38
+ ## Disclaimer
39
+
40
+ _The unit conversions provided here are for reference and general informational
41
+ purposes. While we aim for accuracy, we cannot guarantee precision in all scenarios.
42
+ Users are advised to cross-verify conversions for their specific use cases._
43
+
44
+ ## Minimum Requirements
45
+
46
+ * Ruby 3.2.2+ ([Download Ruby](https://www.ruby-lang.org/en/downloads/branches/))
47
+
48
+ ## Installation
49
+
50
+ To use `unit_measurements` in your Rails application, add the following line to your Gemfile:
51
+
52
+ ```ruby
53
+ gem "unit_measurements"
54
+ ```
55
+
56
+ And then execute:
57
+
58
+ `$ bundle install`
59
+
60
+ Or otherwise simply install it yourself as:
61
+
62
+ `$ gem install unit_measurements`
63
+
64
+ ## Configuration
65
+
66
+ `unit_measurements` is designed to work out of the box, but you can customize its behavior by placing
67
+ the configuration block in an initializer file before requiring the library files:
68
+
69
+ ```ruby
70
+ UnitMeasurements.configure do |config|
71
+ config.use_cache = false
72
+ end
73
+ ```
74
+
75
+ The current available configurable options are:
76
+
77
+ | Option | Default value | Description |
78
+ | ------ | ------------- | ----------- |
79
+ | **use_cache** | false | Set to `true` to enable caching during conversions. |
80
+
81
+ ## Usage
82
+
83
+ The **`UnitMeasurements::Measurement`** class is responsible for conversion of quantity to various compatible units
84
+ but it can't be directly initialized or converted to other units, but rather it is done with the unit group classes
85
+ viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
86
+
87
+ **Initialize a measurement:**
88
+
89
+ ```ruby
90
+ UnitMeasurements::Length.new(1, "km") #=> 1 km
91
+ ```
92
+
93
+ **Converting to other units:**
94
+
95
+ This gem allows you to convert among units of same unit group. You can convert measurement to other unit using `#convert_to`
96
+ (aliased as `#to`, `#in`, and `#as`) or `#convert_to!` (aliased as `#to!`, `#in!`, and `#as!`) methods.
97
+ You can also chain call of `#convert_to` and `#convert_to!` methods.
98
+
99
+ These methods provide `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
100
+
101
+ You can use `#convert_to` as:
102
+
103
+ ```ruby
104
+ UnitMeasurements::Length.new(1, "km").convert_to("m", use_cache: true) #=> 1000.0 m
105
+ ```
106
+
107
+ If you want to modify measurement object itself, you can use `#convert_to!` method as:
108
+
109
+ ```ruby
110
+ UnitMeasurements::Length.new(1, "km").convert_to!("m") #=> 1000.0 m
111
+ ```
112
+
113
+ You can convert the measurement directly to the `primitive` unit of the unit group as:
114
+
115
+ ```ruby
116
+ UnitMeasurements::Length.new(1, "cm").to_primitive #=> 0.01 m
117
+ ```
118
+
119
+ Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
120
+
121
+ **Parse string without having to split out the quantity and source unit:**
122
+
123
+ This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
124
+
125
+ ```ruby
126
+ UnitMeasurements::Length.parse("1 km") #=> 1.0 km
127
+ ```
128
+
129
+ **Parse string that mentions quantity, source unit, and target unit:**
130
+
131
+ A source unit can be separated from the target unit using the `in`, `to`, or `as` operators.
132
+
133
+ ```ruby
134
+ UnitMeasurements::Length.parse("1 km to m") #=> 1000.0 m
135
+ ```
136
+
137
+ **Parse scientific numbers, source unit, and (or) target unit:**
138
+
139
+ ```ruby
140
+ UnitMeasurements::Length.parse("2e+2 km").convert_to("m") #=> 200000.0 m
141
+ UnitMeasurements::Length.parse("2e+2 km to m") #=> 200000.0 m
142
+ ```
143
+ You can check supported special characters for exponents
144
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).
145
+
146
+ **Parse complex numbers, source unit, and (or) target unit:**
147
+
148
+ ```ruby
149
+ UnitMeasurements::Length.parse("2+3i km").convert_to("m") #=> 2000.0+3000.0i m
150
+ UnitMeasurements::Length.parse("2+3i km to m") #=> 2000.0+3000.0i m
151
+ ```
152
+
153
+ **Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**
154
+
155
+ ```ruby
156
+ UnitMeasurements::Length.parse("2 ½ km").convert_to("m") #=> 2500.0 m
157
+ UnitMeasurements::Length.parse("2/3 km to m") #=> 666.666666666667 m
158
+ ```
159
+
160
+ You can check supported special characters for fractional notations
161
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).
162
+
163
+ **Parse ratios, source unit, and (or) target unit:**
164
+
165
+ ```ruby
166
+ UnitMeasurements::Length.parse("1:2 km").convert_to("m") #=> 500.0 m
167
+ UnitMeasurements::Length.parse("1:2 km to m") #=> 500.0 m
168
+ ```
169
+
170
+ **Calculate the ratio between two units:**
171
+
172
+ ```ruby
173
+ UnitMeasurements::Length.ratio("in", "ft") #=> "12.0 in/ft"
174
+ ```
175
+
176
+ **Formatting measurement:**
177
+
178
+ If you want to format the measurement to certain format, you can use `#to_fs` method.
179
+ If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.
180
+
181
+ ```ruby
182
+ UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s") #=> "3937.0079 in"
183
+ ```
184
+
185
+ You can check more about formatting along with their examples
186
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Formatter.html).
187
+
188
+ **Extract the quantity and the unit from measurement:**
189
+
190
+ ```ruby
191
+ length = UnitMeasurements::Length.new(1, "km")
192
+ length.quantity #=> 1
193
+ length.unit #=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
194
+ ```
195
+
196
+ Unit object can be interrogated for a range of attributes:
197
+
198
+ ```ruby
199
+ # Alternative names for the unit.
200
+ length.unit.aliases #=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
201
+ # Conversion factor relative to primitive unit.
202
+ length.unit.conversion_factor #=> 1000.0
203
+ ```
204
+
205
+ **See primitive unit of the unit group:**
206
+
207
+ ```ruby
208
+ UnitMeasurements::Length.primitive #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
209
+ ```
210
+
211
+ **See all units of the unit group:**
212
+
213
+ ```ruby
214
+ UnitMeasurements::Length.units
215
+ #=> [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ..., ...]
216
+ ```
217
+
218
+ **See names of all valid units of the unit group:**
219
+
220
+ ```ruby
221
+ UnitMeasurements::Length.unit_names #=> ["ft", "in", "m", "mi", "yd"]
222
+ ```
223
+
224
+ **See all valid units of the unit group along with their aliases:**
225
+
226
+ ```ruby
227
+ UnitMeasurements::Length.unit_names_with_aliases
228
+ #=> ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
229
+ ```
230
+
231
+ **See list of unit systems defined within the unit group:**
232
+
233
+ ```ruby
234
+ UnitMeasurements::Length.systems #=> ["metric", "imperial", "us_customary", "astronomical"]
235
+ ```
236
+
237
+ **See list of units within the unit system:**
238
+
239
+ You can use `#units_for` or `#units_for!` methods to find all the units defined
240
+ within the unit system. `#units_for!` method returns a runtime error if the unit
241
+ system is not defined within the unit group.
242
+
243
+ ```ruby
244
+ UnitMeasurements::Length.units_for("metric")
245
+ #=> [#<UnitMeasurements::Unit: m (meter, meters, metre, metres)>, ...]
246
+ ```
247
+
248
+ **Finding units within the unit group:**
249
+
250
+ You can use `#unit_for` or `#unit_for!` (aliased as `#[]`) methods to find units
251
+ within the unit group. `#unit_for!` method returns an error if the unit is not
252
+ defined within the unit group.
253
+
254
+ ```ruby
255
+ UnitMeasurements::Length.unit_for("m") #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
256
+ UnitMeasurements::Length.unit_for("z") #=> nil
257
+ UnitMeasurements::Length.unit_for!("z") #=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
258
+ ```
259
+
260
+ **Finding whether the unit is defined within the unit group:**
261
+
262
+ ```ruby
263
+ UnitMeasurements::Length.defined?("m") #=> true
264
+ UnitMeasurements::Length.defined?("metre") #=> false
265
+ ```
266
+
267
+ **Check if the unit is a valid unit or alias within the unit group:**
268
+
269
+ ```ruby
270
+ UnitMeasurements::Length.unit_or_alias?("m") #=> true
271
+ UnitMeasurements::Length.unit_or_alias?("metre") #=> true
272
+ ```
273
+
274
+ **Clear cached data for the unit group:**
275
+
276
+ ```ruby
277
+ UnitMeasurements::Length.clear_cache
278
+ ```
279
+
280
+ ### Comparisons
281
+
282
+ You have ability to compare the measurements with the same or different units within the same unit group.
283
+ For example, comparing length with length will work, comparing a length with a area would fail.
284
+
285
+ ```ruby
286
+ UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m") #=> true
287
+ ```
288
+
289
+ You can check supported comparisons along with their examples
290
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Comparison.html).
291
+
292
+ ### Arithmetic
293
+
294
+ You have ability to perform arithmetic operations on measurements with the same or
295
+ different units within a same unit group. You can perform arithmetic operations on
296
+ measurement by either other measurement with compatible unit or numeric value.
297
+ In cases of different units, the left hand side takes precedence.
298
+
299
+ ```ruby
300
+ UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m") #=> 1.001 km
301
+ UnitMeasurements::Length.new(2, "km") * 2+2i #=> 4+2i km
302
+ ```
303
+
304
+ You can check supported arithmetic operations along with their examples
305
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Arithmetic.html).
306
+
307
+ ### Math
308
+
309
+ You can perform mathematical functions on the measurements.
310
+
311
+ ```ruby
312
+ UnitMeasurements::Length.new(17.625, "m").round #=> 18 m
313
+ ```
314
+
315
+ You can check supported mathematical functions along with their examples
316
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Math.html).
317
+
318
+ ### Numeric conversions
319
+
320
+ You can convert measurement quantity directly to other numeric types viz.
321
+ `Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
322
+
323
+ ```ruby
324
+ UnitMeasurements::Length.new(2.25567, "km").to_i #=> 2 km
325
+ ```
326
+
327
+ You can check more about them along with their examples
328
+ [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Conversion.html).
329
+
330
+ ## Units
331
+
332
+ The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
333
+
334
+ ### SI prefixed units
335
+
336
+ Support for SI prefixed units is provided through the `si_unit` method. Units
337
+ declared this way automatically support all decimal SI prefixes. This method takes
338
+ an optional `add_binary_prefixes` parameter, which can be set to `true` if the
339
+ unit supports binary SI prefixes in addition to decimal SI prefixes.
340
+
341
+ #### Decimal SI prefixes
342
+
343
+ | Multiplying Factor | SI Prefix | Scientific Notation |
344
+ | ----------------------------------------- | ---------- | ------------------- |
345
+ | 1 000 000 000 000 000 000 000 000 000 000 | quetta (Q) | 10^30 |
346
+ | 1 000 000 000 000 000 000 000 000 000 | ronna (R) | 10^27 |
347
+ | 1 000 000 000 000 000 000 000 000 | yotta (Y) | 10^24 |
348
+ | 1 000 000 000 000 000 000 000 | zetta (Z) | 10^21 |
349
+ | 1 000 000 000 000 000 000 | exa (E) | 10^18 |
350
+ | 1 000 000 000 000 000 | peta (P) | 10^15 |
351
+ | 1 000 000 000 000 | tera (T) | 10^12 |
352
+ | 1 000 000 000 | giga (G) | 10^9 |
353
+ | 1 000 000 | mega (M) | 10^6 |
354
+ | 1 000 | kilo (k) | 10^3 |
355
+ | 1 00 | hecto (h) | 10^2 |
356
+ | 1 0 | deca (da) | 10^1 |
357
+ | 0.1 | deci (d) | 10^-1 |
358
+ | 0.01 | centi (c) | 10^-2 |
359
+ | 0.001 | milli (m) | 10^-3 |
360
+ | 0.000 001 | micro (µ) | 10^-6 |
361
+ | 0.000 000 001 | nano (n) | 10^-9 |
362
+ | 0.000 000 000 001 | pico (p) | 10^-12 |
363
+ | 0.000 000 000 000 001 | femto (f) | 10^-15 |
364
+ | 0.000 000 000 000 000 001 | atto (a) | 10^-18 |
365
+ | 0.000 000 000 000 000 000 001 | zepto (z) | 10^-21 |
366
+ | 0.000 000 000 000 000 000 000 001 | yocto (y) | 10^-24 |
367
+ | 0.000 000 000 000 000 000 000 000 001 | ronto (r) | 10^-27 |
368
+ | 0.000 000 000 000 000 000 000 000 000 001 | quecto (q) | 10^-30 |
369
+
370
+ #### Binary SI prefixes
371
+
372
+ | Multiplying Factor | SI Prefix | Scientific Notation |
373
+ | --------------------------------- | --------- | ------------------- |
374
+ | 1 024 | kibi (Ki) | 2^10 |
375
+ | 1 048 576 | mebi (Mi) | 2^20 |
376
+ | 1 073 741 824 | gibi (Gi) | 2^30 |
377
+ | 1 099 511 627 776 | tebi (Ti) | 2^40 |
378
+ | 1 125 899 906 842 624 | pebi (Pi) | 2^50 |
379
+ | 1 152 921 504 606 846 976 | exbi (Ei) | 2^60 |
380
+ | 1 180 591 620 717 411 303 424 | zebi (Zi) | 2^70 |
381
+ | 1 208 925 819 614 629 174 706 176 | yobi (Yi) | 2^80 |
382
+
383
+ ### Bundled units
384
+
385
+ There are tons of units that are bundled in `unit_measurements`. You can check them out
386
+ [here](https://github.com/shivam091/unit_measurements/blob/main/units.md).
387
+
388
+ ### Specifing units
389
+
390
+ By default, `unit_measurements` includes all unit groups automatically when you require the gem using:
391
+
392
+ ```ruby
393
+ require "unit_measurements"
394
+ ```
395
+
396
+ **You can skip these unit groups and only [build your own unit groups](#building-new-unit-groups) by doing:**
397
+
398
+ ```ruby
399
+ gem "unit_measurements", require: "unit_measurements/base"
400
+ ```
401
+
402
+ **You can also use unit groups in your application as per your need as:**
403
+
404
+ ```ruby
405
+ gem "unit_measurements", require: ["unit_measurements/base", "unit_measurements/unit_groups/length"]
406
+ ```
407
+
408
+ ### Building new unit groups
409
+
410
+ This library provides a simpler way to define your own unit groups. Use the `UnitMeasurements.build` method to define
411
+ units within it. You can group units by the unit system using the `system` method and set the primitive unit for the
412
+ unit group using the `primitive` method. You can specify cache file name in unit group definition using the `cache` method.
413
+
414
+ ```ruby
415
+ UnitMeasurements::MyUnitGroup = UnitMeasurements.build do
416
+ # Set primitive unit for the unit group.
417
+ primitive "s"
418
+
419
+ # Group units by the unit system (optional).
420
+ system :metric do
421
+ # This will add unit `m` along with all decimal SI prefixes.
422
+ si_unit "s", aliases: ["second", "seconds"]
423
+
424
+ # This will add unit `B` along with all binary & decimal SI prefixes.
425
+ si_unit "B", aliases: ["byte", "bytes"], add_binary_prefixes: true
426
+
427
+ # Add units to the group, along with their conversion multipliers.
428
+ unit "min", value: "60 s", aliases: ["hour", "hours"]
429
+
430
+ # You can also specify unit value as an array.
431
+ unit "h", value: [60, "min"], aliases: ["day", "days"]
432
+ end
433
+
434
+ # Sets the name of the cache file (optional).
435
+ cache "my_units_cache.json"
436
+ end
437
+ ```
438
+
439
+ All units allow aliases, as long as they are unique. Unit name can be used to
440
+ define the unit as long as it is unique. All unit names are case sensitive.
441
+
442
+ ### Namespaces
443
+
444
+ All unit groups and their definition classes are namespaced by default, but can be aliased in your application.
445
+
446
+ ```ruby
447
+ Weight = UnitMeasurements::Weight
448
+ Length = UnitMeasurements::Length
449
+ Volume = UnitMeasurements::Volume
450
+ ```
451
+
452
+ ## Extras
453
+
454
+ ### Numeric methods
455
+
456
+ The `.define_numeric_methods` method allows you to define numeric methods that help you to initialize measurements in a
457
+ manner similar to how `ActiveSupport::Duration` objects are created in Rails, providing a familiar syntax and functionality.
458
+
459
+ To define numeric methods for specific units within the unit group, use
460
+ the following syntax:
461
+
462
+ ```ruby
463
+ UnitMeasurements::Length.define_numeric_methods("metre", "foot", "inch")
464
+ ```
465
+
466
+ This will enable the usage of these units as methods to instantiate and use measurements:
467
+
468
+ ```ruby
469
+ # Initialize a measurement
470
+ 1.m #=> 1 m
471
+ 5.feet #=> 5 ft
472
+ 10.inches #=> 10 in
473
+
474
+ # Usage
475
+ ## Equality comparison
476
+ 1.foot == 12.inches #=> true
477
+ ## Arithmetic operation
478
+ 1.ft + 12.in #=> 2.0 ft
479
+ ```
480
+
481
+ ### Conversion methods
482
+
483
+ The `.define_conversion_methods` method allows you to define conversion methods that
484
+ help you to easily convert measurements to a different unit.
485
+
486
+ To define conversion methods for specific units within the unit group, use the following syntax:
487
+
488
+ ```ruby
489
+ UnitMeasurements::Length.define_conversion_methods("metre", "foot", "inch")
490
+ ```
491
+
492
+ This will enable you to convert units as:
493
+
494
+ ```ruby
495
+ UnitMeasurements::Length.new(1, "ft").in_inches #=> 12.0 in
496
+ 12.in.in_foot #=> 1.0 ft
497
+ ```
498
+
499
+ ## Contributing
500
+
501
+ Contributions to this project are welcomed! To contribute:
502
+
503
+ 1. Fork this repository
504
+ 2. Create a new branch (`git checkout -b my-new-feature`)
505
+ 3. Commit your changes (`git commit -am "Add some feature"`)
506
+ 4. Push the changes to your branch (`git push origin my-new-feature`)
507
+ 5. Create new **Pull Request**
508
+
509
+ ## License
510
+
511
+ Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ require "bundler/gem_tasks"
6
+ require "rspec/core/rake_task"
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec