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 +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/README.md +22 -0
- data/lib/unit_measurements/base.rb +1 -0
- data/lib/unit_measurements/comparison.rb +22 -0
- data/lib/unit_measurements/measurement.rb +2 -0
- data/lib/unit_measurements/unit.rb +17 -7
- 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: 3562e30ce7bfed682e67fbdc5c9d8d30b011a6cd16996b7efe6860622abf25d5
|
4
|
+
data.tar.gz: af4224714284377ebd6aea3a78c4842078f708eeaf06500658ef45f151192373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
@@ -38,13 +38,11 @@ module UnitMeasurements
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def conversion_factor
|
41
|
-
if value.is_a?(
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
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.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-
|
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
|