unit_measurements 1.4.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/README.md +39 -36
- data/lib/unit_measurements/arithmetic.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/measurement.rb +15 -11
- data/lib/unit_measurements/unit_group_builder.rb +3 -0
- data/lib/unit_measurements/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aac3030ed1c46bb4bdfe831bc9bedb29ad2a8b0e28595ba4726b2a3ff5e24791
|
4
|
+
data.tar.gz: 9179c7b41ef7e84b6864ef98ccb45210ab34b2488830aa3de5552659b7f39039
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5435fba092be97c1c8407b917857c1990db07d1aa1729653ecf14519d98dcec2a4c5e5ad0c4997586eb2f72b17d1099506068aa99eb592ee35ddd8da4a2f8ba7
|
7
|
+
data.tar.gz: 5938944868b729168d49d1f5e7e888029931bb17237fd72c66980bf6b47d7fe58f8eefdd94f1927652d9b5d1e3e480e8df746f5c7607995390ea7caeec186634
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## [1.5.1](https://github.com/shivam091/unit_measurements/compare/v1.5.0...v1.5.1) - 2023-08-18
|
2
|
+
|
3
|
+
### What's updated
|
4
|
+
|
5
|
+
- Added extra blank line between method code and last code statement (considered as return).
|
6
|
+
|
7
|
+
----------
|
8
|
+
|
9
|
+
## [1.5.0](https://github.com/shivam091/unit_measurements/compare/v1.4.0...v1.5.0) - 2023-08-18
|
10
|
+
|
11
|
+
### What's fixed
|
12
|
+
|
13
|
+
- Fixed precision in `Measurement#quantity` method.
|
14
|
+
|
15
|
+
----------
|
16
|
+
|
1
17
|
## [1.4.0](https://github.com/shivam091/unit_measurements/compare/v1.3.0...v1.4.0) - 2023-08-17
|
2
18
|
|
3
19
|
### What's new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -79,14 +79,14 @@ You can use `#convert_to` as:
|
|
79
79
|
|
80
80
|
```ruby
|
81
81
|
UnitMeasurements::Weight.new(1, :kg).convert_to(:g)
|
82
|
-
#=> 1000 g
|
82
|
+
#=> 1000.0 g
|
83
83
|
```
|
84
84
|
|
85
85
|
If you want to modify measurement object itself, you can use `#convert_to!` method as:
|
86
86
|
|
87
87
|
```ruby
|
88
88
|
UnitMeasurements::Weight.new(1, :kg).convert_to!(:g)
|
89
|
-
#=> 1000 g
|
89
|
+
#=> 1000.0 g
|
90
90
|
```
|
91
91
|
|
92
92
|
You can also chain call of `#convert_to` and `#convert_to!` methods as:
|
@@ -100,18 +100,18 @@ UnitMeasurements::Weight.new(1, :kg).convert_to(:g).convert_to(:t).convert_to!(:
|
|
100
100
|
|
101
101
|
```ruby
|
102
102
|
UnitMeasurements::Weight.parse("1 kg")
|
103
|
-
#=> 1 kg
|
103
|
+
#=> 1.0 kg
|
104
104
|
```
|
105
105
|
|
106
106
|
**Parse string that mentions quantity, source unit, and target unit:**
|
107
107
|
|
108
108
|
```ruby
|
109
109
|
UnitMeasurements::Weight.parse("1 kg to g")
|
110
|
-
#=> 1000 g
|
110
|
+
#=> 1000.0 g
|
111
111
|
UnitMeasurements::Weight.parse("1 kg as g")
|
112
|
-
#=> 1000 g
|
112
|
+
#=> 1000.0 g
|
113
113
|
UnitMeasurements::Weight.parse("1 kg in g")
|
114
|
-
#=> 1000 g
|
114
|
+
#=> 1000.0 g
|
115
115
|
```
|
116
116
|
|
117
117
|
**Parse rational numbers, source unit, and (or) target unit:**
|
@@ -144,58 +144,58 @@ UnitMeasurements::Weight.parse("2+3i kg to g")
|
|
144
144
|
|
145
145
|
```ruby
|
146
146
|
UnitMeasurements::Weight.new(BigDecimal(2), :kg).convert_to(:g)
|
147
|
-
#=> 2000 g
|
147
|
+
#=> 2000.0 g
|
148
148
|
UnitMeasurements::Weight.new(0.2e1, :kg).convert_to(:g)
|
149
|
-
#=> 2000 g
|
149
|
+
#=> 2000.0 g
|
150
150
|
UnitMeasurements::Weight.parse("0.2e1 kg").convert_to(:g)
|
151
|
-
#=> 2000 g
|
151
|
+
#=> 2000.0 g
|
152
152
|
UnitMeasurements::Weight.parse("0.2e1 kg to g")
|
153
|
-
#=> 2000 g
|
153
|
+
#=> 2000.0 g
|
154
154
|
```
|
155
155
|
|
156
156
|
**Parse ratios, source unit, and (or) target unit:**
|
157
157
|
|
158
158
|
```ruby
|
159
159
|
UnitMeasurements::Weight.new("1:2", :kg).convert_to(:g)
|
160
|
-
#=> 500 g
|
160
|
+
#=> 500.0 g
|
161
161
|
UnitMeasurements::Weight.parse("1:2 kg").convert_to(:g)
|
162
|
-
#=> 500 g
|
162
|
+
#=> 500.0 g
|
163
163
|
UnitMeasurements::Weight.parse("1:2 kg to g")
|
164
|
-
#=> 500 g
|
164
|
+
#=> 500.0 g
|
165
165
|
```
|
166
166
|
|
167
167
|
**Parse fractional notations, source unit, and (or) target unit:**
|
168
168
|
|
169
169
|
```ruby
|
170
170
|
UnitMeasurements::Weight.new("1/2", :kg).convert_to(:g)
|
171
|
-
#=> 500 g
|
171
|
+
#=> 500.0 g
|
172
172
|
UnitMeasurements::Weight.parse("1/2 kg").convert_to(:g)
|
173
|
-
#=> 500 g
|
173
|
+
#=> 500.0 g
|
174
174
|
UnitMeasurements::Weight.parse("1/2 kg to g")
|
175
|
-
#=> 500 g
|
175
|
+
#=> 500.0 g
|
176
176
|
UnitMeasurements::Weight.new("½", :kg).convert_to(:g)
|
177
|
-
#=> 500 g
|
177
|
+
#=> 500.0 g
|
178
178
|
UnitMeasurements::Weight.parse("½ kg").convert_to(:g)
|
179
|
-
#=> 500 g
|
179
|
+
#=> 500.0 g
|
180
180
|
UnitMeasurements::Weight.parse("½ kg to g")
|
181
|
-
#=> 500 g
|
181
|
+
#=> 500.0 g
|
182
182
|
```
|
183
183
|
|
184
184
|
**Parse mixed fractional notations, source unit, and (or) target unit:**
|
185
185
|
|
186
186
|
```ruby
|
187
187
|
UnitMeasurements::Weight.new("2 1/2", :kg).convert_to(:g)
|
188
|
-
#=> 2500 g
|
188
|
+
#=> 2500.0 g
|
189
189
|
UnitMeasurements::Weight.parse("2 1/2 kg").convert_to(:g)
|
190
|
-
#=> 2500 g
|
190
|
+
#=> 2500.0 g
|
191
191
|
UnitMeasurements::Weight.parse("2 1/2 kg to g")
|
192
|
-
#=> 2500 g
|
192
|
+
#=> 2500.0 g
|
193
193
|
UnitMeasurements::Weight.new("2 ½", :kg).convert_to(:g)
|
194
|
-
#=> 2500 g
|
194
|
+
#=> 2500.0 g
|
195
195
|
UnitMeasurements::Weight.parse("2 ½ kg").convert_to(:g)
|
196
|
-
#=> 2500 g
|
196
|
+
#=> 2500.0 g
|
197
197
|
UnitMeasurements::Weight.parse("2 ½ kg to g")
|
198
|
-
#=> 2500 g
|
198
|
+
#=> 2500.0 g
|
199
199
|
```
|
200
200
|
|
201
201
|
Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓`, `⅔`, `⅕`, `⅖`, `⅗`, `⅘`, `⅙`, `⅚`, `⅐`, `⅛`, `⅜`, `⅝`, `⅞`, `⅑`, `⅒`, `↉`, `⁄`.
|
@@ -204,17 +204,17 @@ Supported special characters for fractional notations are `¼`, `½`, `¾`, `⅓
|
|
204
204
|
|
205
205
|
```ruby
|
206
206
|
UnitMeasurements::Weight.new("2e+2", :kg).convert_to(:g)
|
207
|
-
#=> 200000 g
|
207
|
+
#=> 200000.0 g
|
208
208
|
UnitMeasurements::Weight.parse("2e² kg").convert_to(:g)
|
209
|
-
#=> 200000 g
|
209
|
+
#=> 200000.0 g
|
210
210
|
UnitMeasurements::Weight.parse("2e+2 kg to g")
|
211
|
-
#=> 200000 g
|
211
|
+
#=> 200000.0 g
|
212
212
|
UnitMeasurements::Weight.new("2e⁺²", :kg).convert_to(:g)
|
213
|
-
#=> 200000 g
|
213
|
+
#=> 200000.0 g
|
214
214
|
UnitMeasurements::Weight.parse("2e⁺2 kg").convert_to(:g)
|
215
|
-
#=> 200000 g
|
215
|
+
#=> 200000.0 g
|
216
216
|
UnitMeasurements::Weight.parse("2e⁻² kg to g")
|
217
|
-
#=> 20 g
|
217
|
+
#=> 20.0 g
|
218
218
|
```
|
219
219
|
|
220
220
|
Supported special characters for exponents are `⁰`, `¹`, `²`, `³`, `⁴`, `⁵`, `⁶`, `⁷`, `⁸`, `⁹`, `⁺`, `⁻`.
|
@@ -236,9 +236,9 @@ UnitMeasurements::Weight.parse("2 kg").to(:st).format("%.4<quantity>f")
|
|
236
236
|
**Extract the unit and the quantity from measurement:**
|
237
237
|
|
238
238
|
```ruby
|
239
|
-
weight = UnitMeasurements::Weight.new(1
|
239
|
+
weight = UnitMeasurements::Weight.new(1, :kg)
|
240
240
|
weight.quantity
|
241
|
-
#=>
|
241
|
+
#=> 1
|
242
242
|
weight.unit
|
243
243
|
#=> #<UnitMeasurements::Unit: kg (kilogram, kilogramme, kilogrammes, kilograms)>
|
244
244
|
```
|
@@ -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,12 +319,16 @@ 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
|
326
329
|
|
327
|
-
You have
|
328
|
-
different units within
|
330
|
+
You have ability to perform arithmetic operations on measurements with the same or
|
331
|
+
different units within a same unit group. You can perform arithmetic operations on
|
329
332
|
measurement by either other compatible measurement or number.
|
330
333
|
|
331
334
|
**Methods:**
|
@@ -26,6 +26,7 @@ module UnitMeasurements
|
|
26
26
|
return self if target_unit == unit
|
27
27
|
|
28
28
|
conversion_factor = (unit.conversion_factor / target_unit.conversion_factor)
|
29
|
+
|
29
30
|
self.class.new((quantity * conversion_factor), target_unit)
|
30
31
|
end
|
31
32
|
alias_method :to, :convert_to
|
@@ -33,6 +34,7 @@ module UnitMeasurements
|
|
33
34
|
def convert_to!(target_unit)
|
34
35
|
measurement = convert_to(target_unit)
|
35
36
|
@quantity, @unit = measurement.quantity, measurement.unit
|
37
|
+
|
36
38
|
self
|
37
39
|
end
|
38
40
|
alias_method :to!, :convert_to!
|
@@ -44,7 +46,16 @@ module UnitMeasurements
|
|
44
46
|
end
|
45
47
|
|
46
48
|
def to_s
|
47
|
-
"#{
|
49
|
+
"#{quantity} #{unit}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def quantity
|
53
|
+
case @quantity
|
54
|
+
when Rational
|
55
|
+
@quantity.denominator == 1 ? @quantity.numerator : @quantity
|
56
|
+
else
|
57
|
+
@quantity
|
58
|
+
end
|
48
59
|
end
|
49
60
|
|
50
61
|
class << self
|
@@ -61,6 +72,7 @@ module UnitMeasurements
|
|
61
72
|
def parse(input)
|
62
73
|
input = Normalizer.normalize(input)
|
63
74
|
source, target = input.match(CONVERSION_STRING_REGEXP)&.captures
|
75
|
+
|
64
76
|
target ? _parse(source).convert_to(target) : _parse(source)
|
65
77
|
end
|
66
78
|
|
@@ -68,6 +80,7 @@ module UnitMeasurements
|
|
68
80
|
|
69
81
|
def _parse(string)
|
70
82
|
quantity, unit = Parser.parse(string)
|
83
|
+
|
71
84
|
new(quantity, unit)
|
72
85
|
end
|
73
86
|
end
|
@@ -83,6 +96,7 @@ module UnitMeasurements
|
|
83
96
|
when String
|
84
97
|
quantity = Normalizer.normalize(quantity)
|
85
98
|
quantity, _ = Parser.parse(quantity)
|
99
|
+
|
86
100
|
quantity
|
87
101
|
else
|
88
102
|
quantity
|
@@ -92,15 +106,5 @@ module UnitMeasurements
|
|
92
106
|
def unit_from_unit_or_name!(value)
|
93
107
|
value.is_a?(Unit) ? value : self.class.unit_group.unit_for!(value)
|
94
108
|
end
|
95
|
-
|
96
|
-
def humanized_quantity
|
97
|
-
case quantity
|
98
|
-
when Complex
|
99
|
-
quantity
|
100
|
-
when Numeric
|
101
|
-
num = quantity.to_r
|
102
|
-
num.denominator == 1 ? num.numerator.to_s : num.to_f.to_s
|
103
|
-
end
|
104
|
-
end
|
105
109
|
end
|
106
110
|
end
|
@@ -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
|
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.5.1
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|