unit_measurements 5.9.0 → 5.11.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 +25 -0
- data/Gemfile.lock +1 -1
- data/README.md +25 -8
- data/lib/unit_measurements/arithmetic.rb +4 -0
- data/lib/unit_measurements/base.rb +2 -0
- data/lib/unit_measurements/configuration.rb +1 -1
- data/lib/unit_measurements/errors/blank_quantity_error.rb +21 -0
- data/lib/unit_measurements/errors/blank_unit_error.rb +21 -0
- data/lib/unit_measurements/formatter.rb +16 -9
- data/lib/unit_measurements/math.rb +2 -2
- data/lib/unit_measurements/measurement.rb +41 -5
- data/lib/unit_measurements/unit_group.rb +16 -0
- data/lib/unit_measurements/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca26bc8965c93c692a72e788ab2577f2bad297909372a72d3cc2fec93a45b5bd
|
4
|
+
data.tar.gz: 581d716cb56cf59c89eeb69d01095c4c0c14054006dc6bb0822580e3d6750ffd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed994225ecb853f8ce577e0b53a863b0f091980ee33f054b50f69d156ad0a7e393a4a59d962d665aa52fa6cdc077f19f42bc27d0b1d8e7413f359dbde8e6a9fb
|
7
|
+
data.tar.gz: 889e45c784390c89a46079067ca9493253e638b1117ee88c16c5ab2ed60db89a855a11bf487f2a6536b1729a2704983fbfcb5fa60959a2b8eed79dae4957d920
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,28 @@
|
|
1
|
+
## [5.11.0](https://github.com/shivam091/unit_measurements/compare/v5.10.0...v5.11.0) - 2023-11-11
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added `#systems` method to return unit systems within the unit group.
|
6
|
+
- Added `#ratio` method to calculate the ratio between two units.
|
7
|
+
|
8
|
+
----------
|
9
|
+
|
10
|
+
## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09
|
11
|
+
|
12
|
+
### What's new
|
13
|
+
|
14
|
+
- Added new method `#to_fs` to format the measurement.
|
15
|
+
- Aliased arithmetic method `#**` to `#pow` and `#^`.
|
16
|
+
- Aliased arithmetic method `#-@` to `#inverse` and `#negate`.
|
17
|
+
- Added `UnitMeasurements::BlankQuantityError` error if tried to initialize the `Measurement` with a blank quantity.
|
18
|
+
- Added `UnitMeasurements::BlankUnitError` error if tried to initialize the `Measurement` with a blank unit.
|
19
|
+
|
20
|
+
### What's deprecated
|
21
|
+
|
22
|
+
- `#format` method in favour of `#to_fs`.
|
23
|
+
|
24
|
+
----------
|
25
|
+
|
1
26
|
## [5.9.0](https://github.com/shivam091/unit_measurements/compare/v5.8.0...v5.9.0) - 2023-11-08
|
2
27
|
|
3
28
|
### What's new
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -95,6 +95,7 @@ UnitMeasurements::Length.new(1, "km")
|
|
95
95
|
|
96
96
|
This gem allows you to convert among units of same unit group. You can convert measurement to other unit using `#convert_to`
|
97
97
|
(aliased as `#to`, `#in`, and `#as`) or `#convert_to!` (aliased as `#to!`, `#in!`, and `#as!`) methods.
|
98
|
+
You can also chain call of `#convert_to` and `#convert_to!` methods.
|
98
99
|
|
99
100
|
These methods provide `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
|
100
101
|
|
@@ -119,13 +120,6 @@ UnitMeasurements::Length.new(1, "cm").convert_to("primitive")
|
|
119
120
|
#=> 0.01 m
|
120
121
|
```
|
121
122
|
|
122
|
-
You can also chain call of `#convert_to` and `#convert_to!` methods as:
|
123
|
-
|
124
|
-
```ruby
|
125
|
-
UnitMeasurements::Length.new(100, "m").convert_to("ft").convert_to!("in", use_cache: true)
|
126
|
-
#=> 3937.00787401574071916010498688 in
|
127
|
-
```
|
128
|
-
|
129
123
|
**Parse string without having to split out the quantity and source unit:**
|
130
124
|
|
131
125
|
This method provides `use_cache` parameter which defaults to `false` to indicate whether the caching of conversion factors should happen.
|
@@ -185,6 +179,13 @@ UnitMeasurements::Length.parse("1:2 km to m")
|
|
185
179
|
#=> 500.0 m
|
186
180
|
```
|
187
181
|
|
182
|
+
**Calculate the ratio between two units:**
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
UnitMeasurements::Length.ratio("in", "ft")
|
186
|
+
#=> "12.0 in/ft"
|
187
|
+
```
|
188
|
+
|
188
189
|
**Formatting measurement:**
|
189
190
|
|
190
191
|
If you want to format the measurement to certain format, you can use `#to_fs` method.
|
@@ -198,7 +199,7 @@ UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
|
|
198
199
|
You can check more about formatting along with their examples
|
199
200
|
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Formatter.html).
|
200
201
|
|
201
|
-
**Extract the
|
202
|
+
**Extract the quantity and the unit from measurement:**
|
202
203
|
|
203
204
|
```ruby
|
204
205
|
length = UnitMeasurements::Length.new(1, "km")
|
@@ -208,6 +209,15 @@ length.unit
|
|
208
209
|
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
|
209
210
|
```
|
210
211
|
|
212
|
+
Unit object can be interrogated for a range of attributes:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
length.unit.aliases # Alternative names for the unit.
|
216
|
+
#=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
|
217
|
+
length.unit.conversion_factor # Conversion factor relative to primitive unit.
|
218
|
+
#=> 1000.0
|
219
|
+
```
|
220
|
+
|
211
221
|
**See primitive unit of the unit group:**
|
212
222
|
|
213
223
|
```ruby
|
@@ -236,6 +246,13 @@ UnitMeasurements::Length.unit_names_with_aliases
|
|
236
246
|
#=> ["\"", "'", "feet", "foot", "ft", "in", "inch", "inches", "m", "meter", "meters", "metre", "metres", "mi", "mile", "miles", "yard", "yards", "yd"]
|
237
247
|
```
|
238
248
|
|
249
|
+
**See list of unit systems defined within the unit group:**
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
UnitMeasurements::Length.systems
|
253
|
+
#=> ["metric", "imperial", "us_customary", "astronomical"]
|
254
|
+
```
|
255
|
+
|
239
256
|
**See list of units within the unit system:**
|
240
257
|
|
241
258
|
You can use `#units_for` or `#units_for!` methods to find units within the unit system.
|
@@ -134,6 +134,8 @@ module UnitMeasurements
|
|
134
134
|
def **(other)
|
135
135
|
arithmetic_operation(other, :**)
|
136
136
|
end
|
137
|
+
alias_method :pow, :**
|
138
|
+
alias_method :^, :**
|
137
139
|
|
138
140
|
# Negates the quantity of the measurement.
|
139
141
|
#
|
@@ -151,6 +153,8 @@ module UnitMeasurements
|
|
151
153
|
def -@
|
152
154
|
self.class.new(-self.quantity, self.unit)
|
153
155
|
end
|
156
|
+
alias_method :inverse, :-@
|
157
|
+
alias_method :negate, :-@
|
154
158
|
|
155
159
|
# Checks whether the quantity of the measurement is nonzero.
|
156
160
|
#
|
@@ -148,3 +148,5 @@ require "unit_measurements/errors/unit_error"
|
|
148
148
|
require "unit_measurements/errors/parse_error"
|
149
149
|
require "unit_measurements/errors/unit_already_defined_error"
|
150
150
|
require "unit_measurements/errors/primitive_unit_already_set_error"
|
151
|
+
require "unit_measurements/errors/blank_quantity_error"
|
152
|
+
require "unit_measurements/errors/blank_unit_error"
|
@@ -55,7 +55,7 @@ module UnitMeasurements
|
|
55
55
|
# @since 5.3.0
|
56
56
|
def use_cache=(use_cache)
|
57
57
|
unless [true, false].include?(use_cache)
|
58
|
-
raise
|
58
|
+
raise ArgumentError, "Configuration#use_cache= only accepts true or false, but received #{use_cache}"
|
59
59
|
end
|
60
60
|
|
61
61
|
@use_cache = use_cache
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
# The +UnitMeasurements::BlankQuantityError+ class represents an error that
|
7
|
+
# occurs when trying to initialize the +Measurement+ with a blank quantity.
|
8
|
+
#
|
9
|
+
# @see BaseError
|
10
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
11
|
+
# @since 5.10.0
|
12
|
+
class BlankQuantityError < BaseError
|
13
|
+
# Initializes a new +BlankQuantityError+ instance.
|
14
|
+
#
|
15
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
16
|
+
# @since 5.10.0
|
17
|
+
def initialize
|
18
|
+
super("Quantity cannot be blank.")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# -*- frozen_string_literal: true -*-
|
3
|
+
# -*- warn_indent: true -*-
|
4
|
+
|
5
|
+
module UnitMeasurements
|
6
|
+
# The +UnitMeasurements::BlankUnitError+ class represents an error that
|
7
|
+
# occurs when trying to initialize the +Measurement+ with a blank unit.
|
8
|
+
#
|
9
|
+
# @see BaseError
|
10
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
11
|
+
# @since 5.10.0
|
12
|
+
class BlankUnitError < BaseError
|
13
|
+
# Initializes a new +BlankUnitError+ instance.
|
14
|
+
#
|
15
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
16
|
+
# @since 5.10.0
|
17
|
+
def initialize
|
18
|
+
super("Unit cannot be blank.")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -18,19 +18,27 @@ module UnitMeasurements
|
|
18
18
|
# containing placeholders for +quantity+ and +unit+.
|
19
19
|
DEFAULT_FORMAT = "%.2<quantity>f %<unit>s".freeze
|
20
20
|
|
21
|
+
# @deprecated This method has been deprecated in favour of {#to_fs}.
|
22
|
+
#
|
23
|
+
# This method is no longer recommended for use. Please use {#to_fs}
|
24
|
+
# instead.
|
25
|
+
def format(format = nil)
|
26
|
+
warn "DEPRECATION WARNING: The `format` method is deprecated and will be removed in upcoming release. Please use `to_fs` instead."
|
27
|
+
to_fs(format)
|
28
|
+
end
|
29
|
+
|
21
30
|
# Formats measurement to certain formatted string specified by +format+.
|
22
|
-
# If +format+ is not specified, it uses
|
31
|
+
# If +format+ is not specified, it uses {DEFAULT_FORMAT} for formatting the
|
23
32
|
# measurement.
|
24
33
|
#
|
25
|
-
# The
|
26
|
-
# measurement. It uses format placeholders for +quantity+ and +unit+.
|
27
|
-
# custom format is provided, it will use the +DEFAULT_FORMAT+.
|
34
|
+
# The {#to_fs} method allows for customization of the output format of a
|
35
|
+
# measurement. It uses format placeholders for +quantity+ and +unit+.
|
28
36
|
#
|
29
37
|
# @example
|
30
|
-
# UnitMeasurements::Length.new(1, "m").to("in").
|
38
|
+
# UnitMeasurements::Length.new(1, "m").to("in").to_fs
|
31
39
|
# => "39.37 in"
|
32
40
|
#
|
33
|
-
# UnitMeasurements::Length.new(1, "m").to("in").
|
41
|
+
# UnitMeasurements::Length.new(1, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
|
34
42
|
# => "39.3701 in"
|
35
43
|
#
|
36
44
|
# @param [String, optional] format
|
@@ -41,10 +49,9 @@ module UnitMeasurements
|
|
41
49
|
#
|
42
50
|
# @see DEFAULT_FORMAT
|
43
51
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
44
|
-
# @since
|
45
|
-
def
|
52
|
+
# @since 5.10.0
|
53
|
+
def to_fs(format = nil)
|
46
54
|
kwargs = {quantity: quantity, unit: unit.to_s}
|
47
|
-
|
48
55
|
(format || DEFAULT_FORMAT) % kwargs
|
49
56
|
end
|
50
57
|
end
|
@@ -64,7 +64,7 @@ module UnitMeasurements
|
|
64
64
|
#
|
65
65
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
66
66
|
# @since 1.6.0
|
67
|
-
def floor(ndigits =0)
|
67
|
+
def floor(ndigits = 0)
|
68
68
|
self.class.new(quantity.floor(ndigits), unit)
|
69
69
|
end
|
70
70
|
|
@@ -84,7 +84,7 @@ module UnitMeasurements
|
|
84
84
|
#
|
85
85
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
86
86
|
# @since 1.6.0
|
87
|
-
def ceil(ndigits =0)
|
87
|
+
def ceil(ndigits = 0)
|
88
88
|
self.class.new(quantity.ceil(ndigits), unit)
|
89
89
|
end
|
90
90
|
end
|
@@ -104,14 +104,16 @@ module UnitMeasurements
|
|
104
104
|
# @param [Numeric|String] quantity The quantity of the measurement.
|
105
105
|
# @param [String|Unit] unit The unit of the measurement.
|
106
106
|
#
|
107
|
-
# @raise [
|
107
|
+
# @raise [BlankQuantityError] If +quantity+ is blank.
|
108
|
+
# @raise [BlankUnitError] If +unit+ is blank.
|
108
109
|
#
|
109
|
-
# @see
|
110
|
+
# @see BlankQuantityError
|
111
|
+
# @see BlankUnitError
|
110
112
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
111
113
|
# @since 1.0.0
|
112
114
|
def initialize(quantity, unit)
|
113
|
-
raise
|
114
|
-
raise
|
115
|
+
raise BlankQuantityError if quantity.blank?
|
116
|
+
raise BlankUnitError if unit.blank?
|
115
117
|
|
116
118
|
@quantity = convert_quantity(quantity)
|
117
119
|
@unit = unit_from_unit_or_name!(unit)
|
@@ -241,7 +243,7 @@ module UnitMeasurements
|
|
241
243
|
def_delegators :unit_group, :primitive, :units, :cache_file, :unit_names,
|
242
244
|
:unit_with_name_and_aliases, :unit_names_with_aliases,
|
243
245
|
:unit_for, :unit_for!, :defined?, :unit_or_alias?, :[],
|
244
|
-
:units_for, :units_for
|
246
|
+
:units_for, :units_for!, :systems
|
245
247
|
|
246
248
|
# Parses an input string and returns a +Measurement+ instance depending on
|
247
249
|
# the input string. This method first normalizes the +input+ internally,
|
@@ -366,6 +368,40 @@ module UnitMeasurements
|
|
366
368
|
cached.clear_cache
|
367
369
|
end
|
368
370
|
|
371
|
+
# Calculates the ratio between two units.
|
372
|
+
#
|
373
|
+
# This method takes a source unit and a target unit, and returns the ratio
|
374
|
+
# between them as a string representation.
|
375
|
+
#
|
376
|
+
# @example Calculating the ratio between 'in' and 'ft':
|
377
|
+
# UnitMeasurements::Length.ratio("in", "ft")
|
378
|
+
# => "12.0 in/ft"
|
379
|
+
#
|
380
|
+
# UnitMeasurements::Length.ratio(UnitMeasurements::Length.unit_for("in"), "ft")
|
381
|
+
# => "12.0 in/ft"
|
382
|
+
#
|
383
|
+
# @param [Unit|String|Symbol] source_unit
|
384
|
+
# The source unit for the ratio calculation.
|
385
|
+
# @param [Unit|String|Symbol] target_unit
|
386
|
+
# The target unit for the ratio calculation.
|
387
|
+
#
|
388
|
+
# @return [String] The ratio between the source and target units.
|
389
|
+
#
|
390
|
+
# @raise [UnitError]
|
391
|
+
# If either the source unit or the target unit is not found in the unit group.
|
392
|
+
#
|
393
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
394
|
+
# @since 5.11.0
|
395
|
+
def ratio(source_unit, target_unit)
|
396
|
+
source_unit = source_unit.is_a?(Unit) ? source_unit : unit_for!(source_unit)
|
397
|
+
target_unit = target_unit.is_a?(Unit) ? target_unit : unit_for!(target_unit)
|
398
|
+
|
399
|
+
source_quantity = 1
|
400
|
+
target_quantity = new(source_quantity, target_unit).convert_to(source_unit).quantity
|
401
|
+
|
402
|
+
"#{target_quantity} #{source_unit}/#{target_unit}"
|
403
|
+
end
|
404
|
+
|
369
405
|
private
|
370
406
|
|
371
407
|
# @private
|
@@ -247,6 +247,22 @@ module UnitMeasurements
|
|
247
247
|
system_units
|
248
248
|
end
|
249
249
|
|
250
|
+
# Returns an array of unit system names defined within the unit group.
|
251
|
+
#
|
252
|
+
# It scans through the units and compiles a list of unique system names.
|
253
|
+
#
|
254
|
+
# @example
|
255
|
+
# UnitMeasurements::Length.systems
|
256
|
+
# => ["metric", "imperial", "us_customary", "astronomical"]
|
257
|
+
#
|
258
|
+
# @return [Array<String>] An array of unit system names.
|
259
|
+
#
|
260
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
261
|
+
# @since 5.11.0
|
262
|
+
def systems
|
263
|
+
units.map { _1.system }.uniq
|
264
|
+
end
|
265
|
+
|
250
266
|
private
|
251
267
|
|
252
268
|
# @private
|
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: 5.
|
4
|
+
version: 5.11.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-11-
|
11
|
+
date: 2023-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -111,6 +111,8 @@ files:
|
|
111
111
|
- lib/unit_measurements/comparison.rb
|
112
112
|
- lib/unit_measurements/configuration.rb
|
113
113
|
- lib/unit_measurements/conversion.rb
|
114
|
+
- lib/unit_measurements/errors/blank_quantity_error.rb
|
115
|
+
- lib/unit_measurements/errors/blank_unit_error.rb
|
114
116
|
- lib/unit_measurements/errors/parse_error.rb
|
115
117
|
- lib/unit_measurements/errors/primitive_unit_already_set_error.rb
|
116
118
|
- lib/unit_measurements/errors/unit_already_defined_error.rb
|