unit_measurements 5.14.0 → 5.16.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: d59abc6cd3128c515be37c2e7bcc40cbbaa73530a4a5ed1ce6d5e7093202e063
4
- data.tar.gz: 22091e2ce789b4a064fb0f06fb704805b69cc86601334ab4d9f8a7afe9c6a0fd
3
+ metadata.gz: fcdf50759b2e50022f7dc5b4661a5ad149c781f0cc39f1f6f1c21825b8dec89d
4
+ data.tar.gz: 6ca3e101ab49cce3c7f6b6c177373cf70187e677135f9daee6ee33d7a8154bf2
5
5
  SHA512:
6
- metadata.gz: 4e16e3e25e06d155664fab616cf4e3147d75cf4aef71c15c6312572418fd124d11436c402fbe7e83e984e483c737a073ad180947f0ee071fab170a59c8f880a4
7
- data.tar.gz: b6e6bd19810a115b48d7107859b82e21d8b5dc231455d8a995a9ea5157c44d38c53c287a15945a342f1b376fcfe80b539de29ad1be9ca112e5750ac866013cbc
6
+ metadata.gz: 80b49c55971a01de943c2b7b5638debc16a49c9c68ee38bf84df62168a1d366fa80c12870cf483564350127b7bbc17cbb012e9947ecba7076699e4300458f248
7
+ data.tar.gz: c185e222fc88a2bc91ad232a004ef31e22669b16c4690eaefeff12b16e665d9cb6964f2504339fb0a70d0aa5f668fd085113a7232b9b08df93217f72f273bcd0
data/CHANGELOG.md CHANGED
@@ -1,8 +1,24 @@
1
+ ## [5.16.0](https://github.com/shivam091/unit_measurements/compare/v5.15.0...v5.16.0) - 2023-12-13
2
+
3
+ ### What's new
4
+
5
+ - Added planck units and their conversions.
6
+
7
+ ----------
8
+
9
+ ## [5.15.0](https://github.com/shivam091/unit_measurements/compare/v5.14.0...v5.15.0) - 2023-12-11
10
+
11
+ ### What's new
12
+
13
+ - Added `.define_conversion_methods` method to define conversion helper methods for units.
14
+
15
+ ----------
16
+
1
17
  ## [5.14.0](https://github.com/shivam091/unit_measurements/compare/v5.13.0...v5.14.0) - 2023-11-29
2
18
 
3
19
  ### What's new
4
20
 
5
- - Added `.define_numeric_methods` support to define numeric extension methods for units.
21
+ - Added `.define_numeric_methods` method to define numeric extension methods for units.
6
22
 
7
23
  ----------
