unit_measurements 5.11.1 → 5.13.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: 299b2e4f1e3ebfedd82a18f0010b1aef87e464a00b17bb0fdccbd3d1d1d43807
4
- data.tar.gz: a80df705e041ae39d0008e8f4e58b792850fc0b5cd74c4983846e02a371b7216
3
+ metadata.gz: 98def91ad4bff4c38e579a967b82ae5467e52db044246e6c91d00ccee9ea8070
4
+ data.tar.gz: 5e7c0aca97692daf71dc1923aabfca63f99ccfa0e246e1ec83665c8f821eb784
5
5
  SHA512:
6
- metadata.gz: 5c01c9a8d9b7fd6f3315b8befea0d852cb3e9c8ba8b2866975f60055c697f5fbb8af8fb7844f94144ca1b3919107bc8fb4570945974a1a62568febe1d665cd61
7
- data.tar.gz: 880f955b5cf1ff30c51247ed3338a68ed78b4a9d416d9f7a594ca377b403749f5c3fad8b1515c56b2cf5b2656ddaa7b4759bad8ca38e8b383043b12b1853b597
6
+ metadata.gz: e9a2f7bb789161cdd305eeec424d8b60eaf50c0ce6daa6868699c872ed5abf6bd06ba7fb9d5ae6cdb2d9021c837081553b984d29fb67b75c03af46e1f0b36fd2
7
+ data.tar.gz: 6098f3468240de037f7a39ec5f0a4ea8e431379523bee2da921f5dbe17368ea772d545e41e187341dfb48749168895a3d24d2a89c70e0b10dc18d9fe2656932a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## [5.13.0](https://github.com/shivam091/unit_measurements/compare/v5.12.0...v5.13.0) - 2023-11-27
2
+
3
+ ### What's new
4
+
5
+ - Added `Measurement#to_primitive` to convert the measurement to the primitive unit.
6
+ - Added `#cbrt` method to calculate cube root of the measurement quantity.
7
+ - Added `#sqrt` method to calculate square root of the measurement quantity.
8
+ - Aliased `#to_primitive` method as `#in_primitive` and `#as_primitive`.
9
+
10
+ ----------
11
+
12
+ ## [5.12.0](https://github.com/shivam091/unit_measurements/compare/v5.11.1...v5.12.0) - 2023-11-25
13
+
14
+ ### What's new
15
+
16
+ - Added `MissingPrimitiveUnitError` error if user tries to convert measurement to
17
+ the primitive unit but it's not set for the unit group.
18
+ - Aliased `#+` method as `#add`.
19
+ - Aliased `#-` method as `#subtract`.
20
+ - Aliased `#*` method as `#times` and `#multiply`.
21
+ - Aliased `#/` method as `#divide`.
22
+
23
+ ----------
24
+
1
25
  ## [5.11.1](https://github.com/shivam091/unit_measurements/compare/v5.11.0...v5.11.1) - 2023-11-16
2
26
 
3
27
  ### What's changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.11.1)
