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 +4 -4
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +3 -0
- data/lib/composite_unit_measurements/time.rb +80 -9
- data/lib/composite_unit_measurements/version.rb +1 -1
- data/lib/composite_unit_measurements/weight.rb +40 -4
- 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: 4b3ee724bfce2ea0502643679d1779360a70977d89dc946a855457b982f7b4c9
|
4
|
+
data.tar.gz: 1b971b28b9a8d9d6c72476c9c4bee08de44fc97f9670497245de616f0658872f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
40
|
-
when DURATION
|
41
|
-
when MINUTE_SECOND
|
42
|
-
when WEEK_DAY
|
43
|
-
when MONTH_DAY
|
44
|
-
|
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
|
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, :
|
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
|
@@ -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
|
-
|
153
|
-
|
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.
|
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:
|
11
|
+
date: 2025-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|