8
24
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.14.0)
4
+ unit_measurements (5.16.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.](https://shivam091.github.io)
12
+ **[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**
13
13
 
14
14
  ## Introduction
15
15
 
@@ -43,11 +43,11 @@ Users are advised to cross-verify conversions for their specific use cases._
43
43
 
44
44
  ## Minimum Requirements
45
45
 
46
- * Ruby 3.2.2+ (https://www.ruby-lang.org/en/downloads/branches/)
46
+ * Ruby 3.2.2+ ([Download Ruby](https://www.ruby-lang.org/en/downloads/branches/))
47
47
 
48
48
  ## Installation
49
49
 
50
- If using bundler, first add this line to your application's Gemfile:
50
+ To use `unit_measurements` in your Rails application, add the following line to your Gemfile:
51
51
 
52
52
  ```ruby
53
53
  gem "unit_measurements"
@@ -87,8 +87,7 @@ viz., `UnitMeasurements::Weight`, `UnitMeasurements::Length`, etc.
87
87
  **Initialize a measurement:**
88
88
 
89
89
  ```ruby
90
- UnitMeasurements::Length.new(1, "km")
91
- #=> 1 km
90
+ UnitMeasurements::Length.new(1, "km") #=> 1 km
92
91
  ```
93
92
 
94
93
  **Converting to other units:**
@@ -102,22 +101,19 @@ These methods provide `use_cache` parameter which defaults to `false` to indicat
102
101
  You can use `#convert_to` as:
103
102
 
104
103
  ```ruby
105
- UnitMeasurements::Length.new(1, "km").convert_to("m", use_cache: true)
106
- #=> 1000.0 m
104
+ UnitMeasurements::Length.new(1, "km").convert_to("m", use_cache: true) #=> 1000.0 m
107
105
  ```
108
106
 
109
107
  If you want to modify measurement object itself, you can use `#convert_to!` method as:
110
108
 
111
109
  ```ruby
112
- UnitMeasurements::Length.new(1, "km").convert_to!("m")
113
- #=> 1000.0 m
110
+ UnitMeasurements::Length.new(1, "km").convert_to!("m") #=> 1000.0 m
114
111
  ```
115
112
 
116
113
  You can convert the measurement directly to the `primitive` unit of the unit group as:
117
114
 
118
115
  ```ruby
119
- UnitMeasurements::Length.new(1, "cm").to_primitive
120
- #=> 0.01 m
116
+ UnitMeasurements::Length.new(1, "cm").to_primitive #=> 0.01 m
121
117
  ```
122
118
 
123
119
  Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
@@ -127,8 +123,7 @@ Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
127
123
  This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
128
124
 
129
125
  ```ruby
130
- UnitMeasurements::Length.parse("1 km")
131
- #=> 1.0 km
126
+ UnitMeasurements::Length.parse("1 km") #=> 1.0 km
132
127
  ```
133
128
 
134
129
  **Parse string that mentions quantity, source unit, and target unit:**
@@ -136,17 +131,14 @@ UnitMeasurements::Length.parse("1 km")
136
131
  A source unit can be separated from the target unit using the `in`, `to`, or `as` operators.
137
132
 
138
133
  ```ruby
139
- UnitMeasurements::Length.parse("1 km to m")
140
- #=> 1000.0 m
134
+ UnitMeasurements::Length.parse("1 km to m") #=> 1000.0 m
141
135
  ```
142
136
 
143
137
  **Parse scientific numbers, source unit, and (or) target unit:**
144
138
 
145
139
  ```ruby
146
- UnitMeasurements::Length.parse("2e+2 km").convert_to("m")
147
- #=> 200000.0 m
148
- UnitMeasurements::Length.parse("2e+2 km to m")
149
- #=> 200000.0 m
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
150
142
  ```
151
143
  You can check supported special characters for exponents
152
144
  [here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Normalizer.html).
@@ -154,19 +146,15 @@ You can check supported special characters for exponents
154
146
  **Parse complex numbers, source unit, and (or) target unit:**
155
147
 
156
148
  ```ruby
157
- UnitMeasurements::Length.parse("2+3i km").convert_to("m")
158
- #=> 2000.0+3000.0i m
159
- UnitMeasurements::Length.parse("2+3i km to m")
160
- #=> 2000.0+3000.0i m
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
161
151
  ```
162
152
 
163
153
  **Parse fractional/mixed fractional numbers, source unit, and (or) target unit:**
164
154
 
165
155
  ```ruby
166
- UnitMeasurements::Length.parse("2 ½ km").convert_to("m")
167
- #=> 2500.0 m
168
- UnitMeasurements::Length.parse("2/3 km to m")
169
- #=> 666.666666666667 m
156
+ UnitMeasurements::Length.parse("2 ½ km").convert_to("m") #=> 2500.0 m
157
+ UnitMeasurements::Length.parse("2/3 km to m") #=> 666.666666666667 m
170
158
  ```
171
159
 
172
160
  You can check supported special characters for fractional notations
@@ -175,17 +163,14 @@ You can check supported special characters for fractional notations
175
163
  **Parse ratios, source unit, and (or) target unit:**
176
164
 
177
165
  ```ruby
178
- UnitMeasurements::Length.parse("1:2 km").convert_to("m")
179
- #=> 500.0 m
180
- UnitMeasurements::Length.parse("1:2 km to m")
181
- #=> 500.0 m
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
182
168
  ```
183
169
 
184
170
  **Calculate the ratio between two units:**
185
171
 
186
172
  ```ruby
187
- UnitMeasurements::Length.ratio("in", "ft")
188
- #=> "12.0 in/ft"
173
+ UnitMeasurements::Length.ratio("in", "ft") #=> "12.0 in/ft"
189
174
  ```
190
175
 
191
176
  **Formatting measurement:**
@@ -194,8 +179,7 @@ If you want to format the measurement to certain format, you can use `#to_fs` me
194
179
  If format is not specified, it defaults to `"%.2<value>f %<unit>s"`.
195
180
 
196
181
  ```ruby
197
- UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
198
- #=> "3937.0079 in"
182
+ UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s") #=> "3937.0079 in"
199
183
  ```
200
184
 
201
185
  You can check more about formatting along with their examples
@@ -205,26 +189,23 @@ You can check more about formatting along with their examples
205
189
 
206
190
  ```ruby
207
191
  length = UnitMeasurements::Length.new(1, "km")
208
- length.quantity
209
- #=> 1
210
- length.unit
211
- #=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
192
+ length.quantity #=> 1
193
+ length.unit #=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
212
194
  ```
213
195
 
214
196
  Unit object can be interrogated for a range of attributes:
215
197
 
216
198
  ```ruby
217
- length.unit.aliases # Alternative names for the unit.
218
- #=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
219
- length.unit.conversion_factor # Conversion factor relative to primitive unit.
220
- #=> 1000.0
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
221
203
  ```
222
204
 
223
205
  **See primitive unit of the unit group:**
224
206
 
225
207
  ```ruby
226
- UnitMeasurements::Length.primitive
227
- #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
208
+ UnitMeasurements::Length.primitive #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
228
209
  ```
229
210
 
230
211
  **See all units of the unit group:**
@@ -237,8 +218,7 @@ UnitMeasurements::Length.units
237
218
  **See names of all valid units of the unit group:**
238
219
 
239
220
  ```ruby
240
- UnitMeasurements::Length.unit_names
241
- #=> ["ft", "in", "m", "mi", "yd"]
221
+ UnitMeasurements::Length.unit_names #=> ["ft", "in", "m", "mi", "yd"]
242
222
  ```
243
223
 
244
224
  **See all valid units of the unit group along with their aliases:**
@@ -251,8 +231,7 @@ UnitMeasurements::Length.unit_names_with_aliases
251
231
  **See list of unit systems defined within the unit group:**
252
232
 
253
233
  ```ruby
254
- UnitMeasurements::Length.systems
255
- #=> ["metric", "imperial", "us_customary", "astronomical"]
234
+ UnitMeasurements::Length.systems #=> ["metric", "imperial", "us_customary", "astronomical"]
256
235
  ```
257
236
 
258
237
  **See list of units within the unit system:**
@@ -269,34 +248,26 @@ UnitMeasurements::Length.units_for("metric")
269
248
  **Finding units within the unit group:**
270
249
 
271
250
  You can use `#unit_for` or `#unit_for!` (aliased as `#[]`) methods to find units
272
- within the unit group. `#unit_for!` method returns an error if a unit is not present
273
- in the unit group.
251
+ within the unit group. `#unit_for!` method returns an error if a unit system is not defined within the unit group.
274
252
 
275
253
  ```ruby
276
- UnitMeasurements::Length.unit_for("m")
277
- #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
278
- UnitMeasurements::Length.unit_for("z")
279
- #=> nil
280
- UnitMeasurements::Length.unit_for!("z")
281
- #=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
254
+ UnitMeasurements::Length.unit_for("m") #=> #<UnitMeasurements::Unit: m (meter, meters, metre, metres)>
255
+ UnitMeasurements::Length.unit_for("z") #=> nil
256
+ UnitMeasurements::Length.unit_for!("z") #=> Invalid unit: 'z'. (UnitMeasurements::UnitError)
282
257
  ```
283
258
 
284
259
  **Finding whether the unit is defined within the unit group:**
285
260
 
286
261
  ```ruby
287
- UnitMeasurements::Length.defined?("m")
288
- #=> true
289
- UnitMeasurements::Length.defined?("metre")
290
- #=> false
262
+ UnitMeasurements::Length.defined?("m") #=> true
263
+ UnitMeasurements::Length.defined?("metre") #=> false
291
264
  ```
292
265
 
293
266
  **Check if the unit is a valid unit or alias within the unit group:**
294
267
 
295
268
  ```ruby
296
- UnitMeasurements::Length.unit_or_alias?("m")
297
- #=> true
298
- UnitMeasurements::Length.unit_or_alias?("metre")
299
- #=> true
269
+ UnitMeasurements::Length.unit_or_alias?("m") #=> true
270
+ UnitMeasurements::Length.unit_or_alias?("metre") #=> true
300
271
  ```
301
272
 
302
273
  **Clear cached data for the unit group:**
@@ -311,8 +282,7 @@ You have ability to compare the measurements with the same or different units wi
311
282
  For example, comparing length with length will work, comparing a length with a area would fail.
312
283
 
313
284
  ```ruby
314
- UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m")
315
- #=> true
285
+ UnitMeasurements::Length.parse("1 km") != UnitMeasurements::Length.parse("1 m") #=> true
316
286
  ```
317
287
 
318
288
  You can check supported comparisons along with their examples
@@ -326,10 +296,8 @@ measurement by either other measurement with compatible unit or numeric value.
326
296
  In cases of different units, the left hand side takes precedence.
327
297
 
328
298
  ```ruby
329
- UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m")
330
- #=> 1.001 km
331
- UnitMeasurements::Length.new(2, "km") * 2+2i
332
- #=> 4+2i km
299
+ UnitMeasurements::Length.new(1, "km") + UnitMeasurements::Length.new(1, "m") #=> 1.001 km
300
+ UnitMeasurements::Length.new(2, "km") * 2+2i #=> 4+2i km
333
301
  ```
334
302
 
335
303
  You can check supported arithmetic operations along with their examples
@@ -340,8 +308,7 @@ You can check supported arithmetic operations along with their examples
340
308
  You can perform mathematical functions on the measurements.
341
309
 
342
310
  ```ruby
343
- UnitMeasurements::Length.new(17.625, "m").round
344
- # => 18 m
311
+ UnitMeasurements::Length.new(17.625, "m").round #=> 18 m
345
312
  ```
346
313
 
347
314
  You can check supported mathematical functions along with their examples
@@ -353,8 +320,7 @@ You can convert measurement quantity directly to other numeric types viz.
353
320
  `Integer`, `BigDecimal`, `Rational`, `Complex`, and `Float`.
354
321
 
355
322
  ```ruby
356
- UnitMeasurements::Length.new(2.25567, "km").to_i
357
- #=> 2 km
323
+ UnitMeasurements::Length.new(2.25567, "km").to_i #=> 2 km
358
324
  ```
359
325
 
360
326
  You can check more about them along with their examples
@@ -484,13 +450,12 @@ Volume = UnitMeasurements::Volume
484
450
 
485
451
  ## Extras
486
452
 
487
- ### Numeric extension methods
453
+ ### Numeric methods
488
454
 
489
- The `.define_numeric_methods` method allows you to instantiate measurements in a
490
- manner similar to how `ActiveSupport::Duration` objects are created in Rails,
491
- providing a familiar syntax and functionality.
455
+ The `.define_numeric_methods` method allows you to define numeric methods that help you to initialize measurements in a
456
+ manner similar to how `ActiveSupport::Duration` objects are created in Rails, providing a familiar syntax and functionality.
492
457
 
493
- To define numeric extension methods for specific units within a unit group, use
458
+ To define numeric methods for specific units within the unit group, use
494
459
  the following syntax:
495
460
 
496
461
  ```ruby
@@ -500,21 +465,46 @@ UnitMeasurements::Length.define_numeric_methods("metre", "foot", "inch")
500
465
  This will enable the usage of these units as methods to instantiate and use measurements:
501
466
 
502
467
  ```ruby
503
- 1.m #=> Instantiate a measurement representing 1 metre.
504
- 5.feet #=> Instantiate a measurement representing 5 feet.
505
- 10.inches #=> Instantiate a measurement representing 10 inches.
506
- 1.foot == 12.inches #=> equality comparison between two measurements.
507
- 1.ft + 12.in #=> adds quantity of two measurements.
468
+ # Initialize a measurement
469
+ 1.m #=> 1 m
470
+ 5.feet #=> 5 ft
471
+ 10.inches #=> 10 in
472
+
473
+ # Usage
474
+ ## Equality comparison
475
+ 1.foot == 12.inches #=> true
476
+ ## Arithmetic operation
477
+ 1.ft + 12.in #=> 2.0 ft
478
+ ```
479
+
480
+ ### Conversion methods
481
+
482
+ The `.define_conversion_methods` method allows you to define conversion methods that
483
+ help you to easily convert measurements to a different unit.
484
+
485
+ To define conversion methods for specific units within the unit group, use the following syntax:
486
+
487
+ ```ruby
488
+ UnitMeasurements::Length.define_conversion_methods("metre", "foot", "inch")
489
+ ```
490
+
491
+ This will enable you to convert units as:
492
+
493
+ ```ruby
494
+ UnitMeasurements::Length.new(1, "ft").in_inches #=> 12.0 in
495
+ 12.in.in_foot #=> 1.0 ft
508
496
  ```
509
497
 
510
498
  ## Contributing
511
499
 
512
- 1. Fork it
513
- 2. Create your feature branch (`git checkout -b my-new-feature`)
500
+ Contributions to this project are welcomed! To contribute:
501
+
502
+ 1. Fork this repository
503
+ 2. Create a new branch (`git checkout -b my-new-feature`)
514
504
  3. Commit your changes (`git commit -am "Add some feature"`)
515
- 4. Push to the branch (`git push origin my-new-feature`)
516
- 5. Create new Pull Request
505
+ 4. Push the changes to your branch (`git push origin my-new-feature`)
506
+ 5. Create new **Pull Request**
517
507
 
518
508
  ## License
519
509
 
520
- Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
510
+ Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -152,6 +152,7 @@ end
152
152
 
153
153
  # The following requires load various components of the unit measurements library.
154
154
  require "unit_measurements/extras/numeric_methods"
155
+ require "unit_measurements/extras/conversion_methods"
155
156
 
156
157
  require "unit_measurements/configuration"
157
158
  require "unit_measurements/cache"
@@ -0,0 +1,87 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ # This module provides functionality to define conversion methods for a list
7
+ # of units within a unit group. If units are empty, it defaults to defining
8
+ # methods for all units in the unit group. These methods allow easy conversion
9
+ # between different units within a given unit group.
10
+ #
11
+ # This module is included in the +Measurement+ class to allow defining conversion
12
+ # methods for specified units.
13
+ #
14
+ # @see Measurement
15
+ #
16
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
17
+ # @since 5.15.0
18
+ module ConversionMethods
19
+ # @scope class
20
+ # Defines conversion methods for specified +units+ within the unit group.
21
+ # If +units+ are empty, it defaults to defining methods for all units within
22
+ # the unit group.
23
+ #
24
+ # @example Define conversion methods for metres, centimetres, and millimetres:
25
+ # UnitMeasurements::Length.define_conversion_methods("metres", :cm, :mm)
26
+ #
27
+ # @example Define conversion methods for all units within the unit group:
28
+ # UnitMeasurements::Length.define_conversion_methods
29
+ #
30
+ # @param [Array<String|Symbol>, optional] units
31
+ # An array of units' names for which conversion methods need to be defined.
32
+ # If empty, methods will be defined for all units within the unit group.
33
+ #
34
+ # @return [Array<Unit>]
35
+ # An array of units for which the conversion methods were defined.
36
+ #
37
+ # @note
38
+ # This method defines a conversion methods specifically for units that contain
39
+ # alphabetic characters in their names.
40
+ #
41
+ # @see .define_conversion_method_for
42
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
43
+ # @since 5.15.0
44
+ def define_conversion_methods(*units)
45
+ unit_group = self
46
+ units = units.empty? ? unit_group.units : units
47
+
48
+ units.inject([]) do |units, unit|
49
+ units << define_conversion_method_for(unit, unit_group)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # @private
56
+ # @scope class
57
+ # Defines conversion methods for a specific +unit+ within a +unit_group+.
58
+ # These methods are defined dynamically using +define_method+.
59
+ #
60
+ # @param [String|Symbol|Unit] unit
61
+ # The unit (or its name) for which the conversion methods need to be defined.
62
+ # @param [UnitGroup] unit_group The unit group to which the unit belongs.
63
+ #
64
+ # @return [Unit]
65
+ # The unit instance for which the conversion methods were defined.
66
+ #
67
+ # @see .define_conversion_methods
68
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
69
+ # @since 5.15.0
70
+ def define_conversion_method_for(unit, unit_group)
71
+ unit = unit.is_a?(Unit) ? unit : unit_group.unit_for!(unit)
72
+
73
+ unit.names.each do |method_name|
74
+ # Check if the name contains alphabetic characters
75
+ next unless method_name =~ /^[a-zA-Z]+$/
76
+
77
+ define_method("in_#{method_name}") do |use_cache: false|
78
+ convert_to(unit, use_cache: use_cache)
79
+ end
80
+ alias_method "to_#{method_name}", "in_#{method_name}"
81
+ alias_method "as_#{method_name}", "in_#{method_name}"
82
+ end
83
+
84
+ unit
85
+ end
86
+ end
87
+ end
@@ -3,12 +3,12 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- # This module provides methods to define +Numeric+ extension methods for a list
7
- # of units within a unit group. If units are empty, it defaults to defining
8
- # methods for all units in the unit group.
6
+ # This module provides methods to define +Numeric+ methods for a list of units
7
+ # within a unit group. If units are empty, it defaults to defining methods for
8
+ # all units in the unit group.
9
9
  #
10
10
  # This module is included in the +Measurement+ class to allow defining numeric
11
- # extension methods for specified units.
11
+ # methods for specified units.
12
12
  #
13
13
  # @see Measurement
14
14
  #
@@ -16,23 +16,27 @@ module UnitMeasurements
16
16
  # @since 5.14.0
17
17
  module NumericMethods
18
18
  # @scope class
19
- # Defines +Numeric+ extension methods for specified units within the unit
20
- # group. If units are empty, it defaults to defining methods for all units
21
- # within the unit group.
22
- #
23
- # @param [Array<String|Symbol>] units
24
- # An array of units' names for which numeric methods need to be defined.
25
- # If empty, methods will be defined for all units in the unit group.
26
- #
27
- # @return [Array<Unit>] An array of units for which methods are defined.
19
+ # Defines +Numeric+ methods for specified +units+ within the unit group. If
20
+ # +units+ are empty, it defaults to defining methods for all units within
21
+ # the unit group.
28
22
  #
29
23
  # @example Define numeric methods for metres, centimetres, and millimetres:
30
24
  # UnitMeasurements::Length.define_numeric_methods("metres", :cm, :mm)
31
25
  #
32
- # @example Define numeric methods for all units in the unit group:
26
+ # @example Define numeric methods for all units within the unit group:
33
27
  # UnitMeasurements::Length.define_numeric_methods
34
28
  #
35
- # @see #define_numeric_method_for
29
+ # @param [Array<String|Symbol>, optional] units
30
+ # An array of units' names for which numeric methods need to be defined.
31
+ # If empty, methods will be defined for all units within the unit group.
32
+ #
33
+ # @return [Array<Unit>] An array of units for which numeric methods were defined.
34
+ #
35
+ # @note
36
+ # This method defines a numeric methods specifically for units that contain
37
+ # alphabetic characters in their names.
38
+ #
39
+ # @see .define_numeric_method_for
36
40
  #
37
41
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
38
42
  # @since 5.14.0
@@ -49,17 +53,17 @@ module UnitMeasurements
49
53
 
50
54
  # @private
51
55
  # @scope class
52
- # This method defines a numeric method for a specific unit within a unit group.
53
- # The method is defined dynamically using +define_method+ and associates the
54
- # unit with the numeric value.
56
+ # Defines a numeric method for a specific +unit+ within a +unit_group+. The
57
+ # method is defined dynamically using +define_method+ and associates the unit
58
+ # with the numeric value.
55
59
  #
56
60
  # @param [String|Symbol|Unit] unit
57
- # The unit for which the numeric method is defined.
61
+ # The unit (or its name) for which the numeric method needs to be defined.
58
62
  # @param [UnitGroup] unit_group The unit group to which the unit belongs.
59
63
  #
60
64
  # @return [Unit] The unit instance for which the method was defined.
61
65
  #
62
- # @see #define_numeric_methods
66
+ # @see .define_numeric_methods
63
67
  #
64
68
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
65
69
  # @since 5.14.0
@@ -34,6 +34,7 @@ module UnitMeasurements
34
34
  include Math
35
35
 
36
36
  extend NumericMethods
37
+ extend ConversionMethods
37
38
 
38
39
  # Regular expression to match conversion strings.
39
40
  #
@@ -32,4 +32,8 @@ UnitMeasurements::Acceleration = UnitMeasurements.build do
32
32
  system :centimetre_gram_second do
33
33
  unit "Gal", value: "1e-2 m/s²", aliases: ["gal", "galileo"]
34
34
  end
35
+
36
+ system :planck_units do
37
+ unit "aₚ", value: "5.5608e+51 m/s²", aliases: ["planck acceleration", "quantum acceleration"]
38
+ end
35
39
  end
@@ -36,6 +36,10 @@ UnitMeasurements::Area = UnitMeasurements.build do
36
36
  unit "ch² (US)", value: "404.6873 m²", aliases: ["ch^2 (US)", "sq ch (US)", "square chain (US)", "square chains (US)"]
37
37
  end
38
38
 
39
+ system :planck_units do
40
+ unit "Aₚ", value: "2.6121e-70 m²", aliases: ["planck area", "quantum area"]
41
+ end
42
+
39
43
  unit "bd", value: "0.00774192 m²", aliases: ["board", "boards"]
40
44
  unit "ro", value: "1/4 ac", aliases: ["rood"]
41
45
  end
@@ -26,4 +26,8 @@ UnitMeasurements::Density = UnitMeasurements.build do
26
26
  unit "slug/ft³", value: "515.3788184 kg/m³", aliases: ["slug/ft^3", "slug per cubic foot", "slugs per cubic foot"]
27
27
  unit "slug/in³", value: "890574.582782 kg/m³", aliases: ["slug/in^3", "slug per cubic inch", "slugs per cubic inch"]
28
28
  end
29
+
30
+ system :planck_units do
31
+ unit "ρₚ", value: "5.1550e+96 kg/m³", aliases: ["planck density", "quantum density"]
32
+ end
29
33
  end
@@ -15,4 +15,8 @@ UnitMeasurements::ElectricCharge = UnitMeasurements.build do
15
15
  unit "abC", value: "10 C", aliases: ["abcoulomb", "abcoulombs"]
16
16
  unit "statC", value: "3.335641e-10 C", aliases: ["statcoulomb", "statcoulombs"]
17
17
  end
18
+
19
+ system :planck_units do
20
+ unit "qₚ", value: "1.875545956e-18 C", aliases: ["planck length", "quantum length"]
21
+ end
18
22
  end
@@ -14,4 +14,8 @@ UnitMeasurements::ElectricCurrent = UnitMeasurements.build do
14
14
  unit "abA", value: "10 A", aliases: ["abampere", "abamperes"]
15
15
  unit "statA", value: "3.33564e-10 A", aliases: ["statampere", "statamperes"]
16
16
  end
17
+
18
+ system :planck_units do
19
+ unit "Iₚ", value: "3.4789e+25 A", aliases: ["planck current", "quantum current"]
20
+ end
17
21
  end
@@ -46,6 +46,10 @@ UnitMeasurements::Energy = UnitMeasurements.build do
46
46
  unit "hp⋅h", value: "2.685e+13 erg", aliases: ["hp*h", "horsepower-hour"]
47
47
  end
48
48
 
49
+ system :planck_units do
50
+ unit "Eₚ", value: "1.9561e+9 J", aliases: ["planck energy", "quantum energy"]
51
+ end
52
+
49
53
  unit "Ry", value: "2.179872e-18 J", aliases: ["rydberg"]
50
54
  unit "Ha", value: "4.359744e-18 J", aliases: ["hartree", "atomic unit of energy"]
51
55
  unit "boe", value: "5.86152 GJ", aliases: ["barrel of oil equivalent", "barrels of oil equivalent"]
@@ -43,4 +43,8 @@ UnitMeasurements::Force = UnitMeasurements.build do
43
43
  unit "(lb⋅ft)/s²", value: "0.138254954376 N", aliases: ["(lb*ft)/s^2", "pound foot per second squared"]
44
44
  unit "(lb⋅in)/s²", value: "0.011521246198 N", aliases: ["(lb*in)/s^2", "pound inch per second squared"]
45
45
  end
46
+
47
+ system :planck_units do
48
+ unit "Fₚ", value: "1.2103e+44 N", aliases: ["planck force", "quantum force"]
49
+ end
46
50
  end
@@ -56,4 +56,8 @@ UnitMeasurements::Length = UnitMeasurements.build do
56
56
  unit "lm", value: "30 ld", aliases: ["light-month", "light-months"]
57
57
  unit "ly", value: "365.25 ld", aliases: ["light-year", "light-years"]
58
58
  end
59
+
60
+ system :planck_units do
61
+ unit "lₚ", value: "1.616255e-35 m", aliases: ["planck length", "quantum length"]
62
+ end
59
63
  end
@@ -49,5 +49,9 @@ UnitMeasurements::Power = UnitMeasurements.build do
49
49
  unit "erg/s", value: "1e-7 W", aliases: ["erg per second", "ergs per second"]
50
50
  end
51
51
 
52
+ system :planck_units do
53
+ unit "Pₚ", value: "3.6283e+52 W", aliases: ["planck power", "quantum power"]
54
+ end
55
+
52
56
  unit "lusec", value: "133.3224 μW", aliases: ["lusecs"]
53
57
  end
@@ -56,4 +56,8 @@ UnitMeasurements::Pressure = UnitMeasurements.build do
56
56
  system :gravitational_metric do
57
57
  unit "at", value: "98066.5 Pa", aliases: ["technical atmosphere", "technical atmospheres"]
58
58
  end
59
+
60
+ system :planck_units do
61
+ unit "Pₚ", value: "4.63309e+113 Pa", aliases: ["planck pressure", "quantum pressure"]
62
+ end
59
63
  end
@@ -12,4 +12,8 @@ UnitMeasurements::Temperature = UnitMeasurements.build do
12
12
  system :english_engineering do
13
13
  unit "°R", value: "5/9 K", aliases: ["R", "°Ra", "Ra", "rankine"]
14
14
  end
15
+
16
+ system :planck_units do
17
+ unit "Tₚ", value: "1.416784e+32 K", aliases: ["planck temperature", "quantum temperature"]
18
+ end
15
19
  end
@@ -13,6 +13,10 @@ UnitMeasurements::Time = UnitMeasurements.build do
13
13
  unit "min", value: "60 s", aliases: ["minute", "minutes"]
14
14
  end
15
15
 
16
+ system :planck_units do
17
+ unit "tₚ", value: "5.391247e-44 s", aliases: ["planck time", "quantum time"]
18
+ end
19
+
16
20
  unit "wk", value: "7 d", aliases: ["week", "weeks"]
17
21
  unit "mo", value: "30.4167 d", aliases: ["month", "months"]
18
22
  unit "yr", value: "365 d", aliases: ["y", "year", "years"]
@@ -57,4 +57,8 @@ UnitMeasurements::Volume = UnitMeasurements.build do
57
57
 
58
58
  unit "cd-ft", value: "1/8 cd", aliases: ["cord-foot", "cord-feet"]
59
59
  end
60
+
61
+ system :planck_units do
62
+ unit "Vₚ", value: "4.2217e-105 m³", aliases: ["planck volume", "quantum volume"]
63
+ end
60
64
  end
@@ -45,4 +45,8 @@ UnitMeasurements::Weight = UnitMeasurements.build do
45
45
  system :foot_pound_second do
46
46
  unit "slug", value: "32.17404856 lb", aliases: ["slugs"]
47
47
  end
48
+
49
+ system :planck_units do
50
+ unit "mₚ", value: "2.176434e-8 kg", aliases: ["planck mass", "quantum mass"]
51
+ end
48
52
  end
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.14.0"
7
+ VERSION = "5.16.0"
8
8
  end
data/units.md CHANGED
@@ -49,6 +49,7 @@ These units are defined in `UnitMeasurements::Length`.
49
49
  | 30 | lmin | light-minute, light-minutes |
50
50
  | 31 | cb (M) | CBL. (M), cable length (M) |
51
51
  | 32 | cb (US) | CBL. (US), cable length (US) |
52
+ | 33 | lₚ | planck length, quantum length |
52
53
 
53
54
  ## 2. Weight or mass
54
55
 
@@ -78,6 +79,7 @@ These units are defined in `UnitMeasurements::Weight`.
78
79
  | 20 | cwt | hundredweight, long hundredweight, imperial hundredweight |
79
80
  | 21 | slug | slugs |
80
81
  | 22 | cwt (US) | hundredweight (US), short hundredweight |
82
+ | 23 | mₚ | planck mass, quantum mass |
81
83
 
82
84
  ## 3. Time or duration
83
85
 
@@ -96,6 +98,7 @@ These units are defined in `UnitMeasurements::Time`.
96
98
  | 9 | qtr | quarter, quarters |
97
99
  | 10 | dec | decade, decades |
98
100
  | 11 | cent | century, centuries |
101
+ | 12 | tₚ | planck time, quantum time |
99
102
 
100
103
  ## 4. Amount of substance
101
104
 
@@ -116,6 +119,7 @@ These units are defined in `UnitMeasurements::ElectricCurrent`.
116
119
  | 2 | abA | abampere, abamperes |
117
120
  | 3 | Bi | biot, biots |
118
121
  | 4 | statA | statampere, statamperes |
122
+ | 5 | Iₚ | planck current, quantum current |
119
123
 
120
124
  ## 6. Luminous intensity
121
125
 
@@ -134,6 +138,7 @@ These units are defined in `UnitMeasurements::Temperature`.
134
138
  |:--|:--|:--|
135
139
  | _1_ | _K*_ | _kelvin, kelvins_ |
136
140
  | 2 | °R | R, °Ra, Ra, rankine |
141
+ | 3 | Tₚ | planck temperature, quantum temperature |
137
142
 
138
143
  ## 8. Area
139
144
 
@@ -161,6 +166,7 @@ These units are defined in `UnitMeasurements::Area`.
161
166
  | 18 | ft² (US) | ft^2 (US), sq ft (US), square foot (US), square feet (US) |
162
167
  | 19 | mi² (US) | mi^2 (US), sq mi (US), square mile (US), square miles (US) |
163
168
  | 20 | ch² (US) | ch^2 (US), sq ch (US), square chain (US), square chains (US) |
169
+ | 21 | Aₚ | planck area, quantum area |
164
170
 
165
171
  ## 9. Volume & capacity
166
172
 
@@ -203,6 +209,7 @@ These units are defined in `UnitMeasurements::Volume`.
203
209
  | 33 | ac⋅ft | acre-foot, acre-feet |
204
210
  | 34 | ac⋅in | acre-inch, acre-inches |
205
211
  | 35 | cd-ft | cord-foot, cord-feet |
212
+ | 36 | Vₚ | planck volume, quantum volume |
206
213
 
207
214
  ## 10. Density
208
215
 
@@ -223,6 +230,7 @@ These units are defined in `UnitMeasurements::Density`.
223
230
  | 11 | lb/gal | pound per gallon, pounds per gallon |
224
231
  | 12 | slug/ft³ | slug/ft^3, slug per cubic foot, slugs per cubic foot |
225
232
  | 13 | slug/in³ | slug/in^3, slug per cubic inch, slugs per cubic inch |
233
+ | 14 | ρₚ | planck density, quantum density |
226
234
 
227
235
  ## 11. Quantity
228
236
 
@@ -302,7 +310,8 @@ These units are defined in `UnitMeasurements::Force`.
302
310
  | 14 | kipf | kip, klbf, kip-force |
303
311
  | 15 | (lb⋅m)/s² | (lb*m)/s^2, pound meter per second squared, pound metre per second squared |
304
312
  | 16 | (lb⋅ft)/s² | (lb*ft)/s^2, pound foot per second squared |
305
- | 17 | (lb⋅in)/s² | (lb*in)/s^2, pound inch per second squared |
313
+ | 17 | (lb⋅in)/s² | (lb\*in)/s^2, pound inch per second squared |
314
+ | 18 | Fₚ | planck force, quantum force |
306
315
 
307
316
  ## 16. Velocity or speed
308
317
 
@@ -343,6 +352,7 @@ These units are defined in `UnitMeasurements::Acceleration`.
343
352
  | 10 | mpm/s | mi/(min⋅s), mile per minute per second, miles per minute per second |
344
353
  | 11 | mph/s | mi/(h⋅s), mile per hour per second, miles per hour per second |
345
354
  | 12 | Kn/s | knot per second, knots per second |
355
+ | 13 | aₚ | planck acceleration, quantum acceleration |
346
356
 
347
357
  ## 18. Angular velocity or rotational speed
348
358
 
@@ -396,6 +406,7 @@ These units are defined in `UnitMeasurements::ElectricCharge`.
396
406
  | 3 | statC | statcoulomb, statcoulombs |
397
407
  | 4 | Fr | franklin, franklins |
398
408
  | 5 | Fd | faraday, faradays |
409
+ | 6 | qₚ | planck charge, quantum charge |
399
410
 
400
411
  ## 22. Electrical capacitance
401
412
 
@@ -541,6 +552,7 @@ These units are defined in `UnitMeasurements::Pressure`.
541
552
  | 25 | kgf/m² | kgf/m^2, kilogram-force per square metre, kilogramme-force per square metre |
542
553
  | 26 | kgf/mm² | kgf/mm^2, kilogram-force per square millimetre, kilogramme-force per square millimetre |
543
554
  | 27 | kgf/dm² | kgf/dm^2, kilogram-force per square decimetre, kilogramme-force per square decimetre |
555
+ | 28 | Pₚ | planck pressure, quantum pressure |
544
556
 
545
557
  ## 34. Torque or moment of force
546
558
 
@@ -639,7 +651,8 @@ These units are defined in `UnitMeasurements::Power`.
639
651
  | 22 | lusec | lusecs |
640
652
  | 23 | atm⋅ft³/s | atm*ft^3/s, atmosphere-cubic foot per second |
641
653
  | 24 | atm⋅ft³/min | atm*ft^3/min, atmosphere-cubic foot per minute |
642
- | 25 | atm⋅ft³/h | atm*ft^3/h, atmosphere-cubic foot per hour |
654
+ | 25 | atm⋅ft³/h | atm\*ft^3/h, atmosphere-cubic foot per hour |
655
+ | 26 | Pₚ | planck power, quantum power |
643
656
 
644
657
  ## 40. Energy
645
658
 
@@ -671,6 +684,7 @@ These units are defined in `UnitMeasurements::Energy`.
671
684
  | 22 | in⋅lbf | in\*lbf, in⋅lb, in\*lb, inch-pound force |
672
685
  | 23 | ft⋅pdl | ft*pdl, foot-poundal |
673
686
  | 24 | gal⋅atm | gal*atm, gallon-atmosphere |
687
+ | 25 | Eₚ | planck energy, quantum energy |
674
688
 
675
689
  ## 41. Dynamic viscosity
676
690
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unit_measurements
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.14.0
4
+ version: 5.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-29 00:00:00.000000000 Z
11
+ date: 2023-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -118,6 +118,7 @@ files:
118
118
  - lib/unit_measurements/errors/primitive_unit_already_set_error.rb
119
119
  - lib/unit_measurements/errors/unit_already_defined_error.rb
120
120
  - lib/unit_measurements/errors/unit_error.rb
121
+ - lib/unit_measurements/extras/conversion_methods.rb
121
122
  - lib/unit_measurements/extras/numeric_methods.rb
122
123
  - lib/unit_measurements/formatter.rb
123
124
  - lib/unit_measurements/math.rb