composite_unit_measurements 0.5.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66b4aa8b083b2ce04d0b18a00eda81ca826aa514e66adcf15c6d909fca23bf7b
4
- data.tar.gz: d8482df4c187ecc55f03b0e192f732f4708b8a3c102576f6575a48410098f72b
3
+ metadata.gz: 4b3ee724bfce2ea0502643679d1779360a70977d89dc946a855457b982f7b4c9
4
+ data.tar.gz: 1b971b28b9a8d9d6c72476c9c4bee08de44fc97f9670497245de616f0658872f
5
5
  SHA512:
6
- metadata.gz: 40bdb5fe245ad06dc61527cf0a2f70d76a122d118bdb50b3ec2ad2276d38fe881fc4c2a5b803904bc8c6c1df4447e2bd0de399280b2c35425c1c18439faef134
7
- data.tar.gz: d33a783ce26f8d28d6a9668915c6664a1526924449b792d619df0c270bc1a4472be69b337d6696d80249ec61d6485988f3fb13b516b043545594902cd2ba67ea
6
+ metadata.gz: 076635ea3ccbcab0c65d0baae50da36351d89c96a88f774cc0f4db579332ace45135f49974756e6f6fff4e8bc92c86fdde7ad19251cac97a82eb9b79ebaa7d64
7
+ data.tar.gz: 38e4adbd5231dc8d65134178f05548713f42f2709929d33f416ca4f0d1536635d42c1389e669619be193ab0d78df09b8a8908918a4d10683f27be18b4cc13bc8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.6.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.5.0...v0.6.0) - 2023-12-12
2
+
3
+ ### What's new
4
+
5
+ - Added support to parse `tonne-kilogramme` weight measurements.
6
+ - Added support to parse `second-millisecond` and `year-month` time measurements.
7
+
8
+ -----------
9
+
1
10
  ## [0.5.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.4.0...v0.5.0) - 2023-12-10
2
11
 
3
12
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- composite_unit_measurements (0.5.0)
4
+ composite_unit_measurements (0.6.0)
5
5
  activesupport (~> 7.0)
6
6
  unit_measurements (~> 5)
7
7
 
data/README.md CHANGED
@@ -106,6 +106,7 @@ including:
106
106
  - kilogramme-gramme (4 kg 500 g)
107
107
  - pound-ounce (8 lb 12 oz)
108
108
  - stone-pound (2 st 6 lb)
109
+ - tonne-kilogramme (1 t 500 kg)
109
110
 
110
111
  **3. CompositeUnitMeasurements::Time**
111
112
  - hour-minute (3 h 45 min)
@@ -113,6 +114,8 @@ including:
113
114
  - minute-second (10 min 90 s)
114
115
  - week-day (8 wk 3 d)
115
116
  - month-day (2 mo 60 d)
117
+ - second-millisecond (8 s 500 ms)
118
+ - year-month (3 yr 4 mo)
116
119
 
117
120
  **4. CompositeUnitMeasurements::Volume**
118
121
  - litre-millilitre (2 l 250 ml)
@@ -25,6 +25,10 @@ module CompositeUnitMeasurements
25
25
  # CompositeUnitMeasurements::Time.parse("8 wk 3 d") #=> 8.428571428571429 wk
26
26
  # @example Parse 'month-day' measurement:
27
27
  # CompositeUnitMeasurements::Time.parse("2 mo 60 d") #=> 3.97260057797197 mo
28
+ # @example Parse 'second-millisecond' measurement:
29
+ # CompositeUnitMeasurements::Time.parse("8 s 500 ms") #=> 8.5 s
30
+ # @example Parse 'year-month' measurement:
31
+ # CompositeUnitMeasurements::Time.parse("3 yr 4 mo") #=> 3.333333698630137 yr
28
32
  #
29
33
  # @param [String] string The string to parse for time measurement.
30
34
  # @return [UnitMeasurements::Time]
@@ -36,12 +40,14 @@ module CompositeUnitMeasurements
36
40
  # @since 0.2.0
37
41
  def parse(string)
38
42
  case string
39
- when HOUR_MINUTE then parse_hour_minute(string)
40
- when DURATION then parse_duration(string)
41
- when MINUTE_SECOND then parse_minute_second(string)
42
- when WEEK_DAY then parse_week_day(string)
43
- when MONTH_DAY then parse_month_day(string)
44
- else raise UnitMeasurements::ParseError, string
43
+ when HOUR_MINUTE then parse_hour_minute(string)
44
+ when DURATION then parse_duration(string)
45
+ when MINUTE_SECOND then parse_minute_second(string)
46
+ when WEEK_DAY then parse_week_day(string)
47
+ when MONTH_DAY then parse_month_day(string)
48
+ when SECOND_MILLISECOND then parse_second_millisecond(string)
49
+ when YEAR_MONTH then parse_year_month(string)
50
+ else raise UnitMeasurements::ParseError, string
45
51
  end
46
52
  end
47
53
 