4
+ unit_measurements (5.13.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -116,10 +116,12 @@ UnitMeasurements::Length.new(1, "km").convert_to!("m")
116
116
  You can convert the measurement directly to the `primitive` unit of the unit group as:
117
117
 
118
118
  ```ruby
119
- UnitMeasurements::Length.new(1, "cm").convert_to("primitive")
119
+ UnitMeasurements::Length.new(1, "cm").to_primitive
120
120
  #=> 0.01 m
121
121
  ```
122
122
 
123
+ Note: `#to_primitive` method is aliased as `#in_primitive` and `#as_primitive`.
124
+
123
125
  **Parse string without having to split out the quantity and source unit:**
124
126
 
125
127
  This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
@@ -444,7 +446,7 @@ unit group using the `primitive` method. You can specify cache file name in unit
444
446
 
445
447
  ```ruby
446
448
  UnitMeasurements::MyUnitGroup = UnitMeasurements.build do
447
- # Set primitive unit for the unit group (optional).
449
+ # Set primitive unit for the unit group.
448
450
  primitive "s"
449
451
 
450
452
  # Group units by the unit system (optional).
@@ -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.
@@ -16,10 +16,13 @@ require "unit_measurements/version"
16
16
  module UnitMeasurements
17
17
  # This is the base class for custom errors in the +UnitMeasurements+ module.
18
18
  #
19
+ # @see UnitError
19
20
  # @see ParseError
20
- # @see PrimitiveUnitAlreadySetError
21
+ # @see BlankUnitError
22
+ # @see BlankQuantityError
21
23
  # @see UnitAlreadyDefinedError
22
- # @see UnitError
24
+ # @see MissingPrimitiveUnitError
25
+ # @see PrimitiveUnitAlreadySetError
23
26
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
24
27
  # @since 1.1.0
25
28
  class BaseError < StandardError; end
@@ -168,3 +171,4 @@ require "unit_measurements/errors/unit_already_defined_error"
168
171
  require "unit_measurements/errors/primitive_unit_already_set_error"
169
172
  require "unit_measurements/errors/blank_quantity_error"
170
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
@@ -87,5 +87,34 @@ module UnitMeasurements
87
87
  def ceil(ndigits = 0)
88
88
  self.class.new(quantity.ceil(ndigits), unit)
89
89
  end
90
+
91
+ # Returns square root of the measurement quantity.
92
+ #
93
+ # @example
94
+ # UnitMeasurements::Length.new(9, "m").sqrt
95
+ # => 3.0 m
96
+ #
97
+ # @return [Measurement] A new +Measurement+ instance with square root of quantity.
98
+ #
99
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
100
+ # @since 5.13.0
101
+ def sqrt
102
+ self.class.new((quantity ** Rational(1, 2)), unit)
103
+ end
104
+
105
+ # Returns cube root of the measurement quantity.
106
+ #
107
+ # @example
108
+ # UnitMeasurements::Length.new(27, "m").cbrt
109
+ # => 3.0 m
110
+ #
111
+ # @return [Measurement]
112
+ # A new +Measurement+ instance with cube root of quantity.
113
+ #
114
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
115
+ # @since 5.13.0
116
+ def cbrt
117
+ self.class.new((quantity ** Rational(1, 3)), unit)
118
+ end
90
119
  end
91
120
  end
@@ -131,15 +131,11 @@ module UnitMeasurements
131
131
  # UnitMeasurements::Length.new(1, "m").convert_to("cm")
132
132
  # => 100.0 cm
133
133
  #
134
- # UnitMeasurements::Length.new(1, "cm").convert_to("primitive")
135
- # => 0.01 m
136
- #
137
- # UnitMeasurements::Length.new(1, "m").convert_to("cm", use_cache: true)
138
- # => 100.0 cm
134
+ # UnitMeasurements::Length.new(1, "m").convert_to("mm", use_cache: true)
135
+ # => 1000.0 cm
139
136
  #
140
137
  # @param [String|Symbol] target_unit
141
- # The target unit for conversion. Specifing +primitive+ will convert the
142
- # measurement to a primitive unit of the unit group.
138
+ # The target unit for conversion.
143
139
  # @param [TrueClass|FalseClass] use_cache
144
140
  # Indicates whether to use cached conversion factors.
145
141
  #
@@ -150,11 +146,7 @@ module UnitMeasurements
150
146
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
151
147
  # @since 1.0.0
152
148
  def convert_to(target_unit, use_cache: false)
153
- target_unit = if target_unit.to_s.eql?("primitive")
154
- self.class.unit_group.primitive
155
- else
156
- unit_from_unit_or_name!(target_unit)
157
- end
149
+ target_unit = unit_from_unit_or_name!(target_unit)
158
150
  return self if target_unit == unit
159
151
 
160
152
  conversion_factor = calculate_conversion_factor(target_unit, use_cache)
@@ -171,15 +163,11 @@ module UnitMeasurements
171
163
  # UnitMeasurements::Length.new(1, "m").convert_to!("cm")
172
164
  # => 100.0 cm
173
165
  #
174
- # UnitMeasurements::Length.new(1, "cm").convert_to!("primitive")
175
- # => 0.01 m
176
- #
177
- # UnitMeasurements::Length.new(1, "m").convert_to!("cm", use_cache: true)
178
- # => 100.0 cm
166
+ # UnitMeasurements::Length.new(1, "m").convert_to!("mm", use_cache: true)
167
+ # => 1000.0 mm
179
168
  #
180
169
  # @param [String|Symbol] target_unit
181
- # The target unit for conversion. Specifing +primitive+ will convert the
182
- # measurement to a primitive unit of the unit group.
170
+ # The target unit for conversion.
183
171
  # @param [TrueClass|FalseClass] use_cache
184
172
  # Indicates whether to use cached conversion factors.
185
173
  #
@@ -199,6 +187,41 @@ module UnitMeasurements
199
187
  alias_method :in!, :convert_to!
200
188
  alias_method :as!, :convert_to!
201
189
 
190
+ # Converts the measurement to its primitive unit and returns a new instance
191
+ # of the +Measurement+.
192
+ #
193
+ # The method first retrieves the primitive unit of the unit group associated
194
+ # with the measurement. If the primitive unit is not set, it raises a
195
+ # +MissingPrimitiveUnitError+.
196
+ #
197
+ # @example
198
+ # UnitMeasurements::Length.new(1, "m").to_primitive
199
+ # => 1 m
200
+ #
201
+ # UnitMeasurements::Length.new(1, "cm").to_primitive
202
+ # => 0.01 m
203
+ #
204
+ # @param [TrueClass|FalseClass] use_cache
205
+ # Indicates whether to use cached conversion factors.
206
+ # @return [Measurement]
207
+ # A new +Measurement+ instance representing the measurement in its
208
+ # primitive unit.
209
+ #
210
+ # @raise [MissingPrimitiveUnitError]
211
+ # If the primitive unit is not set for the unit group associated with the
212
+ # measurement.
213
+ #
214
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
215
+ # @since 5.13.0
216
+ def to_primitive(use_cache: false)
217
+ primitive_unit = self.class.primitive
218
+ raise MissingPrimitiveUnitError if primitive_unit.nil?
219
+
220
+ convert_to(primitive_unit, use_cache: use_cache)
221
+ end
222
+ alias_method :in_primitive, :to_primitive
223
+ alias_method :as_primitive, :to_primitive
224
+
202
225
  # Returns an object representation of the +Measurement+.
203
226
  #
204
227
  # @param [TrueClass|FalseClass] dump If +true+, returns the dump representation.
@@ -474,7 +497,7 @@ module UnitMeasurements
474
497
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
475
498
  # @since 1.0.0
476
499
  def unit_from_unit_or_name!(value)
477
- value.is_a?(Unit) ? value : self.class.send(:unit_group).unit_for!(value)
500
+ value.is_a?(Unit) ? value : self.class.unit_for!(value)
478
501
  end
479
502
 
480
503
  # 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.1"
7
+ VERSION = "5.13.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.11.1
4
+ version: 5.13.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-16 00:00:00.000000000 Z
11
+ date: 2023-11-27 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