unit_measurements 1.3.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 +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +23 -0
- data/lib/unit_measurements/arithmetic.rb +74 -0
- data/lib/unit_measurements/base.rb +2 -1
- data/lib/unit_measurements/measurement.rb +1 -0
- data/lib/unit_measurements/version.rb +1 -1
- metadata +3 -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,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
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"
|
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,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
|