unit_measurements 5.10.0 → 5.11.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 +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -0
- data/lib/unit_measurements/measurement.rb +35 -1
- data/lib/unit_measurements/unit_group.rb +16 -0
- data/lib/unit_measurements/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: ca26bc8965c93c692a72e788ab2577f2bad297909372a72d3cc2fec93a45b5bd
         | 
| 4 | 
            +
              data.tar.gz: 581d716cb56cf59c89eeb69d01095c4c0c14054006dc6bb0822580e3d6750ffd
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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
    
    
    
        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
         | 
    
        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. | 
| 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- | 
| 11 | 
            +
            date: 2023-11-11 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activesupport
         |