unit_measurements 5.10.0 → 5.11.1

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: 299b2e4f1e3ebfedd82a18f0010b1aef87e464a00b17bb0fdccbd3d1d1d43807
4
+ data.tar.gz: a80df705e041ae39d0008e8f4e58b792850fc0b5cd74c4983846e02a371b7216
5
5
  SHA512:
6
- metadata.gz: cbfdf296b4fab0d7b2c5a7932939ca9cba3205d42f9709d72b070b98748b97cd68a4a29d10c9f086fbd9decf65f906a977458f902e70a04ba2ee019326062f5a
7
- data.tar.gz: df52b1126595b98d905a9f9b86b9884dcddd4c06b3ac547ba8bbac0d2dc4108d35329b1a629d278bf4cadf2959d4923e27be14a08702d8b5fd911affc977a0db
6
+ metadata.gz: 5c01c9a8d9b7fd6f3315b8befea0d852cb3e9c8ba8b2866975f60055c697f5fbb8af8fb7844f94144ca1b3919107bc8fb4570945974a1a62568febe1d665cd61
7
+ data.tar.gz: 880f955b5cf1ff30c51247ed3338a68ed78b4a9d416d9f7a594ca377b403749f5c3fad8b1515c56b2cf5b2656ddaa7b4759bad8ca38e8b383043b12b1853b597
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## [5.11.1](https://github.com/shivam091/unit_measurements/compare/v5.11.0...v5.11.1) - 2023-11-16
2
+
3
+ ### What's changed
4
+
5
+ - Moved `BaseError` to base file for removing uninitialized constant error in reverse dependencies viz.
6
+ [unit_measurements-rails](https://rubygems.org/gems/unit_measurements-rails) and
7
+ [composite_unit_measurements](https://rubygems.org/gems/composite_unit_measurements).
8
+
9
+ ----------
10
+
11
+ ## [5.11.0](https://github.com/shivam091/unit_measurements/compare/v5.10.0...v5.11.0) - 2023-11-11
12
+
13
+ ### What's new
14
+
15
+ - Added `#systems` method to return unit systems within the unit group.
16
+ - Added `#ratio` method to calculate the ratio between two units.
17
+
18
+ ----------
19
+
1
20
  ## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09
2
21
 
3
22
  ### 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.1)
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.
@@ -5,7 +5,25 @@
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 ParseError
20
+ # @see PrimitiveUnitAlreadySetError
21
+ # @see UnitAlreadyDefinedError
22
+ # @see UnitError
23
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
24
+ # @since 1.1.0
25
+ class BaseError < StandardError; end
26
+
9
27
  class << self
10
28
  # Allows setting an instance of +Configuration+ containing values of desired
11
29
  # configurable options.
@@ -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.1"
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.10.0
4
+ version: 5.11.1
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-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport