unit_measurements 1.1.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: 24b2504d7feb77f54138260f7e0947bd86e00225dbe3c6d9e575da3050a24eeb
4
- data.tar.gz: 7d9b84113fbf2a2cd5ca1c5a46467296b15f1bf1016c1775b854098245998cee
3
+ metadata.gz: 3562e30ce7bfed682e67fbdc5c9d8d30b011a6cd16996b7efe6860622abf25d5
4
+ data.tar.gz: af4224714284377ebd6aea3a78c4842078f708eeaf06500658ef45f151192373
5
5
  SHA512:
6
- metadata.gz: b5fc7c6e1b2b580fcd5477941e239e8d06a79416b90a7c36145f8d50d32c40c7430b83273038b2974ec51df90673e46672207f8d8ae975c31e99b3612cd3b446
7
- data.tar.gz: 7730a9813366e4b36dbf98873a575cf89a33963ee998da6c84d7e6b5fd7c7f5c197530e23f1acaeb2a44e339667f9665d7e3bc62c5164579df06765e01426d85
6
+ metadata.gz: 22a6a8f0b97a5ef1845448bcf219fbd2d0228b3d74b409e93ce596f7648d67207ea5caa64f39e0c0dd9083bf6425c1012bbf11f25624faf718f377307b698309
7
+ data.tar.gz: 1a436d77f1a99a7eb838e397b0038b7aba73fe19d3acf7aa0e02dbd3d482beeb2fd6b33b648d2b02514681ba6b792076596f761dbca41fb0eab3985d84209f27
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
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
+
9
+ ## [1.2.0](https://github.com/shivam091/unit_measurements/compare/v1.1.0...v1.2.0) - 2023-08-15
10
+
11
+ ### What's new
12
+
13
+ - Added support to specify unit value as an `array` of number and unit.
14
+
15
+ ----------
16
+
1
17
  ## [1.1.0](https://github.com/shivam091/unit_measurements/compare/v1.0.0...v1.1.0) - 2023-08-14
2
18
 
3
19
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.1.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.
@@ -399,6 +418,9 @@ UnitMeasurements::Time = UnitMeasurements.build do
399
418
  # You can also specify conversion string if it's converted against a unit other
400
419
  # than the unit group's base unit.
401
420
  unit :h, value: "60 min", aliases: [:hour, :hours]
421
+
422
+ # You can also specify unit value as an array.
423
+ unit :d, value: [24, :h], aliases: [:day, :days]
402
424
  end
403
425
  ```
404
426
 
@@ -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
@@ -38,13 +38,11 @@ module UnitMeasurements
38
38
  end
39
39
 
40
40
  def conversion_factor
41
- if value.is_a?(String)
42
- measurement_value, measurement_unit = Parser.parse(value)
43
- conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor
44
- conversion_factor * measurement_value
45
- else
46
- value
47
- end
41
+ return value if value.is_a?(Numeric)
42
+
43
+ measurement_value, measurement_unit = parse_value(value)
44
+ conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor
45
+ conversion_factor * measurement_value
48
46
  end
49
47
 
50
48
  private
@@ -75,5 +73,17 @@ module UnitMeasurements
75
73
  ["R", %w(ronna), 1e+27],
76
74
  ["Q", %w(quetta), 1e+30]
77
75
  ].map(&:freeze).freeze
76
+
77
+ def parse_value(tokens)
78
+ case tokens
79
+ when String
80
+ tokens = Parser.parse(value)
81
+ when Array
82
+ raise BaseError, "Cannot parse [number, unit] formatted tokens from #{tokens}." unless tokens.size == 2
83
+ else
84
+ raise BaseError, "Value of the unit must be defined as string or array, but received #{tokens}"
85
+ end
86
+ [tokens[0].to_r, tokens[1].freeze]
87
+ end
78
88
  end
79
89
  end
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.1.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.1.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-14 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