unit_measurements 5.13.0 → 5.14.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/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +25 -0
- data/lib/unit_measurements/base.rb +2 -0
- data/lib/unit_measurements/extras/numeric_methods.rb +81 -0
- data/lib/unit_measurements/measurement.rb +3 -0
- data/lib/unit_measurements/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d59abc6cd3128c515be37c2e7bcc40cbbaa73530a4a5ed1ce6d5e7093202e063
|
4
|
+
data.tar.gz: 22091e2ce789b4a064fb0f06fb704805b69cc86601334ab4d9f8a7afe9c6a0fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e16e3e25e06d155664fab616cf4e3147d75cf4aef71c15c6312572418fd124d11436c402fbe7e83e984e483c737a073ad180947f0ee071fab170a59c8f880a4
|
7
|
+
data.tar.gz: b6e6bd19810a115b48d7107859b82e21d8b5dc231455d8a995a9ea5157c44d38c53c287a15945a342f1b376fcfe80b539de29ad1be9ca112e5750ac866013cbc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## [5.14.0](https://github.com/shivam091/unit_measurements/compare/v5.13.0...v5.14.0) - 2023-11-29
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added `.define_numeric_methods` support to define numeric extension methods for units.
|
6
|
+
|
7
|
+
----------
|
8
|
+
|
1
9
|
## [5.13.0](https://github.com/shivam091/unit_measurements/compare/v5.12.0...v5.13.0) - 2023-11-27
|
2
10
|
|
3
11
|
### What's new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -482,6 +482,31 @@ Length = UnitMeasurements::Length
|
|
482
482
|
Volume = UnitMeasurements::Volume
|
483
483
|
```
|
484
484
|
|
485
|
+
## Extras
|
486
|
+
|
487
|
+
### Numeric extension methods
|
488
|
+
|
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.
|
492
|
+
|
493
|
+
To define numeric extension methods for specific units within a unit group, use
|
494
|
+
the following syntax:
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
UnitMeasurements::Length.define_numeric_methods("metre", "foot", "inch")
|
498
|
+
```
|
499
|
+
|
500
|
+
This will enable the usage of these units as methods to instantiate and use measurements:
|
501
|
+
|
502
|
+
```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.
|
508
|
+
```
|
509
|
+
|
485
510
|
## Contributing
|
486
511
|
|
487
512
|
1. Fork it
|
@@ -151,6 +151,8 @@ module UnitMeasurements
|
|
151
151
|
end
|
152
152
|
|
153
153
|
# The following requires load various components of the unit measurements library.
|
154
|
+
require "unit_measurements/extras/numeric_methods"
|
155
|
+
|
154
156
|
require "unit_measurements/configuration"
|
155
157
|
require "unit_measurements/cache"
|
156
158
|
require "unit_measurements/unit_group_builder"
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
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.
|
9
|
+
#
|
10
|
+
# This module is included in the +Measurement+ class to allow defining numeric
|
11
|
+
# extension methods for specified units.
|
12
|
+
#
|
13
|
+
# @see Measurement
|
14
|
+
#
|
15
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
16
|
+
# @since 5.14.0
|
17
|
+
module NumericMethods
|
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.
|
28
|
+
#
|
29
|
+
# @example Define numeric methods for metres, centimetres, and millimetres:
|
30
|
+
# UnitMeasurements::Length.define_numeric_methods("metres", :cm, :mm)
|
31
|
+
#
|
32
|
+
# @example Define numeric methods for all units in the unit group:
|
33
|
+
# UnitMeasurements::Length.define_numeric_methods
|
34
|
+
#
|
35
|
+
# @see #define_numeric_method_for
|
36
|
+
#
|
37
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
38
|
+
# @since 5.14.0
|
39
|
+
def define_numeric_methods(*units)
|
40
|
+
unit_group = self
|
41
|
+
units = units.empty? ? unit_group.units : units
|
42
|
+
|
43
|
+
units.inject([]) do |units, unit|
|
44
|
+
units << define_numeric_method_for(unit, unit_group)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# @private
|
51
|
+
# @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.
|
55
|
+
#
|
56
|
+
# @param [String|Symbol|Unit] unit
|
57
|
+
# The unit for which the numeric method is defined.
|
58
|
+
# @param [UnitGroup] unit_group The unit group to which the unit belongs.
|
59
|
+
#
|
60
|
+
# @return [Unit] The unit instance for which the method was defined.
|
61
|
+
#
|
62
|
+
# @see #define_numeric_methods
|
63
|
+
#
|
64
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
65
|
+
# @since 5.14.0
|
66
|
+
def define_numeric_method_for(unit, unit_group)
|
67
|
+
unit = unit.is_a?(Unit) ? unit : unit_group.unit_for!(unit)
|
68
|
+
|
69
|
+
unit.names.each do |method_name|
|
70
|
+
# Check if the name contains alphabetic characters
|
71
|
+
next unless method_name =~ /^[a-zA-Z]+$/
|
72
|
+
|
73
|
+
Numeric.define_method(method_name) do
|
74
|
+
unit_group.new(self, unit)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
unit
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -23,6 +23,7 @@ module UnitMeasurements
|
|
23
23
|
# @see Conversion
|
24
24
|
# @see Formatter
|
25
25
|
# @see Math
|
26
|
+
# @see NumericMethods
|
26
27
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
27
28
|
# @since 1.0.0
|
28
29
|
class Measurement
|
@@ -32,6 +33,8 @@ module UnitMeasurements
|
|
32
33
|
include Formatter
|
33
34
|
include Math
|
34
35
|
|
36
|
+
extend NumericMethods
|
37
|
+
|
35
38
|
# Regular expression to match conversion strings.
|
36
39
|
#
|
37
40
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
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.
|
4
|
+
version: 5.14.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-
|
11
|
+
date: 2023-11-29 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/numeric_methods.rb
|
121
122
|
- lib/unit_measurements/formatter.rb
|
122
123
|
- lib/unit_measurements/math.rb
|
123
124
|
- lib/unit_measurements/measurement.rb
|