unit_measurements 5.9.0 → 5.10.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 +11 -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 +6 -4
- 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: 519b4112a693a9c722e1241381eafb095005e4cbdef9792662e339df52087abf
|
4
|
+
data.tar.gz: 34054c7ef9056fe40d25f698403f0585b805c7426710302b102e93709582090b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbfdf296b4fab0d7b2c5a7932939ca9cba3205d42f9709d72b070b98748b97cd68a4a29d10c9f086fbd9decf65f906a977458f902e70a04ba2ee019326062f5a
|
7
|
+
data.tar.gz: df52b1126595b98d905a9f9b86b9884dcddd4c06b3ac547ba8bbac0d2dc4108d35329b1a629d278bf4cadf2959d4923e27be14a08702d8b5fd911affc977a0db
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## [5.10.0](https://github.com/shivam091/unit_measurements/compare/v5.9.0...v5.10.0) - 2023-11-09
|
2
|
+
|
3
|
+
### What's new
|
4
|
+
|
5
|
+
- Added new method `#to_fs` to format the measurement.
|
6
|
+
- Aliased arithmetic method `#**` to `#pow` and `#^`.
|
7
|
+
- Aliased arithmetic method `#-@` to `#inverse` and `#negate`.
|
8
|
+
- Added `UnitMeasurements::BlankQuantityError` error if tried to initialize the `Measurement` with a blank quantity.
|
9
|
+
- Added `UnitMeasurements::BlankUnitError` error if tried to initialize the `Measurement` with a blank unit.
|
10
|
+
|
11
|
+
### What's deprecated
|
12
|
+
|
13
|
+
- `#format` method in favour of `#to_fs`.
|
14
|
+
|
15
|
+
----------
|
16
|
+
|
1
17
|
## [5.9.0](https://github.com/shivam091/unit_measurements/compare/v5.8.0...v5.9.0) - 2023-11-08
|
2
18
|
|
3
19
|
### 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.
|
@@ -198,7 +192,7 @@ UnitMeasurements::Length.new(100, "m").to("in").to_fs("%.4<quantity>f %<unit>s")
|
|
198
192
|
You can check more about formatting along with their examples
|
199
193
|
[here](https://shivam091.github.io/unit_measurements/UnitMeasurements/Formatter.html).
|
200
194
|
|
201
|
-
**Extract the
|
195
|
+
**Extract the quantity and the unit from measurement:**
|
202
196
|
|
203
197
|
```ruby
|
204
198
|
length = UnitMeasurements::Length.new(1, "km")
|
@@ -208,6 +202,15 @@ length.unit
|
|
208
202
|
#=> #<UnitMeasurements::Unit: km (kilometer, kilometers, kilometre, kilometres)>
|
209
203
|
```
|
210
204
|
|
205
|
+
Unit object can be interrogated for a range of attributes:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
length.unit.aliases # Alternative names for the unit.
|
209
|
+
#=> #<Set: {"kilometer", "kilometers", "kilometre", "kilometres"}>
|
210
|
+
length.unit.conversion_factor # Conversion factor relative to primitive unit.
|
211
|
+
#=> 1000.0
|
212
|
+
```
|
213
|
+
|
211
214
|
**See primitive unit of the unit group:**
|
212
215
|
|
213
216
|
```ruby
|
@@ -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)
|
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.10.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-09 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
|