unit_measurements 1.2.0 → 1.4.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 +17 -1
 - data/Gemfile.lock +1 -1
 - data/README.md +42 -0
 - data/lib/unit_measurements/arithmetic.rb +74 -0
 - data/lib/unit_measurements/base.rb +2 -0
 - data/lib/unit_measurements/comparison.rb +22 -0
 - data/lib/unit_measurements/measurement.rb +3 -0
 - data/lib/unit_measurements/version.rb +1 -1
 - metadata +4 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: b4606afee8a816cd36066634a4d6e8e23448548eb51d90d984c5230996f4e12c
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ab4a36000579a4c3aa33f766a883bbd80c3fc1a485dcef6b12725316154db094
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: ffb38111aa3c731166a453fd9f217a2a49a6bffc1db9962c8853af470dcdbf998d51834db785fe5863d2d3d8738cd86ac87d9a1f17ecd98990230683734774e6
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: c5e40fbf843746bac311e1e33071dc41b9b936551760f9aa488acd708b54b2ce8c6e26fb83d197f466acda14788f94fc67fd5013d0c5179116be2914f5e5bd25
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    | 
         @@ -1,8 +1,24 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ## [1.4.0](https://github.com/shivam091/unit_measurements/compare/v1.3.0...v1.4.0) - 2023-08-17
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            ### What's new
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            - Added ability to perform `arithmetic` operations of measurements.
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            ----------
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            ## [1.3.0](https://github.com/shivam091/unit_measurements/compare/v1.2.0...v1.3.0) - 2023-08-16
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            ### What's new
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            - Added ability to compare two measurements of the same unit group.
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            ----------
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
       1 
17 
     | 
    
         
             
            ## [1.2.0](https://github.com/shivam091/unit_measurements/compare/v1.1.0...v1.2.0) - 2023-08-15
         
     | 
| 
       2 
18 
     | 
    
         | 
| 
       3 
19 
     | 
    
         
             
            ### What's new
         
     | 
| 
       4 
20 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
            - Added support to specify unit value as an `array` of number and unit. 
     | 
| 
      
 21 
     | 
    
         
            +
            - Added support to specify unit value as an `array` of number and unit.
         
     | 
| 
       6 
22 
     | 
    
         | 
| 
       7 
23 
     | 
    
         
             
            ----------
         
     | 
| 
       8 
24 
     | 
    
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | 
         @@ -303,6 +303,48 @@ UnitMeasurements::Weight.unit_or_alias?(:gramme) 
     | 
|
| 
       303 
303 
     | 
    
         
             
            #=> true
         
     | 
| 
       304 
304 
     | 
    
         
             
            ```
         
     | 
| 
       305 
305 
     | 
    
         | 
| 
      
 306 
     | 
    
         
            +
            ### Comparisons
         
     | 
| 
      
 307 
     | 
    
         
            +
             
     | 
| 
      
 308 
     | 
    
         
            +
            You have ability to compare the measurements with the same or different units within the same unit group.
         
     | 
| 
      
 309 
     | 
    
         
            +
            For example, comparing weight with weight will work, comparing a weight with a area would fail.
         
     | 
| 
      
 310 
     | 
    
         
            +
            Allowed comparisons are `==`, `!=`, `>`, `>=`, `<`, and `<=`.
         
     | 
| 
      
 311 
     | 
    
         
            +
             
     | 
| 
      
 312 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 313 
     | 
    
         
            +
            UnitMeasurements::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
         
     | 
| 
      
 314 
     | 
    
         
            +
            #=> true
         
     | 
| 
      
 315 
     | 
    
         
            +
            UnitMeasurements::Weight.parse("1 kg") == UnitMeasurements::Weight.parse("1000 g")
         
     | 
| 
      
 316 
     | 
    
         
            +
            #=> true
         
     | 
| 
      
 317 
     | 
    
         
            +
            UnitMeasurements::Weight.parse("1 kg") != UnitMeasurements::Weight.parse("1 g")
         
     | 
| 
      
 318 
     | 
    
         
            +
            #=> true
         
     | 
| 
      
 319 
     | 
    
         
            +
            UnitMeasurements::Weight.parse("1 kg") <= UnitMeasurements::Weight.parse("0.5 kg")
         
     | 
| 
      
 320 
     | 
    
         
            +
            #=> false
         
     | 
| 
      
 321 
     | 
    
         
            +
            UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg")
         
     | 
| 
      
 322 
     | 
    
         
            +
            #=> true
         
     | 
| 
      
 323 
     | 
    
         
            +
            ```
         
     | 
| 
      
 324 
     | 
    
         
            +
             
     | 
| 
      
 325 
     | 
    
         
            +
            ### Arithmetic
         
     | 
| 
      
 326 
     | 
    
         
            +
             
     | 
| 
      
 327 
     | 
    
         
            +
            You have the ability to perform arithmetic operations on measurements with the same or
         
     | 
| 
      
 328 
     | 
    
         
            +
            different units within the same unit group. You can perform arithmetic operations on
         
     | 
| 
      
 329 
     | 
    
         
            +
            measurement by either other compatible measurement or number.
         
     | 
| 
      
 330 
     | 
    
         
            +
             
     | 
| 
      
 331 
     | 
    
         
            +
            **Methods:**
         
     | 
| 
      
 332 
     | 
    
         
            +
            1. `#+` - Adds the other measurement quantity or number to the measurement.
         
     | 
| 
      
 333 
     | 
    
         
            +
            2. `#-` - Subtracts the other measurement quantity or number from the measurement.
         
     | 
| 
      
 334 
     | 
    
         
            +
            3. `#*` - Multiplies the measurement quantity by other measurement quantity or number.
         
     | 
| 
      
 335 
     | 
    
         
            +
            4. `#/` - Divides the measurement quantity by other measurement quantity or number.
         
     | 
| 
      
 336 
     | 
    
         
            +
             
     | 
| 
      
 337 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 338 
     | 
    
         
            +
            UnitMeasurements::Weight.new(1, :kg) + UnitMeasurements::Weight.new(1, :g)
         
     | 
| 
      
 339 
     | 
    
         
            +
            #=> 1.001 kg
         
     | 
| 
      
 340 
     | 
    
         
            +
            UnitMeasurements::Weight.new(2, :kg) - 1
         
     | 
| 
      
 341 
     | 
    
         
            +
            #=> 1 kg
         
     | 
| 
      
 342 
     | 
    
         
            +
            UnitMeasurements::Weight.new(2, :kg) * 2
         
     | 
| 
      
 343 
     | 
    
         
            +
            #=> 4 kg
         
     | 
| 
      
 344 
     | 
    
         
            +
            UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg)
         
     | 
| 
      
 345 
     | 
    
         
            +
            #=> 2 kg
         
     | 
