unit_measurements 5.13.0 → 5.15.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: 98def91ad4bff4c38e579a967b82ae5467e52db044246e6c91d00ccee9ea8070
4
- data.tar.gz: 5e7c0aca97692daf71dc1923aabfca63f99ccfa0e246e1ec83665c8f821eb784
3
+ metadata.gz: 9e89ba7ff921595afff359baeafb673b3d1d08d65293aefb4efc3082b99bfad3
4
+ data.tar.gz: ce3695641b050fa395920124859257d2757707cbdda8b8cc42feaa64a75363bc
5
5
  SHA512:
6
- metadata.gz: e9a2f7bb789161cdd305eeec424d8b60eaf50c0ce6daa6868699c872ed5abf6bd06ba7fb9d5ae6cdb2d9021c837081553b984d29fb67b75c03af46e1f0b36fd2
7
- data.tar.gz: 6098f3468240de037f7a39ec5f0a4ea8e431379523bee2da921f5dbe17368ea772d545e41e187341dfb48749168895a3d24d2a89c70e0b10dc18d9fe2656932a
6
+ metadata.gz: 8b4085c191ebe55c90a272c63ba47c0d54860af42278860f3e30096060a0ebfa34b20a5b81ee57c35327e3f3336617193bacfdab7b76be0361b75d4ebd665f75
7
+ data.tar.gz: a2fe5a6c2f58534a2a688bca5140927906e4c10035dd1e05a4c1b40686a61dc90e2afa1c5929b5a25041101c9327fbb5e6e2e1c937fe025e433bea21b3e99507
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
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
+
9
+ ## [5.14.0](https://github.com/shivam091/unit_measurements/compare/v5.13.0...v5.14.0) - 2023-11-29
10
+
11
+ ### What's new
12
+
13
+ - Added `.define_numeric_methods` method to define numeric extension methods for units.
14
+
15
+ ----------
16
+
1
17
  ## [5.13.0](https://github.com/shivam091/unit_measurements/compare/v5.12.0...v5.13.0) - 2023-11-27
2
18
 
3
19
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.13.0)
4
+ unit_measurements (5.15.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
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
- If using bundler, first add this line to your application's Gemfile:
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 present
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")
@@ -482,14 +482,63 @@ Length = UnitMeasurements::Length
482
482
  Volume = UnitMeasurements::Volume
483
483
  ```
484
484
 
485
+ ## Extras
486
+
487
+ ### Numeric methods
488
+
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.
491
+
492
+ To define numeric methods for specific units within the unit group, use
493
+ the following syntax:
494
+
495
+ ```ruby
496
+ UnitMeasurements::Length.define_numeric_methods("metre", "foot", "inch")
497
+ ```
498
+
499
+ This will enable the usage of these units as methods to instantiate and use measurements:
500
+
501
+ ```ruby
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
530
+ ```
531
+
485
532
  ## Contributing
486
533
 
487
- 1. Fork it
488
- 2. Create your feature branch (`git checkout -b my-new-feature`)
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`)
489
538
  3. Commit your changes (`git commit -am "Add some feature"`)
490
- 4. Push to the branch (`git push origin my-new-feature`)
491
- 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**
492
541
 
493
542
  ## License
494
543
 
495
- Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
544
+ Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -151,6 +151,9 @@ 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
+ require "unit_measurements/extras/conversion_methods"
156
+
154
157
  require "unit_measurements/configuration"
155
158
  require "unit_measurements/cache"
156
159
  require "unit_measurements/unit_group_builder"
@@ -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
@@ -0,0 +1,85 @@
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+ 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
+ #
10
+ # This module is included in the +Measurement+ class to allow defining numeric
11
+ # 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+ 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.
22
+ #
23
+ # @example Define numeric methods for metres, centimetres, and millimetres:
24
+ # UnitMeasurements::Length.define_numeric_methods("metres", :cm, :mm)
25
+ #
26
+ # @example Define numeric methods for all units within the unit group:
27
+ # UnitMeasurements::Length.define_numeric_methods
28
+ #
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
40
+ #
41
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
42
+ # @since 5.14.0
43
+ def define_numeric_methods(*units)
44
+ unit_group = self
45
+ units = units.empty? ? unit_group.units : units
46
+
47
+ units.inject([]) do |units, unit|
48
+ units << define_numeric_method_for(unit, unit_group)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ # @private
55
+ # @scope class
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.
59
+ #
60
+ # @param [String|Symbol|Unit] unit
61
+ # The unit (or its name) for which the numeric method needs to be defined.
62
+ # @param [UnitGroup] unit_group The unit group to which the unit belongs.
63
+ #
64
+ # @return [Unit] The unit instance for which the method was defined.
65
+ #
66
+ # @see .define_numeric_methods
67
+ #
68
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
69
+ # @since 5.14.0
70
+ def define_numeric_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
+ Numeric.define_method(method_name) do
78
+ unit_group.new(self, unit)
79
+ end
80
+ end
81
+
82
+ unit
83
+ end
84
+ end
85
+ 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,9 @@ module UnitMeasurements
32
33
  include Formatter
33
34
  include Math
34
35
 
36
+ extend NumericMethods
37
+ extend ConversionMethods
38
+
35
39
  # Regular expression to match conversion strings.
36
40
  #
37
41
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.13.0"
7
+ VERSION = "5.15.0"
8
8
  end
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.13.0
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-27 00:00:00.000000000 Z
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,8 @@ 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
122
+ - lib/unit_measurements/extras/numeric_methods.rb
121
123
  - lib/unit_measurements/formatter.rb
122
124
  - lib/unit_measurements/math.rb
123
125
  - lib/unit_measurements/measurement.rb