@@ -150,6 +156,46 @@ module CompositeUnitMeasurements
150
156
  UnitMeasurements::Time.new(month, "mo") + UnitMeasurements::Time.new(day, "d")
151
157
  end
152
158
  end
159
+
160
+ # @private
161
+ # Parses a +string+ representing a time in the format of +second-millisecond+
162
+ # into a +UnitMeasurements::Time+ object.
163
+ #
164
+ # @param [String] string
165
+ # The string representing time measurement in the format of *second-millisecond*.
166
+ # @return [UnitMeasurements::Time]
167
+ # Returns a UnitMeasurements::Time object if parsing is successful.
168
+ #
169
+ # @see SECOND_MILLISECOND
170
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
171
+ # @since 0.6.0
172
+ def parse_second_millisecond(string)
173
+ second, millisecond = string.match(SECOND_MILLISECOND)&.captures
174
+
175
+ if second && millisecond
176
+ UnitMeasurements::Time.new(second, "s") + UnitMeasurements::Time.new(millisecond, "ms")
177
+ end
178
+ end
179
+
180
+ # @private
181
+ # Parses a +string+ representing a time in the format of +year-month+
182
+ # into a +UnitMeasurements::Time+ object.
183
+ #
184
+ # @param [String] string
185
+ # The string representing time measurement in the format of *year-month*.
186
+ # @return [UnitMeasurements::Time]
187
+ # Returns a UnitMeasurements::Time object if parsing is successful.
188
+ #
189
+ # @see YEAR_MONTH
190
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
191
+ # @since 0.6.0
192
+ def parse_year_month(string)
193
+ year, month = string.match(YEAR_MONTH)&.captures
194
+
195
+ if year && month
196
+ UnitMeasurements::Time.new(year, "yr") + UnitMeasurements::Time.new(month, "mo")
197
+ end
198
+ end
153
199
  end
154
200
 
155
201
  # Regex pattern for aliases of +hour+ unit.
@@ -170,6 +216,12 @@ module CompositeUnitMeasurements
170
216
  # @since 0.5.0
171
217
  SECOND_ALIASES = /(?:s|sec|second(?:s)?)/.freeze
172
218
 
219
+ # Regex pattern for aliases of +millisecond+ unit.
220
+ #
221
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
222
+ # @since 0.6.0
223
+ MILLISECOND_ALIASES = /(?:ms|millisec|millisecond(?:s)?)/.freeze
224
+
173
225
  # Regex pattern for aliases of +day+ unit.
174
226
  #
175
227
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -188,6 +240,12 @@ module CompositeUnitMeasurements
188
240
  # @since 0.5.0
189
241
  MONTH_ALIASES = /(?:mo|month(?:s)?)/.freeze
190
242
 
243
+ # Regex pattern for aliases of +year+ unit.
244
+ #
245
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
246
+ # @since 0.6.0
247
+ YEAR_ALIASES = /(?:y|yr|year(?:s)?)/.freeze
248
+
191
249
  # Regex pattern for parsing a time measurement in the format of +hour-minute+.
192
250
  #
193
251
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -217,10 +275,23 @@ module CompositeUnitMeasurements
217
275
  #
218
276
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
219
277
  # @since 0.5.0
220
- MONTH_DAY = /\A#{ANY_NUMBER}\s#{MONTH_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
278
+ MONTH_DAY = /\A#{ANY_NUMBER}\s*#{MONTH_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
279
+
280
+ # Regex pattern for parsing a time measurement in the format of +second-millisecond+.
281
+ #
282
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
283
+ # @since 0.6.0
284
+ SECOND_MILLISECOND = /\A#{ANY_NUMBER}\s*#{SECOND_ALIASES}\s*#{ANY_NUMBER}\s*#{MILLISECOND_ALIASES}\z/.freeze
285
+
286
+ # Regex pattern for parsing a time measurement in the format of +year-month.
287
+ #
288
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
289
+ # @since 0.6.0
290
+ YEAR_MONTH = /\A#{ANY_NUMBER}\s*#{YEAR_ALIASES}\s*#{ANY_NUMBER}\s*#{MONTH_ALIASES}\z/.freeze
221
291
 
222
292
  private_constant :HOUR_ALIASES, :MINUTE_ALIASES, :SECOND_ALIASES, :DAY_ALIASES,
223
- :WEEK_ALIASES, :MONTH_ALIASES, :HOUR_MINUTE, :DURATION,
224
- :MINUTE_SECOND, :WEEK_DAY, :MONTH_DAY
293
+ :WEEK_ALIASES, :MONTH_ALIASES, :YEAR_ALIASES, :MILLISECOND_ALIASES,
294
+ :HOUR_MINUTE, :DURATION, :MINUTE_SECOND, :WEEK_DAY, :MONTH_DAY,
295
+ :SECOND_MILLISECOND, :YEAR_MONTH
225
296
  end
226
297
  end
@@ -25,5 +25,5 @@
25
25
  # @since 0.1.0
26
26
  module CompositeUnitMeasurements
27
27
  # Current stable version
28
- VERSION = "0.5.0"
28
+ VERSION = "0.6.0"
29
29
  end
