unit_measurements 5.11.0 → 5.12.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: ca26bc8965c93c692a72e788ab2577f2bad297909372a72d3cc2fec93a45b5bd
4
- data.tar.gz: 581d716cb56cf59c89eeb69d01095c4c0c14054006dc6bb0822580e3d6750ffd
3
+ metadata.gz: e01978467f274548e8cf792d9b7bfe26ab85155a2c099482cb8b4017641ed900
4
+ data.tar.gz: d09990112a19291527059e822906a802a051541b654babad13e97110c5557dea
5
5
  SHA512:
6
- metadata.gz: ed994225ecb853f8ce577e0b53a863b0f091980ee33f054b50f69d156ad0a7e393a4a59d962d665aa52fa6cdc077f19f42bc27d0b1d8e7413f359dbde8e6a9fb
7
- data.tar.gz: 889e45c784390c89a46079067ca9493253e638b1117ee88c16c5ab2ed60db89a855a11bf487f2a6536b1729a2704983fbfcb5fa60959a2b8eed79dae4957d920
6
+ metadata.gz: 0faa94f0a962f19134ff58276c6c876a30a81817172039c4261eaa9a15eeb61dd7047f61b96168c2115c4ba40711fee42cb4ceb832c6e6640bd9c8e7e0f4343b
7
+ data.tar.gz: d9213995299feeb51dda98cc4dbbd3bd76f6c4782a8e04b9612cc7f3134b192c74aa80b027779c1b8c862358a84a8c40469753a086ceb94fd5b5ee2b8d33e973
data/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ ## [5.12.0](https://github.com/shivam091/unit_measurements/compare/v5.11.1...v5.12.0) - 2023-11-25
2
+
3
+ ### What's new
4
+
5
+ - Added `MissingPrimitiveUnitError` error if user tries to convert measurement to
6
+ the primitive unit but it's not set for the unit group.
7
+ - Aliased `#+` method as `#add`.
8
+ - Aliased `#-` method as `#subtract`.
9
+ - Aliased `#*` method as `#times` and `#multiply`.
10
+ - Aliased `#/` method as `#divide`.
11
+
12
+ ----------
13
+
14
+ ## [5.11.1](https://github.com/shivam091/unit_measurements/compare/v5.11.0...v5.11.1) - 2023-11-16
15
+
16
+ ### What's changed
17
+
18
+ - Moved `BaseError` to base file for removing uninitialized constant error in reverse dependencies viz.
19
+ [unit_measurements-rails](https://rubygems.org/gems/unit_measurements-rails) and
20
+ [composite_unit_measurements](https://rubygems.org/gems/composite_unit_measurements).
21
+
22
+ ----------
23
+
1
24
  ## [5.11.0](https://github.com/shivam091/unit_measurements/compare/v5.10.0...v5.11.0) - 2023-11-11
2
25
 
3
26
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.11.0)
4
+ unit_measurements (5.12.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
@@ -41,6 +41,7 @@ module UnitMeasurements
41
41
  def +(other)
42
42
  arithmetic_operation(other, :+)
43
43
  end
44
+ alias_method :add, :+
44
45
 
45
46
  # Subtracts the quantity of the other measurement or a numeric value from the
46
47
  # quantity of the current measurement.
@@ -62,6 +63,7 @@ module UnitMeasurements
62
63
  def -(other)
63
64
  arithmetic_operation(other, :-)
64
65
  end
66
+ alias_method :subtract, :-
65
67
 
66
68
  # Multiplies the quantity of the current measurement by the quantity of the
67
69
  # other measurement or a numeric value.
@@ -84,6 +86,8 @@ module UnitMeasurements
84
86
  arithmetic_operation(other, :*)
85
87
  end
86
88
  alias_method :scale, :*
89
+ alias_method :times, :*
90
+ alias_method :multiply, :*
87
91
 
88
92
  # Divides the quantity of the current measurement by the quantity of the other
89
93
  # measurement or a numeric value.
@@ -105,6 +109,7 @@ module UnitMeasurements
105
109
  def /(other)
106
110
  arithmetic_operation(other, :/)
107
111
  end
112
+ alias_method :divide, :/
108
113
 
109
114
  # Raises the quantity of the current measurement to the power of the quantity of
110
115
  # the other measurement or numeric value.
@@ -5,7 +5,28 @@
5
5
  require "active_support/all"
6
6
  require "unit_measurements/version"
7
7
 
8
+ # The +UnitMeasurements+ module serves as a container for various functionalities
9
+ # related to unit measurements. It provides methods for creating custom unit
10
+ # groups, defining units, performing arithmetic operations, comparison between
11
+ # measurements, conversions, normalization of input strings, parsing measurements
12
+ # from strings, and more. It is a fundamental part of the unit measurements library.
13
+ #
14
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
15
+ # @since 0.1.0
8
16
  module UnitMeasurements
17
+ # This is the base class for custom errors in the +UnitMeasurements+ module.
18
+ #
19
+ # @see UnitError
20
+ # @see ParseError
21
+ # @see BlankUnitError
22
+ # @see BlankQuantityError
23
+ # @see UnitAlreadyDefinedError
24
+ # @see MissingPrimitiveUnitError
25
+ # @see PrimitiveUnitAlreadySetError
26
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
27
+ # @since 1.1.0
28
+ class BaseError < StandardError; end
29
+
9
30
  class << self
10
31
  # Allows setting an instance of +Configuration+ containing values of desired
11
32
  # configurable options.
@@ -150,3 +171,4 @@ require "unit_measurements/errors/unit_already_defined_error"
150
171
  require "unit_measurements/errors/primitive_unit_already_set_error"
151
172
  require "unit_measurements/errors/blank_quantity_error"
152
173
  require "unit_measurements/errors/blank_unit_error"
174
+ require "unit_measurements/errors/missing_primitive_unit_error"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module UnitMeasurements
6
+ # The +UnitMeasurements::MissingPrimitiveUnitError+ class represents an error
7
+ # that occurs when the primitive unit is not set for a unit group.
8
+ #
9
+ # This error is raised when a user attempts to convert a measurement to the
10
+ # primitive unit of a unit group that does not have a primitive unit defined.
11
+ #
12
+ # @see BaseError
13
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
14
+ # @since 5.12.0
15
+ class MissingPrimitiveUnitError < BaseError
16
+ # Initializes a new +MissingPrimitiveUnitError+ instance.
17
+ #
18
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
19
+ # @since 5.12.0
20
+ def initialize
21
+ super("The primitive unit is not set for the unit group.")
22
+ end
23
+ end
24
+ end
@@ -147,11 +147,17 @@ module UnitMeasurements
147
147
  # A new +Measurement+ instance with the converted +quantity+ and
148
148
  # +target unit+.
149
149
  #
150
+ # @raise [MissingPrimitiveUnitError]
151
+ # if primitive unit is not set for the unit group.
152
+ #
150
153
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
151
154
  # @since 1.0.0
152
155
  def convert_to(target_unit, use_cache: false)
153
156
  target_unit = if target_unit.to_s.eql?("primitive")
154
- self.class.unit_group.primitive
157
+ primitive_unit = self.class.primitive
158
+
159
+ raise MissingPrimitiveUnitError if primitive_unit.nil?
160
+ primitive_unit
155
161
  else
156
162
  unit_from_unit_or_name!(target_unit)
157
163
  end
@@ -474,7 +480,7 @@ module UnitMeasurements
474
480
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
475
481
  # @since 1.0.0
476
482
  def unit_from_unit_or_name!(value)
477
- value.is_a?(Unit) ? value : self.class.send(:unit_group).unit_for!(value)
483
+ value.is_a?(Unit) ? value : self.class.unit_for!(value)
478
484
  end
479
485
 
480
486
  # Calculates the conversion factor between the current unit and the target
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.11.0"
7
+ VERSION = "5.12.0"
8
8
  end
@@ -2,26 +2,6 @@
2
2
  # -*- frozen_string_literal: true -*-
3
3
  # -*- warn_indent: true -*-
4
4
 
5
- # The +UnitMeasurements+ module serves as a container for various functionalities
6
- # related to unit measurements. It provides methods for creating custom unit
7
- # groups, defining units, performing arithmetic operations, comparison between
8
- # measurements, conversions, normalization of input strings, parsing measurements
9
- # from strings, and more. It is a fundamental part of the unit measurements library.
10
- #
11
- # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
12
- # @since 0.1.0
13
- module UnitMeasurements
14
- # This is the base class for custom errors in the +UnitMeasurements+ module.
15
- #
16
- # @see ParseError
17
- # @see PrimitiveUnitAlreadySetError
18
- # @see UnitAlreadyDefinedError
19
- # @see UnitError
20
- # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
21
- # @since 1.1.0
22
- class BaseError < StandardError; end
23
- end
24
-
25
5
  require "unit_measurements/base"
26
6
 
27
7
  require "unit_measurements/unit_groups/all"
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.11.0
4
+ version: 5.12.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 00:00:00.000000000 Z
11
+ date: 2023-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -113,6 +113,7 @@ files:
113
113
  - lib/unit_measurements/conversion.rb
114
114
  - lib/unit_measurements/errors/blank_quantity_error.rb
115
115
  - lib/unit_measurements/errors/blank_unit_error.rb
116
+ - lib/unit_measurements/errors/missing_primitive_unit_error.rb
116
117
  - lib/unit_measurements/errors/parse_error.rb
117
118
  - lib/unit_measurements/errors/primitive_unit_already_set_error.rb
118
119
  - lib/unit_measurements/errors/unit_already_defined_error.rb