unit_measurements 1.5.0 → 1.6.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 +18 -2
- data/Gemfile.lock +1 -1
- data/README.md +25 -1
- data/lib/unit_measurements/arithmetic.rb +1 -0
- data/lib/unit_measurements/base.rb +1 -0
- data/lib/unit_measurements/errors/parse_error.rb +1 -0
- data/lib/unit_measurements/errors/unit_already_defined_error.rb +1 -0
- data/lib/unit_measurements/errors/unit_error.rb +1 -0
- data/lib/unit_measurements/formatter.rb +1 -0
- data/lib/unit_measurements/math.rb +54 -0
- data/lib/unit_measurements/measurement.rb +8 -2
- data/lib/unit_measurements/unit_group_builder.rb +3 -0
- data/lib/unit_measurements/unit_groups/length.rb +5 -5
- data/lib/unit_measurements/unit_groups/weight.rb +3 -3
- data/lib/unit_measurements/version.rb +1 -1
- data/units.md +5 -5
- 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: daf31532333a9d280d948318fe94079cfb5a8d341f6756a5c64b42843142c80f
|
4
|
+
data.tar.gz: 88a85aed303c4c5209330bdcf14b1ee5696e40b0e815b6b570a60ac6fd4b6db5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60febdff329999f4aeedb36fbb8aa70eccf1ab8feecc34c4318daca4949b2df16034a285ce8ac5bee2c99e59f991fe52f456154dba5ae1c945bdf6e732f2ed55
|
7
|
+
data.tar.gz: 8152670ce01518dac56686ccc17f51e5c3b898f6ec90710d9a5ebb66089fa234351a70a0a91a9b9e1adf02cfb0966df3490b02f625aa639239c63440d515e960
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,24 @@
|
|
1
|
+
## [1.6.0](https://github.com/shivam091/unit_measurements/compare/v1.5.1...v1.6.0) - 2023-08-19
|
2
|
+
|
3
|
+
## What's new
|
4
|
+
|
5
|
+
- Added support to perform mathematical operations on the measurements.
|
6
|
+
|
7
|
+
----------
|
8
|
+
|
9
|
+
## [1.5.1](https://github.com/shivam091/unit_measurements/compare/v1.5.0...v1.5.1) - 2023-08-18
|
10
|
+
|
11
|
+
### What's updated
|
12
|
+
|
13
|
+
- Added extra blank line between method code and last code statement (considered as return).
|
14
|
+
|
15
|
+
----------
|
16
|
+
|
1
17
|
## [1.5.0](https://github.com/shivam091/unit_measurements/compare/v1.4.0...v1.5.0) - 2023-08-18
|
2
18
|
|
3
|
-
|
19
|
+
### What's fixed
|
4
20
|
|
5
|
-
- Fixed precision in `Measurement#quantity` method
|
21
|
+
- Fixed precision in `Measurement#quantity` method.
|
6
22
|
|
7
23
|
----------
|
8
24
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -307,7 +307,6 @@ UnitMeasurements::Weight.unit_or_alias?(:gramme)
|
|
307
307
|
|
308
308
|
You have ability to compare the measurements with the same or different units within the same unit group.
|
309
309
|
For example, comparing weight with weight will work, comparing a weight with a area would fail.
|
310
|
-
Allowed comparisons are `==`, `!=`, `>`, `>=`, `<`, and `<=`.
|
311
310
|
|
312
311
|
```ruby
|
313
312
|
UnitMeasurements::Weight.new(1, "kg") == UnitMeasurements::Weight.new(1, :kg)
|
@@ -320,6 +319,10 @@ UnitMeasurements::Weight.parse("1 kg") <= UnitMeasurements::Weight.parse("0.5 kg
|
|
320
319
|
#=> false
|
321
320
|
UnitMeasurements::Weight.parse("1 kg") >= UnitMeasurements::Weight.parse("0.5 kg")
|
322
321
|
#=> true
|
322
|
+
UnitMeasurements::Length.new(1, :ft).between?(UnitMeasurements::Length.new(12, :in), UnitMeasurements::Length.new(24, :in))
|
323
|
+
#=> true
|
324
|
+
UnitMeasurements::Length.new(1, :ft).clamp(UnitMeasurements::Length.new(13, :in), UnitMeasurements::Length.new(24, :in))
|
325
|
+
#=> 13 in
|
323
326
|
```
|
324
327
|
|
325
328
|
### Arithmetic
|
@@ -345,6 +348,27 @@ UnitMeasurements::Weight.new(4, :kg) / UnitMeasurements::Weight.new(2, :kg)
|
|
345
348
|
#=> 2 kg
|
346
349
|
```
|
347
350
|
|
351
|
+
### Math
|
352
|
+
|
353
|
+
You can perform mathematical operations on the measurements.
|
354
|
+
|
355
|
+
**Methods:**
|
356
|
+
1. `#round` - Rounds quantity of the measurement. If `ndigits` is not specified, quantity is rounded to `Integer`.
|
357
|
+
2. `#abs` - Returns absolute value of the measurement quantity.
|
358
|
+
3. `#floor` - Rounds quantity of the measurement to next lower integer.
|
359
|
+
4. `#ceil` - Rounds quantity of the measurement to next higher integer.
|
360
|
+
|
361
|
+
```ruby
|
362
|
+
UnitMeasurements::Weight.new(1, :g).convert_to(:st).round(4)
|
363
|
+
#=> 0.0002 st
|
364
|
+
UnitMeasurements::Length.new(-17.625, :m).abs
|
365
|
+
#=> 17.625 m
|
366
|
+
UnitMeasurements::Length.new(17.625, :m).floor
|
367
|
+
#=> 17 m
|
368
|
+
UnitMeasurements::Length.new(17.625, :m).ceil
|
369
|
+
#=> 18 m
|
370
|
+
```
|
371
|
+
|
348
372
|
## Units
|
349
373
|
|
350
374
|
The **`UnitMeasurements::Unit`** class is used to represent the units for a measurement.
|
@@ -27,6 +27,7 @@ require "unit_measurements/unit"
|
|
27
27
|
require "unit_measurements/unit_group"
|
28
28
|
require "unit_measurements/arithmetic"
|
29
29
|
require "unit_measurements/comparison"
|
30
|
+
require "unit_measurements/math"
|
30
31
|
require "unit_measurements/normalizer"
|
31
32
|
require "unit_measurements/parser"
|
32
33
|
require "unit_measurements/formatter"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
module Math
|
7
|
+
# Rounds quantity of the measurement. If `ndigits` is not specified,
|
8
|
+
# quantity is rounded to `Integer`.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# UnitMeasurements::Weight.new(1, :g).convert_to(:st).round(4)
|
12
|
+
# => 0.0002 st
|
13
|
+
#
|
14
|
+
# @param [Integer] ndigits
|
15
|
+
#
|
16
|
+
# @return [Measurement]
|
17
|
+
def round(ndigits = 0)
|
18
|
+
self.class.new(quantity.round(ndigits), unit)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns absolute value of the measurement quantity.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# UnitMeasurements::Length.new(-17.625, :m).abs
|
25
|
+
# => 17.625 m
|
26
|
+
#
|
27
|
+
# @return [Measurement]
|
28
|
+
def abs
|
29
|
+
self.class.new(quantity.abs, unit)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Rounds quantity of the measurement to next lower integer.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# UnitMeasurements::Length.new(17.625, :m).floor
|
36
|
+
# => 17 m
|
37
|
+
#
|
38
|
+
# @return [Measurement]
|
39
|
+
def floor(*args)
|
40
|
+
self.class.new(quantity.floor(*args), unit)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Rounds quantity of the measurement to next higher integer.
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# UnitMeasurements::Length.new(17.625, :m).ceil
|
47
|
+
# => 18 m
|
48
|
+
#
|
49
|
+
# @return [Measurement]
|
50
|
+
def ceil(*args)
|
51
|
+
self.class.new(quantity.ceil(*args), unit)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -4,9 +4,10 @@
|
|
4
4
|
|
5
5
|
module UnitMeasurements
|
6
6
|
class Measurement
|
7
|
-
include Formatter
|
8
|
-
include Comparison
|
9
7
|
include Arithmetic
|
8
|
+
include Comparison
|
9
|
+
include Formatter
|
10
|
+
include Math
|
10
11
|
|
11
12
|
CONVERSION_STRING_REGEXP = /(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
|
12
13
|
|
@@ -26,6 +27,7 @@ module UnitMeasurements
|
|
26
27
|
return self if target_unit == unit
|
27
28
|
|
28
29
|
conversion_factor = (unit.conversion_factor / target_unit.conversion_factor)
|
30
|
+
|
29
31
|
self.class.new((quantity * conversion_factor), target_unit)
|
30
32
|
end
|
31
33
|
alias_method :to, :convert_to
|
@@ -33,6 +35,7 @@ module UnitMeasurements
|
|
33
35
|
def convert_to!(target_unit)
|
34
36
|
measurement = convert_to(target_unit)
|
35
37
|
@quantity, @unit = measurement.quantity, measurement.unit
|
38
|
+
|
36
39
|
self
|
37
40
|
end
|
38
41
|
alias_method :to!, :convert_to!
|
@@ -70,6 +73,7 @@ module UnitMeasurements
|
|
70
73
|
def parse(input)
|
71
74
|
input = Normalizer.normalize(input)
|
72
75
|
source, target = input.match(CONVERSION_STRING_REGEXP)&.captures
|
76
|
+
|
73
77
|
target ? _parse(source).convert_to(target) : _parse(source)
|
74
78
|
end
|
75
79
|
|
@@ -77,6 +81,7 @@ module UnitMeasurements
|
|
77
81
|
|
78
82
|
def _parse(string)
|
79
83
|
quantity, unit = Parser.parse(string)
|
84
|
+
|
80
85
|
new(quantity, unit)
|
81
86
|
end
|
82
87
|
end
|
@@ -92,6 +97,7 @@ module UnitMeasurements
|
|
92
97
|
when String
|
93
98
|
quantity = Normalizer.normalize(quantity)
|
94
99
|
quantity, _ = Parser.parse(quantity)
|
100
|
+
|
95
101
|
quantity
|
96
102
|
else
|
97
103
|
quantity
|
@@ -37,17 +37,20 @@ module UnitMeasurements
|
|
37
37
|
end
|
38
38
|
si_units << build_unit("#{short_prefix}#{name}", value: "#{multiplier} #{name}", aliases: si_aliases)
|
39
39
|
end
|
40
|
+
|
40
41
|
si_units
|
41
42
|
end
|
42
43
|
|
43
44
|
def build_unit(name, value:, aliases:)
|
44
45
|
unit = Unit.new(name, value: value, aliases: aliases)
|
45
46
|
check_for_duplicate_unit_names!(unit)
|
47
|
+
|
46
48
|
unit
|
47
49
|
end
|
48
50
|
|
49
51
|
def check_for_duplicate_unit_names!(unit)
|
50
52
|
names = @units.flat_map(&:names)
|
53
|
+
|
51
54
|
if names.any? { |name| unit.names.include?(name) }
|
52
55
|
raise UnitAlreadyDefinedError.new(unit.name)
|
53
56
|
end
|
@@ -3,10 +3,10 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
UnitMeasurements::Length = UnitMeasurements.build do
|
6
|
-
si_unit :m, aliases:
|
6
|
+
si_unit :m, aliases: [:meter, :metre, :meters, :metres]
|
7
7
|
|
8
|
-
unit :in, value: "25.4 mm", aliases:
|
9
|
-
unit :ft, value: "12 in", aliases:
|
10
|
-
unit :yd, value: "3 ft", aliases:
|
11
|
-
unit :mi, value: "1760 yd", aliases:
|
8
|
+
unit :in, value: "25.4 mm", aliases: [:'"', :inch, :inches]
|
9
|
+
unit :ft, value: "12 in", aliases: [:"'", :foot, :feet]
|
10
|
+
unit :yd, value: "3 ft", aliases: [:yard, :yards]
|
11
|
+
unit :mi, value: "1760 yd", aliases: [:mile, :miles]
|
12
12
|
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
# -*- warn_indent: true -*-
|
4
4
|
|
5
5
|
UnitMeasurements::Weight = UnitMeasurements.build do
|
6
|
-
si_unit :g, aliases:
|
6
|
+
si_unit :g, aliases: [:gram, :grams, :gramme, :grammes]
|
7
7
|
|
8
|
-
unit :q, value: "100 kg", aliases:
|
9
|
-
unit :t, value: "10 q", aliases:
|
8
|
+
unit :q, value: "100 kg", aliases: [:quintal, :quintals]
|
9
|
+
unit :t, value: "10 q", aliases: [:tonne, :tonnes, :"metric tonne", :"metric tonnes"]
|
10
10
|
end
|
data/units.md
CHANGED
@@ -5,7 +5,7 @@ bundled units before converting your measurements.
|
|
5
5
|
|
6
6
|
**Notes:**
|
7
7
|
1. Base unit for each unit group is highlighted.
|
8
|
-
2. Unit
|
8
|
+
2. Unit names suffixed with `*` support all [SI prefixes](README.md#si-units-support).
|
9
9
|
|
10
10
|
Below are the units which are bundled in the unit_measurements.
|
11
11
|
|
@@ -13,9 +13,9 @@ Below are the units which are bundled in the unit_measurements.
|
|
13
13
|
|
14
14
|
These units are defined in `UnitMeasurements::Length`.
|
15
15
|
|
16
|
-
| # |
|
16
|
+
| # | Name | Aliases |
|
17
17
|
|:--|:--|:--|
|
18
|
-
| **1
|
18
|
+
| **1** | **m\*** | **meter, metre, meters, metres** |
|
19
19
|
| 2 | in | ", inch, inches |
|
20
20
|
| 3 | ft | ', foot, feet |
|
21
21
|
| 4 | yd | yard, yards |
|
@@ -25,8 +25,8 @@ These units are defined in `UnitMeasurements::Length`.
|
|
25
25
|
|
26
26
|
These units are defined in `UnitMeasurements::Weight`.
|
27
27
|
|
28
|
-
| # |
|
28
|
+
| # | Name | Aliases |
|
29
29
|
|--|--|--|
|
30
|
-
| **1
|
30
|
+
| **1** | **g\*** | **gram, grams, gramme, grammes** |
|
31
31
|
| 2 | q | quintal, quintals |
|
32
32
|
| 3 | t | tonne, tonnes, metric tonne, metric tonnes |
|
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.6.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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/unit_measurements/errors/unit_already_defined_error.rb
|
111
111
|
- lib/unit_measurements/errors/unit_error.rb
|
112
112
|
- lib/unit_measurements/formatter.rb
|
113
|
+
- lib/unit_measurements/math.rb
|
113
114
|
- lib/unit_measurements/measurement.rb
|
114
115
|
- lib/unit_measurements/normalizer.rb
|
115
116
|
- lib/unit_measurements/parser.rb
|