@@ -14,12 +14,14 @@ module CompositeUnitMeasurements
14
14
  class << self
15
15
  # Parses a given +string+ into a +UnitMeasurements::Weight+ object.
16
16
  #
17
- # @example Parse 'kilogramme-gramme' measurement:
18
- # CompositeUnitMeasurements::Weight.parse("4 kg 500 g") #=> 4.5 kg
19
17
  # @example Parse 'pound-ounce' measurement:
20
18
  # CompositeUnitMeasurements::Weight.parse("8 lb 12 oz") #=> 8.75 lb
21
19
  # @example Parse 'stone-pound' measurement:
22
20
  # CompositeUnitMeasurements::Weight.parse("2 st 6 lb") #=> 2.428571428571429 st
21
+ # @example Parse 'kilogramme-gramme' measurement:
22
+ # CompositeUnitMeasurements::Weight.parse("4 kg 500 g") #=> 4.5 kg
23
+ # @example Parse 'tonne-kilogramme' measurement:
24
+ # CompositeUnitMeasurements::Weight.parse("1 t 500 kg") #=> 1.5 t
23
25
  #
24
26
  # @param [String] string The string to parse for weight measurement.
25
27
  # @return [UnitMeasurements::Weight]
@@ -34,6 +36,7 @@ module CompositeUnitMeasurements
34
36
  when POUND_OUNCE then parse_pound_ounce(string)
35
37
  when STONE_POUND then parse_stone_pound(string)
36
38
  when KILOGRAMME_GRAMME then parse_kilogramme_gramme(string)
39
+ when TONNE_KILOGRAMME then parse_tonne_kilogramme(string)
37
40
  else raise UnitMeasurements::ParseError, string
38
41
  end
39
42
  end
@@ -99,6 +102,26 @@ module CompositeUnitMeasurements
99
102
  UnitMeasurements::Weight.new(kilogramme, "kg") + UnitMeasurements::Weight.new(gramme, "g")
100
103
  end
101
104
  end
105
+
106
+ # @private
107
+ # Parses a +string+ representing a weight in the format of +tonne-kilogramme+
108
+ # into a +UnitMeasurements::Weight+ object.
109
+ #
110
+ # @param [String] string
111
+ # The string representing weight measurement in the format of *tonne-kilogramme*.
112
+ # @return [UnitMeasurements::Weight]
113
+ # Returns a UnitMeasurements::Weight object if parsing is successful.
114
+ #
115
+ # @see TONNE_KILOGRAMME
116
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
117
+ # @since 0.6.0
118
+ def parse_tonne_kilogramme(string)
119
+ tonne, kilogramme = string.match(TONNE_KILOGRAMME)&.captures
120
+
121
+ if tonne && kilogramme
122
+ UnitMeasurements::Weight.new(tonne, "t") + UnitMeasurements::Weight.new(kilogramme, "kg")
123
+ end
124
+ end
102
125
  end
103
126
 
104
127
  # Regex pattern for aliases of +pound+ unit.
@@ -131,6 +154,12 @@ module CompositeUnitMeasurements
131
154
  # @since 0.3.0
132
155
  KILOGRAMME_ALIASES = /(?:kg|kilogram(?:s)?|kilogramme(?:s)?)/.freeze
133
156
 
157
+ # Regex pattern for aliases of +tonne+ or +metric tonne+ unit.
158
+ #
159
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
160
+ # @since 0.6.0
161
+ TONNE_ALIASES = /(?:t|tonne(?:s)?|metric tonne(?:s)?)/.freeze
162
+
134
163
  # Regex pattern for parsing a weight measurement in the format of +pound-ounce+.
135
164
  #
136
165
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -149,7 +178,14 @@ module CompositeUnitMeasurements
149
178
  # @since 0.3.0
150
179
  KILOGRAMME_GRAMME = /\A#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\s*#{ANY_NUMBER}\s*#{GRAMME_ALIASES}\z/.freeze
151
180
 
152
- private_constant :KILOGRAMME_GRAMME, :POUND_ALIASES, :OUNCE_ALIASES, :STONE_ALIASES ,
153
- :GRAMME_ALIASES, :KILOGRAMME_ALIASES, :POUND_OUNCE, :STONE_POUND
181
+ # Regex pattern for parsing a weight measurement in the format of +tonne-kilogramme+.
182
+ #
183
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
184
+ # @since 0.6.0
185
+ TONNE_KILOGRAMME = /\A#{ANY_NUMBER}\s*#{TONNE_ALIASES}\s*#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\z/.freeze
186
+
187
+ private_constant :POUND_ALIASES, :OUNCE_ALIASES, :STONE_ALIASES, :GRAMME_ALIASES,
188
+ :KILOGRAMME_ALIASES, :TONNE_ALIASES, :POUND_OUNCE, :STONE_POUND,
189
+ :KILOGRAMME_GRAMME, :TONNE_KILOGRAMME
154
190
  end
155
191
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_unit_measurements
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.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-12-10 00:00:00.000000000 Z
11
+ date: 2025-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport