unit_measurements 5.9.0 → 5.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08305aaf9558055931b73b625212941d80d625e042933de660624d1786af7bc9'
4
- data.tar.gz: 28b9f54eeb207abef81d9d908b040e1e1da4e7426c1f69d0be2332f3611c0d16
3
+ metadata.gz: 519b4112a693a9c722e1241381eafb095005e4cbdef9792662e339df52087abf
4
+ data.tar.gz: 34054c7ef9056fe40d25f698403f0585b805c7426710302b102e93709582090b
5
5
  SHA512:
6
- metadata.gz: b2e8e679fff2e9590c5c8a3f4a72da21ad58c216d0556154529b5e5ad9cab92f78f4dc2ec7c8f7297653755c5cadbd8e246546b7092b983b14c824ccd5acf3ff
7
- data.tar.gz: 006037a17c4b9616550757994f7e384b2344a3fc6651a05243cf19e4e326101bfdb90cb9075e070c54759c9a113d853a43157c1333994b0e4b088db3e2cfb504
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- unit_measurements (5.9.0)
4
+ unit_measurements (5.10.0)
5
5
  activesupport (~> 7.0)
6
6
 
7
7
  GEM
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 unit and the quantity from measurement:**
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 BaseError, "Configuration#use_cache= only accepts true or false, but received #{use_cache}"
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 +DEFAULT_FORMAT+ for formatting the
31
+ # If +format+ is not specified, it uses {DEFAULT_FORMAT} for formatting the
23
32
  # measurement.
24
33
  #
25
- # The format method allows for customization of the output format of a
26
- # measurement. It uses format placeholders for +quantity+ and +unit+. If no
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").format
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").format("%.4<quantity>f %<unit>s")
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 1.1.0
45
- def format(format = nil)
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 [BaseError] If +quantity+ or +unit+ is blank.
107
+ # @raise [BlankQuantityError] If +quantity+ is blank.
108
+ # @raise [BlankUnitError] If +unit+ is blank.
108
109
  #
109
- # @see BaseError
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 BaseError, "Quantity cannot be blank." if quantity.blank?
114
- raise BaseError, "Unit cannot be blank." if unit.blank?
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)
@@ -4,5 +4,5 @@
4
4
 
5
5
  module UnitMeasurements
6
6
  # Current stable version.
7
- VERSION = "5.9.0"
7
+ VERSION = "5.10.0"
8
8
  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: 5.9.0
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-08 00:00:00.000000000 Z
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