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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 330be281aba063532010cf47f1e0065c35390510749002f368ff3139a22e6b5c
4
- data.tar.gz: 26a8aa9db580205a117b11a8b653a5c609d1972be7b3eb76e2b91ec20f02b7be
3
+ metadata.gz: b4606afee8a816cd36066634a4d6e8e23448548eb51d90d984c5230996f4e12c
4
+ data.tar.gz: ab4a36000579a4c3aa33f766a883bbd80c3fc1a485dcef6b12725316154db094
5
5
  SHA512:
6
- metadata.gz: 2d67ddc3a66baec5ce04878b046541dd7b9f3d8da65a18fcf84179f4178c467b3e2276f1a236e22caac9d170815d6529be842fdac2ac45aed79e48b32d136de7
7
- data.tar.gz: 331afbfc4616fd9cbccf13ed6c3d01266ba6fd49e9fe4ae06c9f8ed311210a77381056d0037415b4fc4c48477a5fcda48d2d64397c8df3888bcacf1b965c79c8
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. viz.,
21
+ - Added support to specify unit value as an `array` of number and unit.
6
22
 
7
23
  ----------
8
24
 
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.4.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
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
@@ -5,6 +5,9 @@
5
5
  module UnitMeasurements
6
6
  class Measurement
7
7
  include Formatter
8
+ include Comparison
9
+ include Arithmetic
10
+
8
11
  CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
9
12
 
10
13
  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.4.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.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-15 00:00:00.000000000 Z
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