unit_measurements 5.10.0 → 5.11.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: 519b4112a693a9c722e1241381eafb095005e4cbdef9792662e339df52087abf
4
- data.tar.gz: 34054c7ef9056fe40d25f698403f0585b805c7426710302b102e93709582090b
3
+ metadata.gz: ca26bc8965c93c692a72e788ab2577f2bad297909372a72d3cc2fec93a45b5bd
4
+ data.tar.gz: 581d716cb56cf59c89eeb69d01095c4c0c14054006dc6bb0822580e3d6750ffd
5
5
  SHA512:
6
- metadata.gz: cbfdf296b4fab0d7b2c5a7932939ca9cba3205d42f9709d72b070b98748b97cd68a4a29d10c9f086fbd9decf65f906a977458f902e70a04ba2ee019326062f5a
7
- data.tar.gz: df52b1126595b98d905a9f9b86b9884dcddd4c06b3ac547ba8bbac0d2dc4108d35329b1a629d278bf4cadf2959d4923e27be14a08702d8b5fd911affc977a0db
6
+ metadata.gz: ed994225ecb853f8ce577e0b53a863b0f091980ee33f054b50f69d156ad0a7e393a4a59d962d665aa52fa6cdc077f19f42bc27d0b1d8e7413f359dbde8e6a9fb
7
+ data.tar.gz: 889e45c784390c89a46079067ca9493253e638b1117ee88c16c5ab2ed60db89a855a11bf487f2a6536b1729a2704983fbfcb5fa60959a2b8eed79dae4957d920
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [5.11.0](https://github.com/shivam091/unit_measurements/compare/v5.10.0...v5.11.0) - 2023-11-11
2
+
3
+ ### What's new
4
+
5
+ - Added `#systems` method to return unit systems within the unit group.
6
+ - Added `#ratio` method to calculate the ratio between two units.
7
+
8
+ ----------
9
+
1
10
  ## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09
2
11
 
3
12
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.10.0)
4
+ unit_measurements (5.11.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -179,6 +179,13 @@ UnitMeasurements::Length.parse("1:2 km to m")
179
179
  #=> 500.0 m
180
180
  ```
181
181
 
182
+ **Calculate the ratio between two units:**
183
+
184
+ ```ruby
185
+ UnitMeasurements::Length.ratio("in", "ft")
186
+ #=> "12.0 in/ft"
187
+ ```
188
+
182
189
  **Formatting measurement:**
183
190
 
184
191
  If you want to format the measurement to certain format, you can use `#to_fs` method.
@@ -239,6 +246,13 @@ UnitMeasurements::Length.unit_names_with_aliases
239
246
  #=> ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
240
247
  ```
241
248
 
249
+ **See list of unit systems defined within the unit group:**
250
+
251
+ ```ruby
252
+ UnitMeasurements::Length.systems
253
+ #=> ["metric", "imperial", "us_customary", "astronomical"]
254
+ ```
255
+
242
256
  **See list of units within the unit system:**
243
257
 
244
258
  You can use `#units_for` or `#units_for!` methods to find units within the unit system.
@@ -243,7 +243,7 @@ module UnitMeasurements
243
243
  def_delegators :unit_group, :primitive, :units, :cache_file, :unit_names,
244
244
  :unit_with_name_and_aliases, :unit_names_with_aliases,
245
245
  :unit_for, :unit_for!, :defined?, :unit_or_alias?, :[],
246
- :units_for, :units_for!
246
+ :units_for, :units_for!, :systems
247
247
 
248
248
  # Parses an input string and returns a +Measurement+ instance depending on
249
249
  # the input string. This method first normalizes the +input+ internally,
@@ -368,6 +368,40 @@ module UnitMeasurements
368
368
  cached.clear_cache
369
369
  end
370
370
 
371
+ # Calculates the ratio between two units.
372
+ #
373
+ # This method takes a source unit and a target unit, and returns the ratio
374
+ # between them as a string representation.
375
+ #
376
+ # @example Calculating the ratio between 'in' and 'ft':
377
+ # UnitMeasurements::Length.ratio("in", "ft")
378
+ # => "12.0 in/ft"
379
+ #
380
+ # UnitMeasurements::Length.ratio(UnitMeasurements::Length.unit_for("in"), "ft")
381
+ # => "12.0 in/ft"
382
+ #
383
+ # @param [Unit|String|Symbol] source_unit
384
+ # The source unit for the ratio calculation.
385
+ # @param [Unit|String|Symbol] target_unit
386
+ # The target unit for the ratio calculation.
387
+ #
388
+ # @return [String] The ratio between the source and target units.
389
+ #
390
+ # @raise [UnitError]
391
+ # If either the source unit or the target unit is not found in the unit group.
392
+ #
393
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
394
+ # @since 5.11.0
395
+ def ratio(source_unit, target_unit)
396
+ source_unit = source_unit.is_a?(Unit) ? source_unit : unit_for!(source_unit)
397
+ target_unit = target_unit.is_a?(Unit) ? target_unit : unit_for!(target_unit)
398
+
399
+ source_quantity = 1
400
+ target_quantity = new(source_quantity, target_unit).convert_to(source_unit).quantity
401
+
402
+ "#{target_quantity} #{source_unit}/#{target_unit}"
403
+ end
404
+
371
405
  private
372
406
 
373
407
  # @private
@@ -247,6 +247,22 @@ module UnitMeasurements
247
247
  system_units
248
248
  end
249
249
 
250
+ # Returns an array of unit system names defined within the unit group.
251
+ #
252
+ # It scans through the units and compiles a list of unique system names.
253
+ #
254
+ # @example
255
+ # UnitMeasurements::Length.systems
256
+ # => ["metric", "imperial", "us_customary", "astronomical"]
257
+ #
258
+ # @return [Array<String>] An array of unit system names.
259
+ #
260
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
261
+ # @since 5.11.0
262
+ def systems
263
+ units.map { _1.system }.uniq
264
+ end
265
+
250
266
  private
251
267
 
252
268
  # @private
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.10.0"
7
+ VERSION = "5.11.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.10.0
4
+ version: 5.11.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-09 00:00:00.000000000 Z
11
+ date: 2023-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport