unit_measurements 1.2.0 → 1.3.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: 330be281aba063532010cf47f1e0065c35390510749002f368ff3139a22e6b5c
4
- data.tar.gz: 26a8aa9db580205a117b11a8b653a5c609d1972be7b3eb76e2b91ec20f02b7be
3
+ metadata.gz: 3562e30ce7bfed682e67fbdc5c9d8d30b011a6cd16996b7efe6860622abf25d5
4
+ data.tar.gz: af4224714284377ebd6aea3a78c4842078f708eeaf06500658ef45f151192373
5
5
  SHA512:
6
- metadata.gz: 2d67ddc3a66baec5ce04878b046541dd7b9f3d8da65a18fcf84179f4178c467b3e2276f1a236e22caac9d170815d6529be842fdac2ac45aed79e48b32d136de7
7
- data.tar.gz: 331afbfc4616fd9cbccf13ed6c3d01266ba6fd49e9fe4ae06c9f8ed311210a77381056d0037415b4fc4c48477a5fcda48d2d64397c8df3888bcacf1b965c79c8
6
+ metadata.gz: 22a6a8f0b97a5ef1845448bcf219fbd2d0228b3d74b409e93ce596f7648d67207ea5caa64f39e0c0dd9083bf6425c1012bbf11f25624faf718f377307b698309
7
+ data.tar.gz: 1a436d77f1a99a7eb838e397b0038b7aba73fe19d3acf7aa0e02dbd3d482beeb2fd6b33b648d2b02514681ba6b792076596f761dbca41fb0eab3985d84209f27
data/CHANGELOG.md CHANGED
@@ -1,8 +1,16 @@
1
+ ## [1.3.0](https://github.com/shivam091/unit_measurements/compare/v1.2.0...v1.3.0) - 2023-08-16
2
+
3
+ ### What's new
4
+
5
+ - Added ability to compare two measurements of the same unit group.
6
+
7
+ ----------
8
+
1
9
  ## [1.2.0](https://github.com/shivam091/unit_measurements/compare/v1.1.0...v1.2.0) - 2023-08-15
2
10
 
3
11
  ### What's new
4
12
 
5
- - Added support to specify unit value as an `array` of number and unit. viz.,
13
+ - Added support to specify unit value as an `array` of number and unit.
6
14
 
7
15
  ----------
8
16
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.2.0)
4
+ unit_measurements (1.3.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -303,6 +303,25 @@ 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
+
306
325
  ## Units
307
326
 
308
327
  The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
@@ -28,6 +28,7 @@ require "unit_measurements/unit_group"
28
28
  require "unit_measurements/normalizer"
29
29
  require "unit_measurements/parser"
30
30
  require "unit_measurements/formatter"
31
+ require "unit_measurements/comparison"
31
32
  require "unit_measurements/measurement"
32
33
 
33
34
  require "unit_measurements/errors/unit_error"
@@ -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
@@ -5,6 +5,8 @@
5
5
  module UnitMeasurements
6
6
  class Measurement
7
7
  include Formatter
8
+ include Comparison
9
+
8
10
  CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
9
11
 
10
12
  attr_reader :quantity, :unit
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.2.0"
6
+ VERSION = "1.3.0"
7
7
  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.2.0
4
+ version: 1.3.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-15 00:00:00.000000000 Z
11
+ date: 2023-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -104,6 +104,7 @@ files:
104
104
  - Rakefile
105
105
  - lib/unit_measurements.rb
106
106
  - lib/unit_measurements/base.rb
107
+ - lib/unit_measurements/comparison.rb
107
108
  - lib/unit_measurements/errors/parse_error.rb
108
109
  - lib/unit_measurements/errors/unit_already_defined_error.rb
109
110
  - lib/unit_measurements/errors/unit_error.rb