| 
      
 346 
     | 
    
         
            +
            ```
         
     | 
| 
      
 347 
     | 
    
         
            +
             
     | 
| 
       306 
348 
     | 
    
         
             
            ## Units
         
     | 
| 
       307 
349 
     | 
    
         | 
| 
       308 
350 
     | 
    
         
             
            The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
         
     | 
| 
         @@ -0,0 +1,74 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            # -*- frozen_string_literal: true -*-
         
     | 
| 
      
 3 
     | 
    
         
            +
            # -*- warn_indent: true -*-
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module UnitMeasurements
         
     | 
| 
      
 6 
     | 
    
         
            +
              module Arithmetic
         
     | 
| 
      
 7 
     | 
    
         
            +
                # Adds the other measurement quantity or number to the measurement.
         
     | 
| 
      
 8 
     | 
    
         
            +
                #
         
     | 
| 
      
 9 
     | 
    
         
            +
                # @param [Numeric or Measurement] other
         
     | 
| 
      
 10 
     | 
    
         
            +
                #
         
     | 
| 
      
 11 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 12 
     | 
    
         
            +
                #   UnitMeasurements::Weight.new(1, :kg) + UnitMeasurements::Weight.new(1, :g)
         
     | 
| 
      
 13 
     | 
    
         
            +
                #   => 1.001 kg
         
     | 
| 
      
 14 
     | 
    
         
            +
                #
         
     | 
| 
      
 15 
     | 
    
         
            +
                # @return [Measurement]
         
     | 
| 
      
 16 
     | 
    
         
            +
                def +(other)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  arithmetic_operation(other, :+)
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                # Subtracts the other measurement quantity or number from the measurement.
         
     | 
| 
      
 21 
     | 
    
         
            +
                #
         
     | 
| 
      
 22 
     | 
    
         
            +
                # @param [Numeric or Measurement] other
         
     | 
| 
      
 23 
     | 
    
         
            +
                #
         
     | 
| 
      
 24 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 25 
     | 
    
         
            +
                #   UnitMeasurements::Weight.new(2, :kg) - 1
         
     | 
| 
      
 26 
     | 
    
         
            +
                #   => 1 kg
         
     | 
| 
      
 27 
     | 
    
         
            +
                #
         
     | 
| 
      
 28 
     | 
    
         
            +
                # @return [Measurement]
         
     | 
| 
      
 29 
     | 
    
         
            +
                def -(other)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  arithmetic_operation(other, :-)
         
     | 
| 
      
 31 
     | 
    
         
            +
                end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                # Multiplies the measurement quantity by other measurement quantity or number.
         
     | 
| 
      
 34 
     | 
    
         
            +
                #
         
     | 
| 
      
 35 
     | 
    
         
            +
                # @param [Numeric or Measurement] other
         
     | 
| 
      
 36 
     | 
    
         
            +
                #
         
     | 
| 
      
 37 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 38 
     | 
    
         
            +
                #   UnitMeasurements::Weight.new(2, :kg) * 2
         
     | 
| 
      
 39 
     | 
    
         
            +
                #   => 4 kg
         
     | 
| 
      
 40 
     | 
    
         
            +
                #
         
     | 
| 
      
 41 
     | 
    
         
            +
                # @return [Measurement]
         
     | 
| 
      
 42 
     | 
    
         
            +
                def *(other)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  arithmetic_operation(other, :*)
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                # Divides the measurement quantity by other measurement quantity or number.
         
     | 
| 
      
 47 
     | 
    
         
            +
                #
         
     | 
| 
      
 48 
     | 
    
         
            +
                # @param [Numeric or Measurement] other
         
     | 
| 
      
 49 
     | 
    
         
            +
                #
         
     | 
| 
      
 50 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 51 
     | 
    
         
            +
                #   UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg)
         
     | 
| 
      
 52 
     | 
    
         
            +
                #   => 2 kg
         
     | 
| 
      
 53 
     | 
    
         
            +
                #
         
     | 
| 
      
 54 
     | 
    
         
            +
                # @return [Measurement]
         
     | 
| 
      
 55 
     | 
    
         
            +
                def /(other)
         
     | 
| 
      
 56 
     | 
    
         
            +
                  arithmetic_operation(other, :/)
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                private
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def coerce(other)
         
     | 
| 
      
 62 
     | 
    
         
            +
                  case other
         
     | 
| 
      
 63 
     | 
    
         
            +
                  when Numeric    then [self.class.new(other, self.unit), self]
         
     | 
| 
      
 64 
     | 
    
         
            +
                  when self.class then [other, self]
         
     | 
| 
      
 65 
     | 
    
         
            +
                  else                 raise TypeError, "Cannot coerce #{other.class} to #{self.class}"
         
     | 
| 
      
 66 
     | 
    
         
            +
                  end
         
     | 
| 
      
 67 
     | 
    
         
            +
                end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                def arithmetic_operation(other, operator)
         
     | 
| 
      
 70 
     | 
    
         
            +
                  other, _ = coerce(other)
         
     | 
| 
      
 71 
     | 
    
         
            +
                  self.class.new(self.quantity.public_send(operator, other.convert_to(self.unit).quantity), self.unit)
         
     | 
| 
      
 72 
     | 
    
         
            +
                end
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -25,6 +25,8 @@ end 
     | 
|
| 
       25 
25 
     | 
    
         
             
            require "unit_measurements/unit_group_builder"
         
     | 
| 
       26 
26 
     | 
    
         
             
            require "unit_measurements/unit"
         
     | 
| 
       27 
27 
     | 
    
         
             
            require "unit_measurements/unit_group"
         
     | 
| 
      
 28 
     | 
    
         
            +
            require "unit_measurements/arithmetic"
         
     | 
| 
      
 29 
     | 
    
         
            +
            require "unit_measurements/comparison"
         
     | 
| 
       28 
30 
     | 
    
         
             
            require "unit_measurements/normalizer"
         
     | 
| 
       29 
31 
     | 
    
         
             
            require "unit_measurements/parser"
         
     | 
| 
       30 
32 
     | 
    
         
             
            require "unit_measurements/formatter"
         
     | 
| 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            # -*- frozen_string_literal: true -*-
         
     | 
| 
      
 3 
     | 
    
         
            +
            # -*- warn_indent: true -*-
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module UnitMeasurements
         
     | 
| 
      
 6 
     | 
    
         
            +
              module Comparison
         
     | 
| 
      
 7 
     | 
    
         
            +
                include Comparable
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                # Compares the quantities of two measurements within the same unit group.
         
     | 
| 
      
 10 
     | 
    
         
            +
                #
         
     | 
| 
      
 11 
     | 
    
         
            +
                # @example
         
     | 
| 
      
 12 
     | 
    
         
            +
                #   UnitMeasurements::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
         
     | 
| 
      
 13 
     | 
    
         
            +
                #   => true
         
     | 
| 
      
 14 
     | 
    
         
            +
                #
         
     | 
| 
      
 15 
     | 
    
         
            +
                # @return [Boolean]
         
     | 
| 
      
 16 
     | 
    
         
            +
                def <=>(object)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  return nil unless object.is_a?(self.class)
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  quantity <=> object.convert_to(unit.name).quantity
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
            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: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.4.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-09- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2023-09-17 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activesupport
         
     | 
| 
         @@ -103,7 +103,9 @@ files: 
     | 
|
| 
       103 
103 
     | 
    
         
             
            - README.md
         
     | 
| 
       104 
104 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       105 
105 
     | 
    
         
             
            - lib/unit_measurements.rb
         
     | 
| 
      
 106 
     | 
    
         
            +
            - lib/unit_measurements/arithmetic.rb
         
     | 
| 
       106 
107 
     | 
    
         
             
            - lib/unit_measurements/base.rb
         
     | 
| 
      
 108 
     | 
    
         
            +
            - lib/unit_measurements/comparison.rb
         
     | 
| 
       107 
109 
     | 
    
         
             
            - lib/unit_measurements/errors/parse_error.rb
         
     | 
| 
       108 
110 
     | 
    
         
             
            - lib/unit_measurements/errors/unit_already_defined_error.rb
         
     | 
| 
       109 
111 
     | 
    
         
             
            - lib/unit_measurements/errors/unit_error.rb
         
     |