composite_unit_measurements 0.4.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 +20 -2
- data/Gemfile.lock +1 -1
- data/README.md +23 -13
- data/lib/composite_unit_measurements/length.rb +43 -1
- data/lib/composite_unit_measurements/time.rb +192 -8
- 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,8 +1,26 @@
|
|
1
|
-
## [0.
|
1
|
+
## [0.6.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.5.0...v0.6.0) - 2023-12-12
|
2
2
|
|
3
3
|
### What's new
|
4
4
|
|
5
|
-
- Added
|
5
|
+
- Added support to parse `tonne-kilogramme` weight measurements.
|
6
|
+
- Added support to parse `second-millisecond` and `year-month` time measurements.
|
7
|
+
|
8
|
+
-----------
|
9
|
+
|
10
|
+
## [0.5.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.4.0...v0.5.0) - 2023-12-10
|
11
|
+
|
12
|
+
### What's new
|
13
|
+
|
14
|
+
- Added ability to parse `mile-yard` length measurement.
|
15
|
+
- Added ability to parse `minute-second`, `week-day`, and `month-day` time measurements.
|
16
|
+
|
17
|
+
-----------
|
18
|
+
|
19
|
+
## [0.4.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.3.0...v0.4.0) - 2023-12-08
|
20
|
+
|
21
|
+
### What's new
|
22
|
+
|
23
|
+
- Added ability to parse `litre-millilitre` volume measurement.
|
6
24
|
- Updated readme with parsing examples.
|
7
25
|
|
8
26
|
-----------
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Composite Unit Measurements
|
2
2
|
|
3
|
-
A collection of specialized parsers designed for handling composite
|
3
|
+
A collection of specialized parsers designed for handling composite measurements.
|
4
4
|
|
5
5
|
[](https://github.com/shivam091/composite_unit_measurements/actions/workflows/main.yml)
|
6
6
|
[](https://badge.fury.io/rb/composite_unit_measurements)
|
@@ -9,12 +9,13 @@ A collection of specialized parsers designed for handling composite measurement
|
|
9
9
|
[](https://codeclimate.com/github/shivam091/composite_unit_measurements/test_coverage)
|
10
10
|
[](https://github.com/shivam091/composite_unit_measurements/blob/main/LICENSE)
|
11
11
|
|
12
|
-
**Harshal V. Ladhe,
|
12
|
+
**[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**
|
13
13
|
|
14
14
|
## Introduction
|
15
15
|
|
16
|
-
The `composite_unit_measurements` gem offers versatile parsers for efficiently
|
17
|
-
composite
|
16
|
+
The `composite_unit_measurements` gem offers versatile parsers for efficiently
|
17
|
+
parsing composite measurements. Leveraging the power of the [`unit_measurements`]
|
18
|
+
(https://github.com/shivam091/unit_measurements) gem,
|
18
19
|
it enables smooth handling of composite measurements in various units.
|
19
20
|
|
20
21
|
## Minimum Requirements
|
@@ -41,15 +42,15 @@ Or otherwise simply install it yourself as:
|
|
41
42
|
## Usage
|
42
43
|
|
43
44
|
Each packaged parser includes the `.parse` method to parse composite measurements.
|
44
|
-
You can use an appropriate parser to
|
45
|
-
is returned in the leftmost unit of your measurement.
|
45
|
+
You can use an appropriate parser to parse measurements and the final result of
|
46
|
+
parser is returned in the leftmost unit of your measurement.
|
46
47
|
|
47
|
-
The result of each parser method returns an instance of measurement on which we
|
48
|
-
perform any functionality offered by `unit_measurements`.
|
48
|
+
The result of each parser method returns an instance of measurement on which we
|
49
|
+
can perform any functionality offered by `unit_measurements`.
|
49
50
|
|
50
|
-
This gem internally uses
|
51
|
-
|
52
|
-
|
51
|
+
This gem internally uses `unit_measurements` to perform conversions and arithmetic
|
52
|
+
operations. You can build supported composite measurements using any
|
53
|
+
[unit alias](https://github.com/shivam091/unit_measurements/blob/main/units.md).
|
53
54
|
|
54
55
|
### Examples
|
55
56
|
|
@@ -79,7 +80,8 @@ CompositeUnitMeasurements::Volume.parse("2 l 250 ml") #=> 2.25 l
|
|
79
80
|
|
80
81
|
### Support for numeric types
|
81
82
|
|
82
|
-
Each parser can handle various numeric types, including scientific notation,
|
83
|
+
Each parser can handle various numeric types, including scientific notation,
|
84
|
+
rational numbers, and complex numbers.
|
83
85
|
|
84
86
|
```ruby
|
85
87
|
CompositeUnitMeasurements::Length.parse("1+2i ft 12 in") #=> 2.0+2.0i ft
|
@@ -91,21 +93,29 @@ CompositeUnitMeasurements::Length.parse("1e-2 ft 1+2i in") #=> 0.09333333333333
|
|
91
93
|
|
92
94
|
## Packaged parsers & supported composite measurements
|
93
95
|
|
94
|
-
The `composite_unit_measurements` gem supports parsing various composite measurements,
|
96
|
+
The `composite_unit_measurements` gem supports parsing various composite measurements,
|
97
|
+
including:
|
95
98
|
|
96
99
|
**1. CompositeUnitMeasurements::Length**
|
97
100
|
- metre-centimetre (6 m 50 cm)
|
98
101
|
- kilometre-metre (5 km 500 m)
|
99
102
|
- foot-inch (5 ft 6 in)
|
103
|
+
- mile-yard (20 mi 220 yd)
|
100
104
|
|
101
105
|
**2. CompositeUnitMeasurements::Weight**
|
102
106
|
- kilogramme-gramme (4 kg 500 g)
|
103
107
|
- pound-ounce (8 lb 12 oz)
|
104
108
|
- stone-pound (2 st 6 lb)
|
109
|
+
- tonne-kilogramme (1 t 500 kg)
|
105
110
|
|
106
111
|
**3. CompositeUnitMeasurements::Time**
|
107
112
|
- hour-minute (3 h 45 min)
|
108
113
|
- hour-minute-second-microsecond (12:60,3600:360000000)
|
114
|
+
- minute-second (10 min 90 s)
|
115
|
+
- week-day (8 wk 3 d)
|
116
|
+
- month-day (2 mo 60 d)
|
117
|
+
- second-millisecond (8 s 500 ms)
|
118
|
+
- year-month (3 yr 4 mo)
|
109
119
|
|
110
120
|
**4. CompositeUnitMeasurements::Volume**
|
111
121
|
- litre-millilitre (2 l 250 ml)
|
@@ -20,6 +20,8 @@ module CompositeUnitMeasurements
|
|
20
20
|
# CompositeUnitMeasurements::Length.parse("5 km 500 m") #=> 5.5 km
|
21
21
|
# @example Parse 'foot-inch' measurement:
|
22
22
|
# CompositeUnitMeasurements::Length.parse("5 ft 6 in") #=> 5.5 ft
|
23
|
+
# @example Parse 'mile-yard' measurement:
|
24
|
+
# CompositeUnitMeasurements::Length.parse("20 mi 220 yd") #=> 20.125 mi
|
23
25
|
#
|
24
26
|
# @param [String] string The string to parse for length measurement.
|
25
27
|
# @return [UnitMeasurements::Length]
|
@@ -35,6 +37,7 @@ module CompositeUnitMeasurements
|
|
35
37
|
when FOOT_INCH then parse_foot_inch(string)
|
36
38
|
when KILOMETRE_METRE then parse_kilometre_metre(string)
|
37
39
|
when METRE_CENTIMETRE then parse_metre_centimetre(string)
|
40
|
+
when MILE_YARD then parse_mile_yard(string)
|
38
41
|
else raise UnitMeasurements::ParseError, string
|
39
42
|
end
|
40
43
|
end
|
@@ -100,6 +103,26 @@ module CompositeUnitMeasurements
|
|
100
103
|
UnitMeasurements::Length.new(kilometre, "km") + UnitMeasurements::Length.new(metre, "m")
|
101
104
|
end
|
102
105
|
end
|
106
|
+
|
107
|
+
# @private
|
108
|
+
# Parses a +string+ representing a length in the format of +mile-yard+
|
109
|
+
# into a +UnitMeasurements::Length+ object.
|
110
|
+
#
|
111
|
+
# @param [String] string
|
112
|
+
# The string representing length measurement in the format of *mile-yard*.
|
113
|
+
# @return [UnitMeasurements::Length]
|
114
|
+
# Returns a UnitMeasurements::Length object if parsing is successful.
|
115
|
+
#
|
116
|
+
# @see MILE_YARD
|
117
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
118
|
+
# @since 0.5.0
|
119
|
+
def parse_mile_yard(string)
|
120
|
+
mile, yard = string.match(MILE_YARD)&.captures
|
121
|
+
|
122
|
+
if mile && yard
|
123
|
+
UnitMeasurements::Length.new(mile, "mi") + UnitMeasurements::Length.new(yard, "yd")
|
124
|
+
end
|
125
|
+
end
|
103
126
|
end
|
104
127
|
|
105
128
|
# Regex pattern for aliases of +foot+ unit.
|
@@ -132,6 +155,18 @@ module CompositeUnitMeasurements
|
|
132
155
|
# @since 0.3.0
|
133
156
|
KILOMETRE_ALIASES = /(?:km|kilometer(?:s)?|kilometre(?:s)?)/.freeze
|
134
157
|
|
158
|
+
# Regex pattern for aliases of +mile+ unit.
|
159
|
+
#
|
160
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
161
|
+
# @since 0.5.0
|
162
|
+
MILE_ALIASES = /(?:mi|mile(?:s)?)/.freeze
|
163
|
+
|
164
|
+
# Regex pattern for aliases of +yard+ unit.
|
165
|
+
#
|
166
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
167
|
+
# @since 0.5.0
|
168
|
+
YARD_ALIASES = /(?:yd|yard(?:s)?)/.freeze
|
169
|
+
|
135
170
|
# Regex pattern for parsing a length measurement in the format of +foot-inch+.
|
136
171
|
#
|
137
172
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
@@ -150,7 +185,14 @@ module CompositeUnitMeasurements
|
|
150
185
|
# @since 0.3.0
|
151
186
|
KILOMETRE_METRE = /\A#{ANY_NUMBER}\s*#{KILOMETRE_ALIASES}\s*#{ANY_NUMBER}\s*#{METRE_ALIASES}\z/.freeze
|
152
187
|
|
188
|
+
# Regex pattern for parsing a length measurement in the format of +mile-yard+.
|
189
|
+
#
|
190
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
191
|
+
# @since 0.5.0
|
192
|
+
MILE_YARD = /\A#{ANY_NUMBER}\s*#{MILE_ALIASES}\s*#{ANY_NUMBER}\s*#{YARD_ALIASES}\z/.freeze
|
193
|
+
|
153
194
|
private_constant :FOOT_ALIASES, :INCH_ALIASES, :METRE_ALIASES, :CENTIMETRE_ALIASES,
|
154
|
-
:KILOMETRE_ALIASES, :
|
195
|
+
:KILOMETRE_ALIASES, :MILE_ALIASES, :YARD_ALIASES, :MILE_YARD,
|
196
|
+
:FOOT_INCH, :KILOMETRE_METRE, :METRE_CENTIMETRE
|
155
197
|
end
|
156
198
|
end
|
@@ -19,6 +19,16 @@ module CompositeUnitMeasurements
|
|
19
19
|
# @example Parse 'duration':
|
20
20
|
# CompositeUnitMeasurements::Time.parse("12:60:3600,360000000") #=> 14.1 h
|
21
21
|
# CompositeUnitMeasurements::Time.parse("12:60:3600") #=> 14.0 h
|
22
|
+
# @example Parse 'minute-second' measurement:
|
23
|
+
# CompositeUnitMeasurements::Time.parse("10 min 90 s") #=> 11.5 min
|
24
|
+
# @example Parse 'week-day' measurement:
|
25
|
+
# CompositeUnitMeasurements::Time.parse("8 wk 3 d") #=> 8.428571428571429 wk
|
26
|
+
# @example Parse 'month-day' measurement:
|
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
|
22
32
|
#
|
23
33
|
# @param [String] string The string to parse for time measurement.
|
24
34
|
# @return [UnitMeasurements::Time]
|
@@ -30,9 +40,14 @@ module CompositeUnitMeasurements
|
|
30
40
|
# @since 0.2.0
|
31
41
|
def parse(string)
|
32
42
|
case string
|
33
|
-
when HOUR_MINUTE
|
34
|
-
when DURATION
|
35
|
-
|
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
|
36
51
|
end
|
37
52
|
end
|
38
53
|
|
@@ -76,10 +91,110 @@ module CompositeUnitMeasurements
|
|
76
91
|
hour, minute, second, microsecond = string.match(DURATION)&.captures
|
77
92
|
raise ArgumentError, "Invalid Duration" if [hour, minute, second, microsecond].all?(&:nil?)
|
78
93
|
|
79
|
-
UnitMeasurements::Time.new((hour || 0),
|
80
|
-
UnitMeasurements::Time.new((minute || 0),
|
81
|
-
UnitMeasurements::Time.new((second || 0),
|
82
|
-
UnitMeasurements::Time.new((microsecond || 0),
|
94
|
+
UnitMeasurements::Time.new((hour || 0), "h") +
|
95
|
+
UnitMeasurements::Time.new((minute || 0), "min") +
|
96
|
+
UnitMeasurements::Time.new((second || 0), "s") +
|
97
|
+
UnitMeasurements::Time.new((microsecond || 0), "μs")
|
98
|
+
end
|
99
|
+
|
100
|
+
# @private
|
101
|
+
# Parses a +string+ representing a time in the format of +minute-second+
|
102
|
+
# into a +UnitMeasurements::Time+ object.
|
103
|
+
#
|
104
|
+
# @param [String] string
|
105
|
+
# The string representing time measurement in the format of *minute-second*.
|
106
|
+
# @return [UnitMeasurements::Time]
|
107
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
108
|
+
#
|
109
|
+
# @see MINUTE_SECOND
|
110
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
111
|
+
# @since 0.5.0
|
112
|
+
def parse_minute_second(string)
|
113
|
+
minute, second = string.match(MINUTE_SECOND)&.captures
|
114
|
+
|
115
|
+
if minute && second
|
116
|
+
UnitMeasurements::Time.new(minute, "min") + UnitMeasurements::Time.new(second, "s")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# @private
|
121
|
+
# Parses a +string+ representing a time in the format of +week-day+
|
122
|
+
# into a +UnitMeasurements::Time+ object.
|
123
|
+
#
|
124
|
+
# @param [String] string
|
125
|
+
# The string representing time measurement in the format of *week-day*.
|
126
|
+
# @return [UnitMeasurements::Time]
|
127
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
128
|
+
#
|
129
|
+
# @see WEEK_DAY
|
130
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
131
|
+
# @since 0.5.0
|
132
|
+
def parse_week_day(string)
|
133
|
+
week, day = string.match(WEEK_DAY)&.captures
|
134
|
+
|
135
|
+
if week && day
|
136
|
+
UnitMeasurements::Time.new(week, "wk") + UnitMeasurements::Time.new(day, "d")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# @private
|
141
|
+
# Parses a +string+ representing a time in the format of +month-day+
|
142
|
+
# into a +UnitMeasurements::Time+ object.
|
143
|
+
#
|
144
|
+
# @param [String] string
|
145
|
+
# The string representing time measurement in the format of *month-day*.
|
146
|
+
# @return [UnitMeasurements::Time]
|
147
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
148
|
+
#
|
149
|
+
# @see MONTH_DAY
|
150
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
151
|
+
# @since 0.5.0
|
152
|
+
def parse_month_day(string)
|
153
|
+
month, day = string.match(MONTH_DAY)&.captures
|
154
|
+
|
155
|
+
if month && day
|
156
|
+
UnitMeasurements::Time.new(month, "mo") + UnitMeasurements::Time.new(day, "d")
|
157
|
+
end
|
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
|
83
198
|
end
|
84
199
|
end
|
85
200
|
|
@@ -95,6 +210,42 @@ module CompositeUnitMeasurements
|
|
95
210
|
# @since 0.3.0
|
96
211
|
MINUTE_ALIASES = /(?:min|minute(?:s)?)/.freeze
|
97
212
|
|
213
|
+
# Regex pattern for aliases of +second+ unit.
|
214
|
+
#
|
215
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
216
|
+
# @since 0.5.0
|
217
|
+
SECOND_ALIASES = /(?:s|sec|second(?:s)?)/.freeze
|
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
|
+
|
225
|
+
# Regex pattern for aliases of +day+ unit.
|
226
|
+
#
|
227
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
228
|
+
# @since 0.5.0
|
229
|
+
DAY_ALIASES = /(?:d|day(?:s)?)/.freeze
|
230
|
+
|
231
|
+
# Regex pattern for aliases of +week+ unit.
|
232
|
+
#
|
233
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
234
|
+
# @since 0.5.0
|
235
|
+
WEEK_ALIASES = /(?:wk|week(?:s)?)/.freeze
|
236
|
+
|
237
|
+
# Regex pattern for aliases of +month+ unit.
|
238
|
+
#
|
239
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
240
|
+
# @since 0.5.0
|
241
|
+
MONTH_ALIASES = /(?:mo|month(?:s)?)/.freeze
|
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
|
+
|
98
249
|
# Regex pattern for parsing a time measurement in the format of +hour-minute+.
|
99
250
|
#
|
100
251
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
@@ -108,6 +259,39 @@ module CompositeUnitMeasurements
|
|
108
259
|
# @since 0.2.0
|
109
260
|
DURATION = /\A(?<hour>#{REAL_NUMBER}):(?<min>#{REAL_NUMBER}):(?:(?<sec>#{REAL_NUMBER}))?(?:,(?<msec>#{REAL_NUMBER}))?\z/.freeze
|
110
261
|
|
111
|
-
|
262
|
+
# Regex pattern for parsing a time measurement in the format of +minute-second+.
|
263
|
+
#
|
264
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
265
|
+
# @since 0.5.0
|
266
|
+
MINUTE_SECOND = /\A#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\s*#{ANY_NUMBER}\s*#{SECOND_ALIASES}\z/.freeze
|
267
|
+
|
268
|
+
# Regex pattern for parsing a time measurement in the format of +week-day+.
|
269
|
+
#
|
270
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
271
|
+
# @since 0.5.0
|
272
|
+
WEEK_DAY = /\A#{ANY_NUMBER}\s*#{WEEK_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
|
273
|
+
|
274
|
+
# Regex pattern for parsing a time measurement in the format of +month-day+.
|
275
|
+
#
|
276
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
277
|
+
# @since 0.5.0
|
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
|
291
|
+
|
292
|
+
private_constant :HOUR_ALIASES, :MINUTE_ALIASES, :SECOND_ALIASES, :DAY_ALIASES,
|
293
|
+
:WEEK_ALIASES, :MONTH_ALIASES, :YEAR_ALIASES, :MILLISECOND_ALIASES,
|
294
|
+
:HOUR_MINUTE, :DURATION, :MINUTE_SECOND, :WEEK_DAY, :MONTH_DAY,
|
295
|
+
:SECOND_MILLISECOND, :YEAR_MONTH
|
112
296
|
end
|
113
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
|