unit_measurements 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3562e30ce7bfed682e67fbdc5c9d8d30b011a6cd16996b7efe6860622abf25d5
4
- data.tar.gz: af4224714284377ebd6aea3a78c4842078f708eeaf06500658ef45f151192373
3
+ metadata.gz: b4606afee8a816cd36066634a4d6e8e23448548eb51d90d984c5230996f4e12c
4
+ data.tar.gz: ab4a36000579a4c3aa33f766a883bbd80c3fc1a485dcef6b12725316154db094
5
5
  SHA512:
6
- metadata.gz: 22a6a8f0b97a5ef1845448bcf219fbd2d0228b3d74b409e93ce596f7648d67207ea5caa64f39e0c0dd9083bf6425c1012bbf11f25624faf718f377307b698309
7
- data.tar.gz: 1a436d77f1a99a7eb838e397b0038b7aba73fe19d3acf7aa0e02dbd3d482beeb2fd6b33b648d2b02514681ba6b792076596f761dbca41fb0eab3985d84209f27
6
+ metadata.gz: ffb38111aa3c731166a453fd9f217a2a49a6bffc1db9962c8853af470dcdbf998d51834db785fe5863d2d3d8738cd86ac87d9a1f17ecd98990230683734774e6
7
+ data.tar.gz: c5e40fbf843746bac311e1e33071dc41b9b936551760f9aa488acd708b54b2ce8c6e26fb83d197f466acda14788f94fc67fd5013d0c5179116be2914f5e5bd25
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
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
+
1
9
  ## [1.3.0](https://github.com/shivam091/unit_measurements/compare/v1.2.0...v1.3.0) - 2023-08-16
2
10
 
3
11
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (1.3.0)
4
+ unit_measurements (1.4.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -322,6 +322,29 @@ UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg
322
322
  #=> true
323
323
  ```
324
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
+
325
348
  ## Units
326
349
 
327
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,10 +25,11 @@ 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"
31
- require "unit_measurements/comparison"
32
33
  require "unit_measurements/measurement"
33
34
 
34
35
  require "unit_measurements/errors/unit_error"
@@ -6,6 +6,7 @@ module UnitMeasurements
6
6
  class Measurement
7
7
  include Formatter
8
8
  include Comparison
9
+ include Arithmetic
9
10
 
10
11
  CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
11
12
 
@@ -3,5 +3,5 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module UnitMeasurements
6
- VERSION = "1.3.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.3.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-16 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,6 +103,7 @@ 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
107
108
  - lib/unit_measurements/comparison.rb
108
109
  - lib/unit_measurements/errors/parse_error.rb