unit_measurements 5.12.0 → 5.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e01978467f274548e8cf792d9b7bfe26ab85155a2c099482cb8b4017641ed900
4
- data.tar.gz: d09990112a19291527059e822906a802a051541b654babad13e97110c5557dea
3
+ metadata.gz: 98def91ad4bff4c38e579a967b82ae5467e52db044246e6c91d00ccee9ea8070
4
+ data.tar.gz: 5e7c0aca97692daf71dc1923aabfca63f99ccfa0e246e1ec83665c8f821eb784
5
5
  SHA512:
6
- metadata.gz: 0faa94f0a962f19134ff58276c6c876a30a81817172039c4261eaa9a15eeb61dd7047f61b96168c2115c4ba40711fee42cb4ceb832c6e6640bd9c8e7e0f4343b
7
- data.tar.gz: d9213995299feeb51dda98cc4dbbd3bd76f6c4782a8e04b9612cc7f3134b192c74aa80b027779c1b8c862358a84a8c40469753a086ceb94fd5b5ee2b8d33e973
6
+ metadata.gz: e9a2f7bb789161cdd305eeec424d8b60eaf50c0ce6daa6868699c872ed5abf6bd06ba7fb9d5ae6cdb2d9021c837081553b984d29fb67b75c03af46e1f0b36fd2
7
+ data.tar.gz: 6098f3468240de037f7a39ec5f0a4ea8e431379523bee2da921f5dbe17368ea772d545e41e187341dfb48749168895a3d24d2a89c70e0b10dc18d9fe2656932a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
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
+
1
12
  ## [5.12.0](https://github.com/shivam091/unit_measurements/compare/v5.11.1...v5.12.0) - 2023-11-25
2
13
 
3
14
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.12.0)
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).
@@ -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
  #
@@ -147,20 +143,10 @@ module UnitMeasurements
147
143
  # A new +Measurement+ instance with the converted +quantity+ and
148
144
  # +target unit+.
149
145
  #
150
- # @raise [MissingPrimitiveUnitError]
151
- # if primitive unit is not set for the unit group.
152
- #
153
146
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
154
147
  # @since 1.0.0
155
148
  def convert_to(target_unit, use_cache: false)
156
- target_unit = if target_unit.to_s.eql?("primitive")
157
- primitive_unit = self.class.primitive
158
-
159
- raise MissingPrimitiveUnitError if primitive_unit.nil?
160
- primitive_unit
161
- else
162
- unit_from_unit_or_name!(target_unit)
163
- end
149
+ target_unit = unit_from_unit_or_name!(target_unit)
164
150
  return self if target_unit == unit
165
151
 
166
152
  conversion_factor = calculate_conversion_factor(target_unit, use_cache)
@@ -177,15 +163,11 @@ module UnitMeasurements
177
163
  # UnitMeasurements::Length.new(1, "m").convert_to!("cm")
178
164
  # => 100.0 cm
179
165
  #
180
- # UnitMeasurements::Length.new(1, "cm").convert_to!("primitive")
181
- # => 0.01 m
182
- #
183
- # UnitMeasurements::Length.new(1, "m").convert_to!("cm", use_cache: true)
184
- # => 100.0 cm
166
+ # UnitMeasurements::Length.new(1, "m").convert_to!("mm", use_cache: true)
167
+ # => 1000.0 mm
185
168
  #
186
169
  # @param [String|Symbol] target_unit
187
- # The target unit for conversion. Specifing +primitive+ will convert the
188
- # measurement to a primitive unit of the unit group.
170
+ # The target unit for conversion.
189
171
  # @param [TrueClass|FalseClass] use_cache
190
172
  # Indicates whether to use cached conversion factors.
191
173
  #
@@ -205,6 +187,41 @@ module UnitMeasurements
205
187
  alias_method :in!, :convert_to!
206
188
  alias_method :as!, :convert_to!
207
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
+
208
225
  # Returns an object representation of the +Measurement+.
209
226
  #
210
227
  # @param [TrueClass|FalseClass] dump If +true+, returns the dump representation.
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.12.0"
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.12.0
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-25 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