unit_measurements 4.9.0 → 4.10.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 +4 -4
- data/.github/workflows/main.yml +12 -11
- data/.github/workflows/pages.yml +31 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +36 -69
- data/lib/unit_measurements/arithmetic.rb +91 -20
- data/lib/unit_measurements/base.rb +56 -0
- data/lib/unit_measurements/comparison.rb +45 -4
- data/lib/unit_measurements/conversion.rb +40 -10
- data/lib/unit_measurements/errors/parse_error.rb +20 -0
- data/lib/unit_measurements/errors/primitive_unit_already_set_error.rb +11 -0
- data/lib/unit_measurements/errors/unit_already_defined_error.rb +20 -0
- data/lib/unit_measurements/errors/unit_error.rb +19 -0
- data/lib/unit_measurements/formatter.rb +31 -9
- data/lib/unit_measurements/math.rb +51 -14
- data/lib/unit_measurements/measurement.rb +251 -8
- data/lib/unit_measurements/normalizer.rb +92 -3
- data/lib/unit_measurements/parser.rb +169 -7
- data/lib/unit_measurements/unit.rb +128 -1
- data/lib/unit_measurements/unit_group.rb +160 -11
- data/lib/unit_measurements/unit_group_builder.rb +133 -0
- data/lib/unit_measurements/version.rb +2 -1
- data/lib/unit_measurements.rb +16 -0
- data/unit_measurements.gemspec +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d936234f88dcdcecd6c7b5ca1644bfb8f36dc99f1f6ee37c291dc08837e22dbc
|
4
|
+
data.tar.gz: 47cb54e79069e35e255aed030a29ceff68c5b0021e45f817cb5eb09a5d0541fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2422ce59dcf6a41eb5490bcf118d6d987037c31d45fd35721c6e36dd81da55bd742984a41bebf69bd4a6640f140584fa4604bc8fff30a6537db8e92448f4907
|
7
|
+
data.tar.gz: c5529853733692e59b879b7a3972cffa3489a2681a6afb03f0ae08e94ea7c4a709c9e662cc59d6a76766ebcc396593d0cc3cb8cd1dca4d170b2385de9e019152
|
data/.github/workflows/main.yml
CHANGED
@@ -8,23 +8,24 @@ on:
|
|
8
8
|
- main
|
9
9
|
jobs:
|
10
10
|
build:
|
11
|
+
name: Ruby ${{ matrix.ruby }}
|
11
12
|
runs-on: ubuntu-latest
|
12
13
|
env:
|
13
14
|
RAILS_ENV: test
|
14
15
|
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
15
|
-
name: Ruby ${{ matrix.ruby }}
|
16
16
|
strategy:
|
17
17
|
matrix:
|
18
18
|
ruby:
|
19
19
|
- "3.2.2"
|
20
20
|
steps:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
- name: Checkout Repository
|
22
|
+
uses: actions/checkout@v3
|
23
|
+
- name: Set up Ruby
|
24
|
+
uses: ruby/setup-ruby@v1
|
25
|
+
with:
|
26
|
+
ruby-version: ${{ matrix.ruby }}
|
27
|
+
bundler-cache: true
|
28
|
+
- name: Run test cases
|
29
|
+
run: bundle exec rake
|
30
|
+
- name: Publish code coverage
|
31
|
+
uses: paambaati/codeclimate-action@v5.0.0
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Publish Documentation to GitHub Pages
|
2
|
+
on:
|
3
|
+
push:
|
4
|
+
branches:
|
5
|
+
- main
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
name: Publish Documentation to GitHub Pages ${{ matrix.ruby }}
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
permissions:
|
11
|
+
contents: write
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby:
|
15
|
+
- "3.2.2"
|
16
|
+
steps:
|
17
|
+
- name: Checkout Repository
|
18
|
+
uses: actions/checkout@v3
|
19
|
+
- name: Set Up Ruby
|
20
|
+
uses: ruby/setup-ruby@v1
|
21
|
+
with:
|
22
|
+
ruby-version: ${{ matrix.ruby }}
|
23
|
+
- name: Install Dependencies
|
24
|
+
run: gem install yard
|
25
|
+
- name: Generate Documentation
|
26
|
+
run: yardoc --private --exclude "unit_groups/*"
|
27
|
+
- name: Deploy to GitHub Pages
|
28
|
+
uses: peaceiris/actions-gh-pages@v3.9.3
|
29
|
+
with:
|
30
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
31
|
+
publish_dir: ./doc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## [4.10.0](https://github.com/shivam091/unit_measurements/compare/v4.9.0...v4.10.0) - 2023-10-14
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added documentation for all modules, classes, and methods.
|
6
|
+
- Updated readme and added workflow for publishing documentation using `yardoc`.
|
7
|
+
|
8
|
+
----------
|
9
|
+
|
1
10
|
## [4.9.0](https://github.com/shivam091/unit_measurements/compare/v4.8.0...v4.9.0) - 2023-10-13
|
2
11
|
|
3
12
|
### What's new
|
@@ -356,6 +365,7 @@
|
|
356
365
|
- Added support to build `si` units.
|
357
366
|
- Added support to parse `Complex`, `Rational`, `Scientific` numbers, and `ratios`.
|
358
367
|
- Added support to convert quantity between two units using `#convert_to`, `#convert_to!`, and `#parse` methods.
|
368
|
+
- Added normalizer to normalize special characters used in numbers.
|
359
369
|
|
360
370
|
----------
|
361
371
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -9,7 +9,7 @@ A library that encapsulate measurements and their units in Ruby.
|
|
9
9
|
[](https://codeclimate.com/github/shivam091/unit_measurements/test_coverage)
|
10
10
|
[](https://github.com/shivam091/unit_measurements/blob/main/LICENSE.md)
|
11
11
|
|
12
|
-
**Harshal V. Ladhe, Master of Computer Science
|
12
|
+
**[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**
|
13
13
|
|
14
14
|
## Introduction
|
15
15
|
|
@@ -26,8 +26,9 @@ The `unit_measurements` gem is designed to simplify the handling of units for sc
|
|
26
26
|
|
27
27
|
1. It provides easy conversion between units.
|
28
28
|
2. It is lightweight and easily extensible to include other units and conversions.
|
29
|
-
|
30
|
-
4. It
|
29
|
+
3. It has built in support for various [unit groups](https://github.com/shivam091/unit_measurements/blob/main/units.md).
|
30
|
+
4. It has well organized and very descriptive documentation published [here](https://shivam091.github.io/unit_measurements).
|
31
|
+
5. It can convert `complex`, `fractional`, `mixed fractional`, `scientific` numbers, and `ratios`.
|
31
32
|
|
32
33
|
## Disclaimer
|
33
34
|
|
@@ -113,27 +114,17 @@ UnitMeasurements::Length.parse("1 km to m")
|
|
113
114
|
**Parse scientific numbers, source unit, and (or) target unit:**
|
114
115
|
|
115
116
|
```ruby
|
116
|
-
UnitMeasurements::Length.
|
117
|
-
#=> 20000.0 m
|
118
|
-
UnitMeasurements::Length.new("2e+2", "km").convert_to("m")
|
119
|
-
#=> 200000.0 m
|
120
|
-
UnitMeasurements::Length.parse("2e² km").convert_to("m")
|
117
|
+
UnitMeasurements::Length.parse("2e+2 km").convert_to("m")
|
121
118
|
#=> 200000.0 m
|
122
119
|
UnitMeasurements::Length.parse("2e+2 km to m")
|
123
120
|
#=> 200000.0 m
|
124
|
-
UnitMeasurements::Length.parse("2e⁻² km to m")
|
125
|
-
#=> 20.0 m
|
126
121
|
```
|
127
|
-
|
128
|
-
|
122
|
+
You can check supported special characters for exponents
|
123
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).
|
129
124
|
|
130
125
|
**Parse complex numbers, source unit, and (or) target unit:**
|
131
126
|
|
132
127
|
```ruby
|
133
|
-
UnitMeasurements::Length.new(Complex(2, 3), "km").convert_to("m")
|
134
|
-
#=> 2000.0+3000.0i m
|
135
|
-
UnitMeasurements::Length.new("2+3i", "km").convert_to("m")
|
136
|
-
#=> 2000.0+3000.0i m
|
137
128
|
UnitMeasurements::Length.parse("2+3i km").convert_to("m")
|
138
129
|
#=> 2000.0+3000.0i m
|
139
130
|
UnitMeasurements::Length.parse("2+3i km to m")
|
@@ -143,12 +134,6 @@ UnitMeasurements::Length.parse("2+3i km to m")
|
|
143
134
|
**Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**
|
144
135
|
|
145
136
|
```ruby
|
146
|
-
UnitMeasurements::Length.new(Rational(2, 3), "km").convert_to("m")
|
147
|
-
#=> 666.666666666667 m
|
148
|
-
UnitMeasurements::Length.new("2/3", "km").convert_to("m")
|
149
|
-
#=> 666.666666666667 m
|
150
|
-
UnitMeasurements::Length.new("½", "km").convert_to("m")
|
151
|
-
#=> 500.0 m
|
152
137
|
UnitMeasurements::Length.parse("2 ½ km").convert_to("m")
|
153
138
|
#=> 2500.0 m
|
154
139
|
UnitMeasurements::Length.parse("2/3 km").convert_to("m")
|
@@ -161,7 +146,8 @@ UnitMeasurements::Length.parse("2 ½ km to m")
|
|
161
146
|
#=> 2500.0 m
|
162
147
|
```
|
163
148
|
|
164
|
-
|
149
|
+
You can check supported special characters for fractional notations
|
150
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).
|
165
151
|
|
166
152
|
**Parse ratios, source unit, and (or) target unit:**
|
167
153
|
|
@@ -188,6 +174,9 @@ UnitMeasurements::Length.new(100, "m").to("in").format("%.4<quantity>f")
|
|
188
174
|
#=> "3937.0079"
|
189
175
|
```
|
190
176
|
|
177
|
+
You can check more about formatting along with their examples
|
178
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Formatter.html).
|
179
|
+
|
191
180
|
**Extract the unit and the quantity from measurement:**
|
192
181
|
|
193
182
|
```ruby
|
@@ -216,14 +205,14 @@ UnitMeasurements::Length.units
|
|
216
205
|
|
217
206
|
```ruby
|
218
207
|
UnitMeasurements::Length.unit_names
|
219
|
-
#=> ["
|
208
|
+
#=> ["ft", "in", "m", "mi", "yd"]
|
220
209
|
```
|
221
210
|
|
222
211
|
**See all valid units of the unit group along with their aliases:**
|
223
212
|
|
224
213
|
```ruby
|
225
214
|
UnitMeasurements::Length.unit_names_with_aliases
|
226
|
-
#=> ["
|
215
|
+
#=> ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
|
227
216
|
```
|
228
217
|
|
229
218
|
**Finding units within the unit group:**
|
@@ -265,62 +254,44 @@ UnitMeasurements::Length.unit_or_alias?("metre")
|
|
265
254
|
|
266
255
|
You have ability to compare the measurements with the same or different units within the same unit group.
|
267
256
|
For example, comparing length with length will work, comparing a length with a area would fail.
|
268
|
-
Supported comparisons and methods are `==`, `!=`, `<`, `>`, `<=`, `>=`, `between?`, and `clamp`.
|
269
257
|
|
270
258
|
```ruby
|
271
|
-
UnitMeasurements::Length.
|
272
|
-
#=> true
|
273
|
-
UnitMeasurements::Length.parse("1 km") <= UnitMeasurements::Length.parse("0.5 km")
|
274
|
-
#=> false
|
275
|
-
UnitMeasurements::Length.new(1, "ft").between?(UnitMeasurements::Length.new(12, "in"), UnitMeasurements::Length.new(24, "in"))
|
259
|
+
UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m")
|
276
260
|
#=> true
|
277
261
|
```
|
278
262
|
|
263
|
+
You can check supported comparisons along with their examples
|
264
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Comparison.html).
|
265
|
+
|
279
266
|
### Arithmetic
|
280
267
|
|
281
268
|
You have ability to perform arithmetic operations on measurements with the same or
|
282
269
|
different units within a same unit group. You can perform arithmetic operations on
|
283
|
-
measurement by either other measurement with compatible unit or
|
284
|
-
In cases of different units, the left hand side takes precedence
|
285
|
-
|
286
|
-
**Methods:**
|
287
|
-
1. `#+` - Adds the other measurement quantity or number to the measurement.
|
288
|
-
2. `#-` - Subtracts the other measurement quantity or number from the measurement.
|
289
|
-
3. `#*` - Multiplies the measurement quantity by other measurement quantity or number.
|
290
|
-
4. `#/` - Divides the measurement quantity by other measurement quantity or number.
|
270
|
+
measurement by either other measurement with compatible unit or numeric value.
|
271
|
+
In cases of different units, the left hand side takes precedence.
|
291
272
|
|
292
273
|
```ruby
|
293
274
|
UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
|
294
275
|
#=> 1.001 km
|
295
|
-
UnitMeasurements::Length.new(2, "km")
|
296
|
-
#=>
|
297
|
-
UnitMeasurements::Length.new(2, "km") * 2
|
298
|
-
#=> 4 km
|
299
|
-
UnitMeasurements::Length.new(4, "km") / UnitMeasurements::Length.new(2, "km")
|
300
|
-
#=> 2 km
|
276
|
+
UnitMeasurements::Length.new(2, "km") * 2+2i
|
277
|
+
#=> 4+2i km
|
301
278
|
```
|
302
279
|
|
303
|
-
|
280
|
+
You can check supported arithmetic operations along with their examples
|
281
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Arithmetic.html).
|
304
282
|
|
305
|
-
|
283
|
+
### Math
|
306
284
|
|
307
|
-
|
308
|
-
1. `#round` - Rounds quantity of the measurement. If `ndigits` is not specified, quantity is rounded to `Integer`.
|
309
|
-
2. `#abs` - Returns absolute value of the measurement quantity.
|
310
|
-
3. `#floor` - Rounds quantity of the measurement to next lower integer.
|
311
|
-
4. `#ceil` - Rounds quantity of the measurement to next higher integer.
|
285
|
+
You can perform mathematical functions on the measurements.
|
312
286
|
|
313
287
|
```ruby
|
314
|
-
UnitMeasurements::Length.new(
|
315
|
-
|
316
|
-
UnitMeasurements::Length.new(-17.625, "m").abs
|
317
|
-
#=> 17.625 m
|
318
|
-
UnitMeasurements::Length.new(17.625, "m").floor
|
319
|
-
#=> 17 m
|
320
|
-
UnitMeasurements::Length.new(17.625, "m").ceil
|
321
|
-
#=> 18 m
|
288
|
+
UnitMeasurements::Length.new(17.625, "m").round
|
289
|
+
# => 18 m
|
322
290
|
```
|
323
291
|
|
292
|
+
You can check supported mathematical functions along with their examples
|
293
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Math.html).
|
294
|
+
|
324
295
|
### Conversions
|
325
296
|
|
326
297
|
You can convert measurement quantity directly to other numeric types viz.
|
@@ -329,16 +300,11 @@ You can convert measurement quantity directly to other numeric types viz.
|
|
329
300
|
```ruby
|
330
301
|
UnitMeasurements::Length.new(2.25567, "km").to_i
|
331
302
|
#=> 2 km
|
332
|
-
UnitMeasurements::Length.new(2.25567, "km").to_f
|
333
|
-
#=> 2.25567 km
|
334
|
-
UnitMeasurements::Length.new(2.25567, "km").to_r
|
335
|
-
#=> 225567/100000 km
|
336
|
-
UnitMeasurements::Length.new(2.25567, "km").to_d
|
337
|
-
#=> 2.25567 km
|
338
|
-
UnitMeasurements::Length.new(2.25567, "km").to_c
|
339
|
-
#=> 2.25567+0i km
|
340
303
|
```
|
341
304
|
|
305
|
+
You can check more about them along with their examples
|
306
|
+
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Conversion.html).
|
307
|
+
|
342
308
|
## Units
|
343
309
|
|
344
310
|
The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
|
@@ -377,7 +343,8 @@ Units declared through it will have automatic support for all decimal prefixes:
|
|
377
343
|
|
378
344
|
### Bundled units
|
379
345
|
|
380
|
-
There are tons of units that are bundled in `unit_measurements`. You can check them out
|
346
|
+
There are tons of units that are bundled in `unit_measurements`. You can check them out
|
347
|
+
[here](https://github.com/shivam091/unit_measurements/blob/main/units.md).
|
381
348
|
|
382
349
|
### Specifing units
|
383
350
|
|
@@ -3,61 +3,118 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
|
+
# The +UnitMeasurements::Arithmetic+ mixin module provides methods for
|
7
|
+
# performing arithmetic operations (addition, subtraction, multiplication,
|
8
|
+
# division, etc) on measurements of the same unit group. In case the
|
9
|
+
# measurements represents different units, the left hand side takes precedence
|
10
|
+
# while performing the arithmetic operation on them.
|
11
|
+
#
|
12
|
+
# This module is included in the +Measurement+ class to allow arithmetic
|
13
|
+
# operations on the measurements.
|
14
|
+
#
|
15
|
+
# @see Measurement
|
16
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
17
|
+
# @since 1.4.0
|
6
18
|
module Arithmetic
|
7
|
-
# Adds the other measurement
|
19
|
+
# Adds the quantity of the other measurement or a numeric value to the
|
20
|
+
# quantity of the current measurement.
|
8
21
|
#
|
9
|
-
# @param [Numeric
|
22
|
+
# @param [Numeric|Measurement] other
|
23
|
+
# The value to be added. It can be a numeric value or another measurement.
|
10
24
|
#
|
11
25
|
# @example
|
12
|
-
# UnitMeasurements::
|
13
|
-
# => 1.001
|
26
|
+
# UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
|
27
|
+
# => 1.001 km
|
14
28
|
#
|
15
|
-
#
|
29
|
+
# UnitMeasurements::Length.new(1, "km") + 4.5
|
30
|
+
# => 5.5 km
|
31
|
+
#
|
32
|
+
# @return [Measurement] A new +Measurement+ instance with the combined quantity.
|
33
|
+
#
|
34
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
35
|
+
# @since 1.4.0
|
16
36
|
def +(other)
|
17
37
|
arithmetic_operation(other, :+)
|
18
38
|
end
|
19
39
|
|
20
|
-
# Subtracts the other measurement
|
40
|
+
# Subtracts the quantity of the other measurement or a numeric value from the
|
41
|
+
# quantity of the current measurement.
|
21
42
|
#
|
22
|
-
# @param [Numeric
|
43
|
+
# @param [Numeric|Measurement] other
|
44
|
+
# The value to be subtracted. It can be a numeric value or another measurement.
|
23
45
|
#
|
24
46
|
# @example
|
25
|
-
# UnitMeasurements::
|
26
|
-
# =>
|
47
|
+
# UnitMeasurements::Length.new(1, "km") - UnitMeasurements::Length.new(2, "in")
|
48
|
+
# => 0.9999492 km
|
27
49
|
#
|
28
|
-
#
|
50
|
+
# UnitMeasurements::Length.new(2, "km") - 1e+2
|
51
|
+
# => -98.0 km
|
52
|
+
#
|
53
|
+
# @return [Measurement] A new +Measurement+ instance with the subtracted quantity.
|
54
|
+
#
|
55
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
56
|
+
# @since 1.4.0
|
29
57
|
def -(other)
|
30
58
|
arithmetic_operation(other, :-)
|
31
59
|
end
|
32
60
|
|
33
|
-
# Multiplies the
|
61
|
+
# Multiplies the quantity of the current measurement by the quantity of the
|
62
|
+
# other measurement or a numeric value.
|
34
63
|
#
|
35
|
-
# @param [Numeric
|
64
|
+
# @param [Numeric|Measurement] other
|
65
|
+
# The value to be multiplied. It can be a numeric value or another measurement.
|
36
66
|
#
|
37
67
|
# @example
|
38
|
-
# UnitMeasurements::
|
39
|
-
# =>
|
68
|
+
# UnitMeasurements::Length.new(2, "km") * UnitMeasurements::Length.new(3, "in")
|
69
|
+
# => 0.0001524 km
|
40
70
|
#
|
41
|
-
#
|
71
|
+
# UnitMeasurements::Length.new(2, "km") * 2+2i
|
72
|
+
# => 4+2i km
|
73
|
+
#
|
74
|
+
# @return [Measurement] A new +Measurement+ instance with the multiplied quantity.
|
75
|
+
#
|
76
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
77
|
+
# @since 1.4.0
|
42
78
|
def *(other)
|
43
79
|
arithmetic_operation(other, :*)
|
44
80
|
end
|
45
81
|
|
46
|
-
# Divides the
|
82
|
+
# Divides the quantity of the current measurement by the quantity of the other
|
83
|
+
# measurement or a numeric value.
|
47
84
|
#
|
48
|
-
# @param [Numeric
|
85
|
+
# @param [Numeric|Measurement] other
|
86
|
+
# The value to be divided. It can be a numeric value or another measurement.
|
49
87
|
#
|
50
88
|
# @example
|
51
|
-
# UnitMeasurements::
|
52
|
-
# => 2
|
89
|
+
# UnitMeasurements::Length.new(4, "km") / UnitMeasurements::Length.new(2, "km")
|
90
|
+
# => 2 km
|
53
91
|
#
|
54
|
-
#
|
92
|
+
# UnitMeasurements::Length.new(2, "km") / 2
|
93
|
+
# => 1 km
|
94
|
+
#
|
95
|
+
# @return [Measurement] A new +Measurement+ instance with the divided quantity.
|
96
|
+
#
|
97
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
98
|
+
# @since 1.4.0
|
55
99
|
def /(other)
|
56
100
|
arithmetic_operation(other, :/)
|
57
101
|
end
|
58
102
|
|
59
103
|
private
|
60
104
|
|
105
|
+
# @private
|
106
|
+
# Coerces a numeric value or another measurement for arithmetic operations.
|
107
|
+
#
|
108
|
+
# @param [Numeric|Measurement] other
|
109
|
+
# The value to be coerced. It can be a numeric value or another measurement.
|
110
|
+
#
|
111
|
+
# @return [Array<Measurement>] An array containing the coerced values.
|
112
|
+
#
|
113
|
+
# @raise [TypeError]
|
114
|
+
# If the coercion is not possible due to incompatible types.
|
115
|
+
#
|
116
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
117
|
+
# @since 1.4.0
|
61
118
|
def coerce(other)
|
62
119
|
case other
|
63
120
|
when Numeric then [self.class.new(other, self.unit), self]
|
@@ -66,6 +123,20 @@ module UnitMeasurements
|
|
66
123
|
end
|
67
124
|
end
|
68
125
|
|
126
|
+
# @private
|
127
|
+
# Performs an arithmetic operation (addition, subtraction, multiplication,
|
128
|
+
# or division) on the current measurement and another numeric value.
|
129
|
+
#
|
130
|
+
# @param [Numeric|Measurement] other
|
131
|
+
# The value to be used in the arithmetic operation. It can be a numeric value
|
132
|
+
# or another measurement.
|
133
|
+
# @param [Symbol] operator The operator to be used for the operation.
|
134
|
+
#
|
135
|
+
# @return [Measurement]
|
136
|
+
# A new +Measurement+ instance with the result of the arithmetic operation.
|
137
|
+
#
|
138
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
139
|
+
# @since 1.4.0
|
69
140
|
def arithmetic_operation(other, operator)
|
70
141
|
other, _ = coerce(other)
|
71
142
|
|
@@ -7,6 +7,61 @@ require "unit_measurements/version"
|
|
7
7
|
|
8
8
|
module UnitMeasurements
|
9
9
|
class << self
|
10
|
+
# Creates a new unit group based on the provided +block+ of instructions.
|
11
|
+
#
|
12
|
+
# The +build+ method allows you to define and create a custom unit group with
|
13
|
+
# units and their conversions. It takes a block of instructions as an argument,
|
14
|
+
# which is evaluated by an instance of +UnitGroupBuilder+.
|
15
|
+
#
|
16
|
+
# Within the +block+, you can use various methods provided by +UnitGroupBuilder+
|
17
|
+
# to define units, group them into unit system, and set primitive unit of
|
18
|
+
# the unit group. These methods include +primitive+, +system+, +si_unit+,
|
19
|
+
# and +unit+.
|
20
|
+
#
|
21
|
+
# The resulting unit group is encapsulated in a new subclass of +Measurement+.
|
22
|
+
# This subclass will have access to the defined units and their conversions
|
23
|
+
# through the +unit_group+ class attribute.
|
24
|
+
#
|
25
|
+
# This method provides a powerful way to create specialized unit groups tailored
|
26
|
+
# to specific measurement domains.
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
# UnitMeasurements.build do
|
30
|
+
# primitive "m"
|
31
|
+
#
|
32
|
+
# system :metric do
|
33
|
+
# si_unit "m", aliases: ["meter", "metre", "meters", "metres"]
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# system :imperial do
|
37
|
+
# unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# @param block
|
42
|
+
# A block of instructions for defining units and their conversions within
|
43
|
+
# the unit group.
|
44
|
+
#
|
45
|
+
# @yield [builder]
|
46
|
+
# A block that defines the units to be added to the unit group.
|
47
|
+
# The block takes a {UnitGroupBuilder} instance as a parameter.
|
48
|
+
#
|
49
|
+
# @yieldparam builder [UnitGroupBuilder]
|
50
|
+
# The {UnitGroupBuilder} instance to define units within the unit group.
|
51
|
+
#
|
52
|
+
# @yieldreturn [UnitGroup]
|
53
|
+
# Returns an instance of {UnitGroup} containing the units and their conversions
|
54
|
+
# defined within the block.
|
55
|
+
#
|
56
|
+
# @return [Class]
|
57
|
+
# A new subclass of +Measurement+ with the defined units and conversions.
|
58
|
+
#
|
59
|
+
# @see Unit
|
60
|
+
# @see UnitGroup
|
61
|
+
# @see UnitGroupBuilder
|
62
|
+
# @see Measurement
|
63
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
64
|
+
# @since 1.0.0
|
10
65
|
def build(&block)
|
11
66
|
builder = UnitGroupBuilder.new
|
12
67
|
builder.instance_eval(&block)
|
@@ -22,6 +77,7 @@ module UnitMeasurements
|
|
22
77
|
end
|
23
78
|
end
|
24
79
|
|
80
|
+
# The following requires load various components of the unit measurements library.
|
25
81
|
require "unit_measurements/unit_group_builder"
|
26
82
|
require "unit_measurements/unit"
|
27
83
|
require "unit_measurements/unit_group"
|
@@ -3,17 +3,58 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
|
+
# The +UnitMeasurements::Comparison+ mixin module is included in measurement
|
7
|
+
# classes to enable comparison operations (e.g., less than, equal to, greater
|
8
|
+
# than, etc.) between two measurements of the same unit group.
|
9
|
+
#
|
10
|
+
# This module is included in the +Measurement+ class to allow comparison of two
|
11
|
+
# measurements.
|
12
|
+
#
|
13
|
+
# @see Measurement
|
14
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
15
|
+
# @since 1.3.0
|
6
16
|
module Comparison
|
7
17
|
include Comparable
|
8
18
|
|
9
|
-
#
|
10
|
-
# the
|
19
|
+
# This method is used to compare the quantity of two measurements. It
|
20
|
+
# implements the comparison logic based on the +<=>+ method defined in the
|
21
|
+
# +Comparable+ module.
|
11
22
|
#
|
12
23
|
# @example
|
13
|
-
# UnitMeasurements::
|
24
|
+
# UnitMeasurements::Length.new(1, "km") == UnitMeasurements::Length.new(1, :km)
|
14
25
|
# => true
|
15
26
|
#
|
16
|
-
#
|
27
|
+
# UnitMeasurements::Length.parse("1 km") == UnitMeasurements::Length.parse("1000 m")
|
28
|
+
# => true
|
29
|
+
#
|
30
|
+
# UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m")
|
31
|
+
# => true
|
32
|
+
#
|
33
|
+
# UnitMeasurements::Length.parse("1 km") < UnitMeasurements::Length.parse("0.5 km")
|
34
|
+
# => false
|
35
|
+
#
|
36
|
+
# UnitMeasurements::Length.parse("1 km") > UnitMeasurements::Length.parse("0.5 km")
|
37
|
+
# => true
|
38
|
+
#
|
39
|
+
# UnitMeasurements::Length.parse("1 km") <= UnitMeasurements::Length.parse("0.5 km")
|
40
|
+
# => false
|
41
|
+
#
|
42
|
+
# UnitMeasurements::Length.parse("1 km") >= UnitMeasurements::Length.parse("0.5 km")
|
43
|
+
# => true
|
44
|
+
#
|
45
|
+
# UnitMeasurements::Length.new(1, "ft").between?(UnitMeasurements::Length.new(12, "in"), UnitMeasurements::Length.new(24, "in"))
|
46
|
+
# => true
|
47
|
+
#
|
48
|
+
# @param [Measurement] object The measurement instance to compare with.
|
49
|
+
#
|
50
|
+
# @return
|
51
|
+
# +nil+ if the comparison is not possible (e.g., if the +object+ is not of
|
52
|
+
# the same unit group). A negative integer if +self+ is less than +object+.
|
53
|
+
# +Zero+ if self is equal to +object+. A positive integer if +self+ is
|
54
|
+
# greater than +object+.
|
55
|
+
#
|
56
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
57
|
+
# @since 1.3.0
|
17
58
|
def <=>(object)
|
18
59
|
return nil unless object.is_a?(self.class)
|
19
60
|
|