composite_unit_measurements 0.4.0 → 0.5.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 +11 -2
- data/Gemfile.lock +1 -1
- data/README.md +20 -13
- data/lib/composite_unit_measurements/length.rb +43 -1
- data/lib/composite_unit_measurements/time.rb +121 -8
- data/lib/composite_unit_measurements/version.rb +1 -1
- 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: 66b4aa8b083b2ce04d0b18a00eda81ca826aa514e66adcf15c6d909fca23bf7b
|
4
|
+
data.tar.gz: d8482df4c187ecc55f03b0e192f732f4708b8a3c102576f6575a48410098f72b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40bdb5fe245ad06dc61527cf0a2f70d76a122d118bdb50b3ec2ad2276d38fe881fc4c2a5b803904bc8c6c1df4447e2bd0de399280b2c35425c1c18439faef134
|
7
|
+
data.tar.gz: d33a783ce26f8d28d6a9668915c6664a1526924449b792d619df0c270bc1a4472be69b337d6696d80249ec61d6485988f3fb13b516b043545594902cd2ba67ea
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,17 @@
|
|
1
|
-
## [0.
|
1
|
+
## [0.5.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.4.0...v0.5.0) - 2023-12-10
|
2
2
|
|
3
3
|
### What's new
|
4
4
|
|
5
|
-
- Added ability to parse `
|
5
|
+
- Added ability to parse `mile-yard` length measurement.
|
6
|
+
- Added ability to parse `minute-second`, `week-day`, and `month-day` time measurements.
|
7
|
+
|
8
|
+
-----------
|
9
|
+
|
10
|
+
## [0.4.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.3.0...v0.4.0) - 2023-12-08
|
11
|
+
|
12
|
+
### What's new
|
13
|
+
|
14
|
+
- Added ability to parse `litre-millilitre` volume measurement.
|
6
15
|
- Updated readme with parsing examples.
|
7
16
|
|
8
17
|
-----------
|
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,12 +93,14 @@ 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)
|
@@ -106,6 +110,9 @@ The `composite_unit_measurements` gem supports parsing various composite measure
|
|
106
110
|
**3. CompositeUnitMeasurements::Time**
|
107
111
|
- hour-minute (3 h 45 min)
|
108
112
|
- hour-minute-second-microsecond (12:60,3600:360000000)
|
113
|
+
- minute-second (10 min 90 s)
|
114
|
+
- week-day (8 wk 3 d)
|
115
|
+
- month-day (2 mo 60 d)
|
109
116
|
|
110
117
|
**4. CompositeUnitMeasurements::Volume**
|
111
118
|
- 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,12 @@ 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
|
22
28
|
#
|
23
29
|
# @param [String] string The string to parse for time measurement.
|
24
30
|
# @return [UnitMeasurements::Time]
|
@@ -30,9 +36,12 @@ module CompositeUnitMeasurements
|
|
30
36
|
# @since 0.2.0
|
31
37
|
def parse(string)
|
32
38
|
case string
|
33
|
-
when HOUR_MINUTE
|
34
|
-
when DURATION
|
35
|
-
|
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
|
36
45
|
end
|
37
46
|
end
|
38
47
|
|
@@ -76,10 +85,70 @@ module CompositeUnitMeasurements
|
|
76
85
|
hour, minute, second, microsecond = string.match(DURATION)&.captures
|
77
86
|
raise ArgumentError, "Invalid Duration" if [hour, minute, second, microsecond].all?(&:nil?)
|
78
87
|
|
79
|
-
UnitMeasurements::Time.new((hour || 0),
|
80
|
-
UnitMeasurements::Time.new((minute || 0),
|
81
|
-
UnitMeasurements::Time.new((second || 0),
|
82
|
-
UnitMeasurements::Time.new((microsecond || 0),
|
88
|
+
UnitMeasurements::Time.new((hour || 0), "h") +
|
89
|
+
UnitMeasurements::Time.new((minute || 0), "min") +
|
90
|
+
UnitMeasurements::Time.new((second || 0), "s") +
|
91
|
+
UnitMeasurements::Time.new((microsecond || 0), "μs")
|
92
|
+
end
|
93
|
+
|
94
|
+
# @private
|
95
|
+
# Parses a +string+ representing a time in the format of +minute-second+
|
96
|
+
# into a +UnitMeasurements::Time+ object.
|
97
|
+
#
|
98
|
+
# @param [String] string
|
99
|
+
# The string representing time measurement in the format of *minute-second*.
|
100
|
+
# @return [UnitMeasurements::Time]
|
101
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
102
|
+
#
|
103
|
+
# @see MINUTE_SECOND
|
104
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
105
|
+
# @since 0.5.0
|
106
|
+
def parse_minute_second(string)
|
107
|
+
minute, second = string.match(MINUTE_SECOND)&.captures
|
108
|
+
|
109
|
+
if minute && second
|
110
|
+
UnitMeasurements::Time.new(minute, "min") + UnitMeasurements::Time.new(second, "s")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# @private
|
115
|
+
# Parses a +string+ representing a time in the format of +week-day+
|
116
|
+
# into a +UnitMeasurements::Time+ object.
|
117
|
+
#
|
118
|
+
# @param [String] string
|
119
|
+
# The string representing time measurement in the format of *week-day*.
|
120
|
+
# @return [UnitMeasurements::Time]
|
121
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
122
|
+
#
|
123
|
+
# @see WEEK_DAY
|
124
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
125
|
+
# @since 0.5.0
|
126
|
+
def parse_week_day(string)
|
127
|
+
week, day = string.match(WEEK_DAY)&.captures
|
128
|
+
|
129
|
+
if week && day
|
130
|
+
UnitMeasurements::Time.new(week, "wk") + UnitMeasurements::Time.new(day, "d")
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# @private
|
135
|
+
# Parses a +string+ representing a time in the format of +month-day+
|
136
|
+
# into a +UnitMeasurements::Time+ object.
|
137
|
+
#
|
138
|
+
# @param [String] string
|
139
|
+
# The string representing time measurement in the format of *month-day*.
|
140
|
+
# @return [UnitMeasurements::Time]
|
141
|
+
# Returns a UnitMeasurements::Time object if parsing is successful.
|
142
|
+
#
|
143
|
+
# @see MONTH_DAY
|
144
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
145
|
+
# @since 0.5.0
|
146
|
+
def parse_month_day(string)
|
147
|
+
month, day = string.match(MONTH_DAY)&.captures
|
148
|
+
|
149
|
+
if month && day
|
150
|
+
UnitMeasurements::Time.new(month, "mo") + UnitMeasurements::Time.new(day, "d")
|
151
|
+
end
|
83
152
|
end
|
84
153
|
end
|
85
154
|
|
@@ -95,6 +164,30 @@ module CompositeUnitMeasurements
|
|
95
164
|
# @since 0.3.0
|
96
165
|
MINUTE_ALIASES = /(?:min|minute(?:s)?)/.freeze
|
97
166
|
|
167
|
+
# Regex pattern for aliases of +second+ unit.
|
168
|
+
#
|
169
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
170
|
+
# @since 0.5.0
|
171
|
+
SECOND_ALIASES = /(?:s|sec|second(?:s)?)/.freeze
|
172
|
+
|
173
|
+
# Regex pattern for aliases of +day+ unit.
|
174
|
+
#
|
175
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
176
|
+
# @since 0.5.0
|
177
|
+
DAY_ALIASES = /(?:d|day(?:s)?)/.freeze
|
178
|
+
|
179
|
+
# Regex pattern for aliases of +week+ unit.
|
180
|
+
#
|
181
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
182
|
+
# @since 0.5.0
|
183
|
+
WEEK_ALIASES = /(?:wk|week(?:s)?)/.freeze
|
184
|
+
|
185
|
+
# Regex pattern for aliases of +month+ unit.
|
186
|
+
#
|
187
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
188
|
+
# @since 0.5.0
|
189
|
+
MONTH_ALIASES = /(?:mo|month(?:s)?)/.freeze
|
190
|
+
|
98
191
|
# Regex pattern for parsing a time measurement in the format of +hour-minute+.
|
99
192
|
#
|
100
193
|
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
@@ -108,6 +201,26 @@ module CompositeUnitMeasurements
|
|
108
201
|
# @since 0.2.0
|
109
202
|
DURATION = /\A(?<hour>#{REAL_NUMBER}):(?<min>#{REAL_NUMBER}):(?:(?<sec>#{REAL_NUMBER}))?(?:,(?<msec>#{REAL_NUMBER}))?\z/.freeze
|
110
203
|
|
111
|
-
|
204
|
+
# Regex pattern for parsing a time measurement in the format of +minute-second+.
|
205
|
+
#
|
206
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
207
|
+
# @since 0.5.0
|
208
|
+
MINUTE_SECOND = /\A#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\s*#{ANY_NUMBER}\s*#{SECOND_ALIASES}\z/.freeze
|
209
|
+
|
210
|
+
# Regex pattern for parsing a time measurement in the format of +week-day+.
|
211
|
+
#
|
212
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
213
|
+
# @since 0.5.0
|
214
|
+
WEEK_DAY = /\A#{ANY_NUMBER}\s*#{WEEK_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
|
215
|
+
|
216
|
+
# Regex pattern for parsing a time measurement in the format of +month-day+.
|
217
|
+
#
|
218
|
+
# @author {Harshal V. Ladhe}[https://shivam091.github.io/]
|
219
|
+
# @since 0.5.0
|
220
|
+
MONTH_DAY = /\A#{ANY_NUMBER}\s#{MONTH_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
|
221
|
+
|
222
|
+
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
|
112
225
|
end
|
113
226
|
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.5.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-
|
11
|
+
date: 2023-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|