unit_measurements 5.14.0 → 5.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +1 -1
- data/README.md +43 -19
- data/lib/unit_measurements/base.rb +1 -0
- data/lib/unit_measurements/extras/conversion_methods.rb +87 -0
- data/lib/unit_measurements/extras/numeric_methods.rb +24 -20
- data/lib/unit_measurements/measurement.rb +1 -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: 9e89ba7ff921595afff359baeafb673b3d1d08d65293aefb4efc3082b99bfad3
|
4
|
+
data.tar.gz: ce3695641b050fa395920124859257d2757707cbdda8b8cc42feaa64a75363bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b4085c191ebe55c90a272c63ba47c0d54860af42278860f3e30096060a0ebfa34b20a5b81ee57c35327e3f3336617193bacfdab7b76be0361b75d4ebd665f75
|
7
|
+
data.tar.gz: a2fe5a6c2f58534a2a688bca5140927906e4c10035dd1e05a4c1b40686a61dc90e2afa1c5929b5a25041101c9327fbb5e6e2e1c937fe025e433bea21b3e99507
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
+
## [5.15.0](https://github.com/shivam091/unit_measurements/compare/v5.14.0...v5.15.0) - 2023-12-01
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added `.define_conversion_methods` method to define conversion helper methods for units.
|
6
|
+
|
7
|
+
----------
|
8
|
+
|
1
9
|
## [5.14.0](https://github.com/shivam091/unit_measurements/compare/v5.13.0...v5.14.0) - 2023-11-29
|
2
10
|
|
3
11
|
### What's new
|
4
12
|
|
5
|
-
- Added `.define_numeric_methods`
|
13
|
+
- Added `.define_numeric_methods` method to define numeric extension methods for units.
|
6
14
|
|
7
15
|
----------
|
8
16
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -43,11 +43,12 @@ 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
|
-
|
50
|
+
To use `unit_measurements` in your Rails application, add the
|
51
|
+
following line to your Gemfile:
|
51
52
|
|
52
53
|
```ruby
|
53
54
|
gem "unit_measurements"
|
@@ -269,8 +270,7 @@ UnitMeasurements::Length.units_for("metric")
|
|
269
270
|
**Finding units within the unit group:**
|
270
271
|
|
271
272
|
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
|
273
|
-
in the unit group.
|
273
|
+
within the unit group. `#unit_for!` method returns an error if a unit system is not defined within the unit group.
|
274
274
|
|
275
275
|
```ruby
|
276
276
|
UnitMeasurements::Length.unit_for("m")
|
@@ -484,13 +484,12 @@ Volume = UnitMeasurements::Volume
|
|
484
484
|
|
485
485
|
## Extras
|
486
486
|
|
487
|
-
### Numeric
|
487
|
+
### Numeric methods
|
488
488
|
|
489
|
-
The `.define_numeric_methods` method allows you to
|
490
|
-
manner similar to how `ActiveSupport::Duration` objects are created in Rails,
|
491
|
-
providing a familiar syntax and functionality.
|
489
|
+
The `.define_numeric_methods` method allows you to define numeric methods that help you to initialize measurements in a
|
490
|
+
manner similar to how `ActiveSupport::Duration` objects are created in Rails, providing a familiar syntax and functionality.
|
492
491
|
|
493
|
-
To define numeric
|
492
|
+
To define numeric methods for specific units within the unit group, use
|
494
493
|
the following syntax:
|
495
494
|
|
496
495
|
```ruby
|
@@ -500,21 +499,46 @@ UnitMeasurements::Length.define_numeric_methods("metre", "foot", "inch")
|
|
500
499
|
This will enable the usage of these units as methods to instantiate and use measurements:
|
501
500
|
|
502
501
|
```ruby
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
502
|
+
# Initialize a measurement
|
503
|
+
1.m #=> 1 m
|
504
|
+
5.feet #=> 5 ft
|
505
|
+
10.inches #=> 10 in
|
506
|
+
|
507
|
+
# Usage
|
508
|
+
## Equality comparison
|
509
|
+
1.foot == 12.inches #=> true
|
510
|
+
## Arithmetic operation
|
511
|
+
1.ft + 12.in #=> 2.0 ft
|
512
|
+
```
|
513
|
+
|
514
|
+
### Conversion methods
|
515
|
+
|
516
|
+
The `.define_conversion_methods` method allows you to define conversion methods that
|
517
|
+
help you to easily convert measurements to a different unit.
|
518
|
+
|
519
|
+
To define conversion methods for specific units within the unit group, use the following syntax:
|
520
|
+
|
521
|
+
```ruby
|
522
|
+
UnitMeasurements::Length.define_conversion_methods("metre", "foot", "inch")
|
523
|
+
```
|
524
|
+
|
525
|
+
This will enable you to convert units as:
|
526
|
+
|
527
|
+
```ruby
|
528
|
+
UnitMeasurements::Length.new(1, "ft").in_inches #=> 12.0 in
|
529
|
+
12.in.in_foot #=> 1.0 ft
|
508
530
|
```
|
509
531
|
|
510
532
|
## Contributing
|
511
533
|
|
512
|
-
|
513
|
-
|
534
|
+
Contributions to this project are welcomed! To contribute:
|
535
|
+
|
536
|
+
1. Fork this repository
|
537
|
+
2. Create a new branch (`git checkout -b my-new-feature`)
|
514
538
|
3. Commit your changes (`git commit -am "Add some feature"`)
|
515
|
-
4. Push to
|
516
|
-
5. Create new Pull Request
|
539
|
+
4. Push the changes to your branch (`git push origin my-new-feature`)
|
540
|
+
5. Create new **Pull Request**
|
517
541
|
|
518
542
|
## License
|
519
543
|
|
520
|
-
Copyright 2023 [Harshal V. LADHE](
|
544
|
+
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+
|
7
|
-
#
|
8
|
-
#
|
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
|
-
#
|
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+
|
20
|
-
#
|
21
|
-
#
|
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
|
26
|
+
# @example Define numeric methods for all units within the unit group:
|
33
27
|
# UnitMeasurements::Length.define_numeric_methods
|
34
28
|
#
|
35
|
-
# @
|
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
|
-
#
|
53
|
-
#
|
54
|
-
#
|
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
|
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
|
66
|
+
# @see .define_numeric_methods
|
63
67
|
#
|
64
68
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
65
69
|
# @since 5.14.0
|
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.15.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-12-11 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
|