unit_measurements 4.9.0 → 4.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47fa12ab5aef0b04a4c5c0144878e0d7493b0ffba91c046633df4512b38b8fc5
4
- data.tar.gz: 889a7b19a632a2fbdbe7cfc8efa2f917b792ffb76cf75922001e342533c06b67
3
+ metadata.gz: 60c27b3d75f3b92e616011aecc0f220ef26f1a2e0828bcc5c85b2d0cadd3b81b
4
+ data.tar.gz: d9d8fd3e291b6cb8391bd45184f09432c1ed9808d80cc0124f26d889f578f680
5
5
  SHA512:
6
- metadata.gz: 2a70f8eb32af057207c1846a2d6678abca3f683bc5cb7f9278bdc45f3e31f6d330ad23f2c146e4ad2a192d2a245729b7dfe82cfb68926f469b77ed4508309bf0
7
- data.tar.gz: 6c36c79eb4c7b1d3edaf9fe66e034ad5d8cea4d0ae2773f30dcf5a1f9ba0cf807cbd5793cf05590d8ec7d2a3cd21fb107358ff2e93f980c7cb356aa43bb8f1fb
6
+ metadata.gz: 5612e9192c1c59076d94072d2e7c6d7838c4022ff152124ece55a9d4986fc8008759947469b0310c6c6dfa7045cb732c1e46d993585f907c595b9b342d5abad8
7
+ data.tar.gz: bbe280e459ae3ccfe7cbed4fdf56bd4f0666b4f02e4b91ba1b4b46747f7fb717a9ac77590eb7767723c23a2eca1993a95e803d5f661dec5530b392e4d633738b
@@ -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
- - uses: actions/checkout@v3
22
- - name: Set up Ruby
23
- uses: ruby/setup-ruby@v1
24
- with:
25
- ruby-version: ${{ matrix.ruby }}
26
- bundler-cache: true
27
- - name: Run test cases
28
- run: bundle exec rake
29
- - name: Publish code coverage
30
- uses: paambaati/codeclimate-action@v5.0.0
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,21 @@
1
+ ## [4.11.0](https://github.com/shivam091/unit_measurements/compare/v4.10.0...v4.11.0) - 2023-10-15
2
+
3
+ ### What's new
4
+
5
+ - Added new units in `length` (ch, th, ftm, fur, nmi, rod, au, pc) unit group.
6
+ - Added new units in `weight` (ct, Da, lb, st, oz, gr, ozt, dwt, dr, slug) unit group.
7
+
8
+ ----------
9
+
10
+ ## [4.10.0](https://github.com/shivam091/unit_measurements/compare/v4.9.0...v4.10.0) - 2023-10-14
11
+
12
+ ### What's new
13
+
14
+ - Added documentation for all modules, classes, and methods.
15
+ - Updated readme and added workflow for publishing documentation using `yardoc`.
16
+
17
+ ----------
18
+
1
19
  ## [4.9.0](https://github.com/shivam091/unit_measurements/compare/v4.8.0...v4.9.0) - 2023-10-13
2
20
 
3
21
  ### What's new
@@ -356,6 +374,7 @@
356
374
  - Added support to build `si` units.
357
375
  - Added support to parse `Complex`, `Rational`, `Scientific` numbers, and `ratios`.
358
376
  - Added support to convert quantity between two units using `#convert_to`, `#convert_to!`, and `#parse` methods.
377
+ - Added normalizer to normalize special characters used in numbers.
359
378
 
360
379
  ----------
361
380
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (4.9.0)
4
+ unit_measurements (4.11.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -9,7 +9,7 @@ A library that encapsulate measurements and their units in Ruby.
9
9
  [![Test Coverage](https://api.codeclimate.com/v1/badges/b8aec9bffa356d108784/test_coverage)](https://codeclimate.com/github/shivam091/unit_measurements/test_coverage)
10
10
  [![License](https://img.shields.io/badge/License-MIT-blue.svg)](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
- 2. It has built in support for various [unit groups](units.md).
30
- 4. It can convert `complex`, `fractional`, `mixed fractional`, `scientific` numbers, and `ratios`.
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.new(BigDecimal(2), "km").convert_to("m")
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
- Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `⁵`, `⁶`, `⁷`, `⁸`, `⁹`, `⁺`, `⁻`.
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
- Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓`, `⅔`, `⅕`, `⅖`, `⅗`, `⅘`, `⅙`, `⅚`, `⅐`, `⅛`, `⅜`, `⅝`, `⅞`, `⅑`, `⅒`, `↉`, `⁄`.
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
- #=> ["m", "km", "mi", "ft", "in", "yd", ...]
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
- #=> ["g", "meter", "metre", "meters", "metres", "km", "kilometer", "kilometre", "kilometers", "kilometres", "in", "inch", "inches", "yd", "yard", "yards", ...]
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.new(1, "km") == UnitMeasurements::Length.new(1, "km")
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 number.
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") - 1
296
- #=> 1 km
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
- ### Math
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
- You can perform mathematical operations on the measurements.
283
+ ### Math
306
284
 
307
- **Methods:**
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(1, "m").to("in").round(4)
315
- #=> 39.3701 in
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 [here](units.md).
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
 
@@ -464,4 +431,4 @@ Volume = UnitMeasurements::Volume
464
431
 
465
432
  ## License
466
433
 
467
- Copyright 2023 [Harshal V. LADHE](https://github.com/shivam091), Released under the [MIT License](http://opensource.org/licenses/MIT).
434
+ Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -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 quantity or number to the 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 or Measurement] other
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::Weight.new(1, "kg") + UnitMeasurements::Weight.new(1, "g")
13
- # => 1.001 kg
26
+ # UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
27
+ # => 1.001 km
14
28
  #
15
- # @return [Measurement]
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 quantity or number from the 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 or Measurement] other
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::Weight.new(2, "kg") - 1
26
- # => 1 kg
47
+ # UnitMeasurements::Length.new(1, "km") - UnitMeasurements::Length.new(2, "in")
48
+ # => 0.9999492 km
27
49
  #
28
- # @return [Measurement]
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 measurement quantity by other measurement quantity or number.
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 or Measurement] other
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::Weight.new(2, "kg") * 2
39
- # => 4 kg
68
+ # UnitMeasurements::Length.new(2, "km") * UnitMeasurements::Length.new(3, "in")
69
+ # => 0.0001524 km
40
70
  #
41
- # @return [Measurement]
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 measurement quantity by other measurement quantity or number.
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 or Measurement] other
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::Weight.new(4, "kg") / UnitMeasurements::Weight.new(2, "kg")
52
- # => 2 kg
89
+ # UnitMeasurements::Length.new(4, "km") / UnitMeasurements::Length.new(2, "km")
90
+ # => 2 km
53
91
  #
54
- # @return [Measurement]
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
- # Implements +Comparable+ methods on the quantities of two measurements of
10
- # the same unit group.
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::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
24
+ # UnitMeasurements::Length.new(1, "km") == UnitMeasurements::Length.new(1, :km)
14
25
  # => true
15
26
  #
16
- # @return [Boolean]
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