composite_unit_measurements 0.3.0 → 0.5.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: cd67813e0e00bce4c907e7076ec4a62994570585875da38dfa72bc968b3c0c20
4
- data.tar.gz: c9de7ccfd6e0ede94d8cd5976174a1a60ed4f4cf3566bc613349a211ea9e4309
3
+ metadata.gz: 66b4aa8b083b2ce04d0b18a00eda81ca826aa514e66adcf15c6d909fca23bf7b
4
+ data.tar.gz: d8482df4c187ecc55f03b0e192f732f4708b8a3c102576f6575a48410098f72b
5
5
  SHA512:
6
- metadata.gz: 7291bb98bb1e404be485f5f89de9f7594a7093f89cb98c4fd27310051804bfd22ea891acdfffed137ed466b0d9e778d6034439561dffe5e1e5b8b354093044b0
7
- data.tar.gz: d86f50e73b9044cd8248bf49454c8c036a99b0bd2577fe21554b2c52ff14d1951bf19a11c388b9c0ddfed1670a1d315b054cda0d191909b1ae29cdc8793bf1e9
6
+ metadata.gz: 40bdb5fe245ad06dc61527cf0a2f70d76a122d118bdb50b3ec2ad2276d38fe881fc4c2a5b803904bc8c6c1df4447e2bd0de399280b2c35425c1c18439faef134
7
+ data.tar.gz: d33a783ce26f8d28d6a9668915c6664a1526924449b792d619df0c270bc1a4472be69b337d6696d80249ec61d6485988f3fb13b516b043545594902cd2ba67ea
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## [0.5.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.4.0...v0.5.0) - 2023-12-10
2
+
3
+ ### What's new
4
+
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.
15
+ - Updated readme with parsing examples.
16
+
17
+ -----------
18
+
1
19
  ## [0.3.0](https://github.com/shivam091/composite_unit_measurements/compare/v0.2.0...v0.3.0) - 2023-11-27
2
20
 
3
21
  ### 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.3.0)
4
+ composite_unit_measurements (0.5.0)
5
5
  activesupport (~> 7.0)
6
6
  unit_measurements (~> 5)
7
7
 
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 measurement strings.
3
+ A collection of specialized parsers designed for handling composite measurements.
4
4
 
5
5
  [![Ruby](https://github.com/shivam091/composite_unit_measurements/actions/workflows/main.yml/badge.svg)](https://github.com/shivam091/composite_unit_measurements/actions/workflows/main.yml)
6
6
  [![Gem Version](https://badge.fury.io/rb/composite_unit_measurements.svg)](https://badge.fury.io/rb/composite_unit_measurements)
@@ -9,12 +9,13 @@ A collection of specialized parsers designed for handling composite measurement
9
9
  [![Test Coverage](https://api.codeclimate.com/v1/badges/94e13b43cdd19e6c462c/test_coverage)](https://codeclimate.com/github/shivam091/composite_unit_measurements/test_coverage)
10
10
  [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/shivam091/composite_unit_measurements/blob/main/LICENSE)
11
11
 
12
- **Harshal V. Ladhe, M.Sc. Computer Science.**
12
+ **[Harshal V. Ladhe, Master of Computer Science.](https://shivam091.github.io)**
13
13
 
14
14
  ## Introduction
15
15
 
16
- The `CompositeUnitMeasurements` gem offers versatile parsers for efficiently parsing
17
- composite measurement strings. Leveraging the power of the `unit_measurements` gem,
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
@@ -23,7 +24,7 @@ it enables smooth handling of composite measurements in various units.
23
24
 
24
25
  ## Installation
25
26
 
26
- To use `composite_unit_measurements-rails` in your Rails application, add the
27
+ To use `composite_unit_measurements` in your Rails application, add the
27
28
  following line to your Gemfile:
28
29
 
29
30
  ```ruby
@@ -40,81 +41,81 @@ Or otherwise simply install it yourself as:
40
41
 
41
42
  ## Usage
42
43
 
43
- Each packaged parser includes the `#parse` method to parse composite measurements.
44
- You can use an appropriate parser to parse measurements. The final result of `#parse`
45
- is returned in the leftmost unit of your measurement.
44
+ Each packaged parser includes the `.parse` method to parse composite measurements.
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
- This gem internally uses [`unit_measurements`](https://github.com/shivam091/unit_measurements)
48
- to perform conversions and arithmetic operations. You can build supported composite measurements
49
- using any [unit alias](https://github.com/shivam091/unit_measurements/blob/main/units.md).
48
+ The result of each parser method returns an instance of measurement on which we
49
+ can perform any functionality offered by `unit_measurements`.
50
+
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).
50
54
 
51
55
  ### Examples
52
56
 
53
57
  **Parsing length measurements:**
54
58
 
55
59
  ```ruby
56
- CompositeUnitMeasurements::Length.parse("5 ft 6 in")
57
- #=> 5.5 ft
58
- CompositeUnitMeasurements::Length.parse("6 m 50 cm")
59
- #=> 6.5 m
60
- CompositeUnitMeasurements::Length.parse("5 km 500 m")
61
- #=> 5.5 km
60
+ CompositeUnitMeasurements::Length.parse("5 km 500 m") #=> 5.5 km
62
61
  ```
63
62
 
64
63
  **Parsing weight measurements:**
65
64
 
66
65
  ```ruby
67
- CompositeUnitMeasurements::Weight.parse("8 lb 12 oz")
68
- #=> 8.75 lb
69
- CompositeUnitMeasurements::Weight.parse("2 st 6 lb")
70
- #=> 2.428571428571429 st
71
- CompositeUnitMeasurements::Weight.parse("2 st 6 lb")
72
- # 4.5 kg
66
+ CompositeUnitMeasurements::Weight.parse("4 kg 500 g") # 4.5 kg
73
67
  ```
74
68
 
75
69
  **Parsing time measurements:**
76
70
 
77
71
  ```ruby
78
- CompositeUnitMeasurements::Time.parse("3 h 45 min")
79
- #=> 3.75 hx
80
- CompositeUnitMeasurements::Time.parse("12:60:3600,360000000")
81
- #=> 14.1 h
72
+ CompositeUnitMeasurements::Time.parse("3 h 45 min") #=> 3.75 hr
73
+ ```
74
+
75
+ **Parsing volume measurements:**
76
+
77
+ ```ruby
78
+ CompositeUnitMeasurements::Volume.parse("2 l 250 ml") #=> 2.25 l
82
79
  ```
83
80
 
84
81
  ### Support for numeric types
85
82
 
86
- Each parser can handle various numeric types, including scientific notation, rational numbers, and complex numbers.
83
+ Each parser can handle various numeric types, including scientific notation,
84
+ rational numbers, and complex numbers.
87
85
 
88
86
  ```ruby
89
- CompositeUnitMeasurements::Length.parse("1+2i ft 12 in")
90
- #=> 2.0+2.0i ft
91
- CompositeUnitMeasurements::Length.parse("1.5 ft 12e2 in")
92
- #=> 101.5 ft
93
- CompositeUnitMeasurements::Length.parse("1 1/2 ft 1+2i in")
94
- #=> 1.5833333333333333+0.16666666666666669i ft
95
- CompositeUnitMeasurements::Length.parse("2 ft 1+2i in")
96
- #=> 2.0833333333333335+0.16666666666666669i ft
97
- CompositeUnitMeasurements::Length.parse("1e-2 ft 1+2i in")
98
- #=> 0.09333333333333334+0.16666666666666669i ft
87
+ CompositeUnitMeasurements::Length.parse("1+2i ft 12 in") #=> 2.0+2.0i ft
88
+ CompositeUnitMeasurements::Length.parse("1.5 ft 12e2 in") #=> 101.5 ft
89
+ CompositeUnitMeasurements::Length.parse("1 1/2 ft 1+2i in") #=> 1.5833333333333333+0.16666666666666669i ft
90
+ CompositeUnitMeasurements::Length.parse("2 ft 1+2i in") #=> 2.0833333333333335+0.16666666666666669i ft
91
+ CompositeUnitMeasurements::Length.parse("1e-2 ft 1+2i in") #=> 0.09333333333333334+0.16666666666666669i ft
99
92
  ```
100
93
 
101
94
  ## Packaged parsers & supported composite measurements
102
95
 
103
- The `composite_unit_measurements` gem supports parsing various composite measurements, including:
96
+ The `composite_unit_measurements` gem supports parsing various composite measurements,
97
+ including:
104
98
 
105
99
  **1. CompositeUnitMeasurements::Length**
106
- - foot-inch (5 ft 6 in)
107
100
  - metre-centimetre (6 m 50 cm)
108
101
  - kilometre-metre (5 km 500 m)
102
+ - foot-inch (5 ft 6 in)
103
+ - mile-yard (20 mi 220 yd)
109
104
 
110
105
  **2. CompositeUnitMeasurements::Weight**
106
+ - kilogramme-gramme (4 kg 500 g)
111
107
  - pound-ounce (8 lb 12 oz)
112
108
  - stone-pound (2 st 6 lb)
113
- - kilogramme-gramme (4 kg 500 g)
114
109
 
115
110
  **3. CompositeUnitMeasurements::Time**
116
111
  - hour-minute (3 h 45 min)
117
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)
116
+
117
+ **4. CompositeUnitMeasurements::Volume**
118
+ - litre-millilitre (2 l 250 ml)
118
119
 
119
120
  ### Specifing parsers
120
121
 
@@ -141,4 +142,4 @@ Contributions to this project are welcomed! To contribute:
141
142
 
142
143
  ## License
143
144
 
144
- Copyright 2023 [Harshal V. LADHE]((https://shivam091.github.io)), Released under the [MIT License](http://opensource.org/licenses/MIT).
145
+ Copyright 2023 [Harshal V. LADHE](https://shivam091.github.io), Released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -54,4 +54,7 @@ module CompositeUnitMeasurements
54
54
 
55
55
  # Matches any number, including scientific, complex, rational, and real numbers.
56
56
  ANY_NUMBER = /(?<number>#{SCIENTIFIC_NUMBER}|#{COMPLEX_NUMBER}|#{RATIONAL_NUMBER}|#{REAL_NUMBER})/.freeze
57
+
58
+ private_constant :REAL_NUMBER, :RATIONAL_NUMBER, :SCIENTIFIC_NUMBER, :COMPLEX_NUMBER,
59
+ :ANY_NUMBER
57
60
  end
@@ -14,6 +14,15 @@ module CompositeUnitMeasurements
14
14
  class << self
15
15
  # Parses a given +string+ into a +UnitMeasurements::Length+ object.
16
16
  #
17
+ # @example Parse 'metre-centimetre' measurement:
18
+ # CompositeUnitMeasurements::Length.parse("6 m 50 cm") #=> 6.5 m
19
+ # @example Parse 'kilometre-metre' measurement:
20
+ # CompositeUnitMeasurements::Length.parse("5 km 500 m") #=> 5.5 km
21
+ # @example Parse 'foot-inch' measurement:
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
25
+ #
17
26
  # @param [String] string The string to parse for length measurement.
18
27
  # @return [UnitMeasurements::Length]
19
28
  # Returns a UnitMeasurements::Length object if parsing is successful.
@@ -28,6 +37,7 @@ module CompositeUnitMeasurements
28
37
  when FOOT_INCH then parse_foot_inch(string)
29
38
  when KILOMETRE_METRE then parse_kilometre_metre(string)
30
39
  when METRE_CENTIMETRE then parse_metre_centimetre(string)
40
+ when MILE_YARD then parse_mile_yard(string)
31
41
  else raise UnitMeasurements::ParseError, string
32
42
  end
33
43
  end
@@ -35,7 +45,8 @@ module CompositeUnitMeasurements
35
45
  private
36
46
 
37
47
  # @private
38
- # Parses a +string+ representing a length in the format of +foot-inch+.
48
+ # Parses a +string+ representing a length in the format of +foot-inch+
49
+ # into a +UnitMeasurements::Length+ object.
39
50
  #
40
51
  # @param [String] string
41
52
  # The string representing length measurement in the format of *foot-inch*.
@@ -54,7 +65,8 @@ module CompositeUnitMeasurements
54
65
  end
55
66
 
56
67
  # @private
57
- # Parses a +string+ representing a length in the format of +metre-centimetre+.
68
+ # Parses a +string+ representing a length in the format of +metre-centimetre+
69
+ # into a +UnitMeasurements::Length+ object.
58
70
  #
59
71
  # @param [String] string
60
72
  # The string representing length measurement in the format of *metre-centimetre*.
@@ -73,7 +85,8 @@ module CompositeUnitMeasurements
73
85
  end
74
86
 
75
87
  # @private
76
- # Parses a +string+ representing a length in the format of +kilometre-metre+.
88
+ # Parses a +string+ representing a length in the format of +kilometre-metre+
89
+ # into a +UnitMeasurements::Length+ object.
77
90
  #
78
91
  # @param [String] string
79
92
  # The string representing length measurement in the format of *kilometre-metre*.
@@ -90,9 +103,27 @@ module CompositeUnitMeasurements
90
103
  UnitMeasurements::Length.new(kilometre, "km") + UnitMeasurements::Length.new(metre, "m")
91
104
  end
92
105
  end
93
- end
94
106
 
95
- private
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
126
+ end
96
127
 
97
128
  # Regex pattern for aliases of +foot+ unit.
98
129
  #
@@ -124,6 +155,18 @@ module CompositeUnitMeasurements
124
155
  # @since 0.3.0
125
156
  KILOMETRE_ALIASES = /(?:km|kilometer(?:s)?|kilometre(?:s)?)/.freeze
126
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
+
127
170
  # Regex pattern for parsing a length measurement in the format of +foot-inch+.
128
171
  #
129
172
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -141,5 +184,15 @@ module CompositeUnitMeasurements
141
184
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
142
185
  # @since 0.3.0
143
186
  KILOMETRE_METRE = /\A#{ANY_NUMBER}\s*#{KILOMETRE_ALIASES}\s*#{ANY_NUMBER}\s*#{METRE_ALIASES}\z/.freeze
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
+
194
+ private_constant :FOOT_ALIASES, :INCH_ALIASES, :METRE_ALIASES, :CENTIMETRE_ALIASES,
195
+ :KILOMETRE_ALIASES, :MILE_ALIASES, :YARD_ALIASES, :MILE_YARD,
196
+ :FOOT_INCH, :KILOMETRE_METRE, :METRE_CENTIMETRE
144
197
  end
145
198
  end
@@ -14,6 +14,18 @@ module CompositeUnitMeasurements
14
14
  class << self
15
15
  # Parses a given +string+ into a +UnitMeasurements::Time+ object.
16
16
  #
17
+ # @example Parse 'hour-minute' measurement:
18
+ # CompositeUnitMeasurements::Time.parse("3 h 45 min") #=> 3.75 h
19
+ # @example Parse 'duration':
20
+ # CompositeUnitMeasurements::Time.parse("12:60:3600,360000000") #=> 14.1 h
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
+ #
17
29
  # @param [String] string The string to parse for time measurement.
18
30
  # @return [UnitMeasurements::Time]
19
31
  # Returns a UnitMeasurements::Time object if parsing is successful.
@@ -24,16 +36,20 @@ module CompositeUnitMeasurements
24
36
  # @since 0.2.0
25
37
  def parse(string)
26
38
  case string
27
- when HOUR_MINUTE then parse_hour_minute(string)
28
- when DURATION then parse_duration(string)
29
- else raise UnitMeasurements::ParseError, 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
30
45
  end
31
46
  end
32
47
 
33
48
  private
34
49
 
35
50
  # @private
36
- # Parses a +string+ representing a time in the format of +hour-minute+.
51
+ # Parses a +string+ representing a time in the format of +hour-minute+
52
+ # into a +UnitMeasurements::Time+ object.
37
53
  #
38
54
  # @param [String] string
39
55
  # The string representing time measurement in the format of *hour-minute*.
@@ -53,7 +69,8 @@ module CompositeUnitMeasurements
53
69
 
54
70
  # @private
55
71
  # Parses a +string+ representing time duration in the format of
56
- # +hour:minute:second,microsecond+ or +hour:minute:second+.
72
+ # +hour:minute:second,microsecond+ or +hour:minute:second+ into a
73
+ # +UnitMeasurements::Time+ object.
57
74
  #
58
75
  # @param [String] string The string representing time duration.
59
76
  # @return [UnitMeasurements::Time]
@@ -68,14 +85,72 @@ module CompositeUnitMeasurements
68
85
  hour, minute, second, microsecond = string.match(DURATION)&.captures
69
86
  raise ArgumentError, "Invalid Duration" if [hour, minute, second, microsecond].all?(&:nil?)
70
87
 
71
- UnitMeasurements::Time.new((hour || 0), :h) +
72
- UnitMeasurements::Time.new((minute || 0), :min) +
73
- UnitMeasurements::Time.new((second || 0), :s) +
74
- UnitMeasurements::Time.new((microsecond || 0), s)
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")
75
92
  end
76
- end
77
93
 
78
- private
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
152
+ end
153
+ end
79
154
 
80
155
  # Regex pattern for aliases of +hour+ unit.
81
156
  #
@@ -89,6 +164,30 @@ module CompositeUnitMeasurements
89
164
  # @since 0.3.0
90
165
  MINUTE_ALIASES = /(?:min|minute(?:s)?)/.freeze
91
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
+
92
191
  # Regex pattern for parsing a time measurement in the format of +hour-minute+.
93
192
  #
94
193
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -101,5 +200,27 @@ module CompositeUnitMeasurements
101
200
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
102
201
  # @since 0.2.0
103
202
  DURATION = /\A(?<hour>#{REAL_NUMBER}):(?<min>#{REAL_NUMBER}):(?:(?<sec>#{REAL_NUMBER}))?(?:,(?<msec>#{REAL_NUMBER}))?\z/.freeze
203
+
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
104
225
  end
105
226
  end
@@ -25,5 +25,5 @@
25
25
  # @since 0.1.0
26
26
  module CompositeUnitMeasurements
27
27
  # Current stable version
28
- VERSION = "0.3.0"
28
+ VERSION = "0.5.0"
29
29
  end
@@ -0,0 +1,79 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ require "unit_measurements/unit_groups/volume"
6
+
7
+ module CompositeUnitMeasurements
8
+ # A parser handling +volume+ measurements, particularly for composite units
9
+ # like +litre-millilitre+, +gallon-quart+, +quart-pint+, etc.
10
+ #
11
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
12
+ # @since 0.4.0
13
+ class Volume
14
+ class << self
15
+ # Parses a given +string+ into a +UnitMeasurements::Volume+ object.
16
+ #
17
+ # @example Parse 'litre-millilitre' measurement:
18
+ # CompositeUnitMeasurements::Volume.parse("2 l 250 ml") #=> 2.25 l
19
+ #
20
+ # @param [String] string The string to parse for volume measurement.
21
+ # @return [UnitMeasurements::Volume]
22
+ # Returns a UnitMeasurements::Volume object if parsing is successful.
23
+ #
24
+ # @raise [UnitMeasurements::ParseError]
25
+ # If the string does not match any known format.
26
+ #
27
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
28
+ # @since 0.4.0
29
+ def parse(string)
30
+ case string
31
+ when LITRE_MILLILITRE then parse_litre_millilitre(string)
32
+ else raise UnitMeasurements::ParseError, string
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ # @private
39
+ # Parses a +string+ representing a volume in the format of +litre-millilitre+
40
+ # into a +UnitMeasurements::Volume+ object.
41
+ #
42
+ # @param [String] string
43
+ # The string representing volume measurement in the format of *litre-millilitre*.
44
+ # @return [UnitMeasurements::Volume]
45
+ # Returns a UnitMeasurements::Volume object if parsing is successful.
46
+ #
47
+ # @see LITRE_MILLILITRE
48
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
49
+ # @since 0.4.0
50
+ def parse_litre_millilitre(string)
51
+ litre, millilitre = string.match(LITRE_MILLILITRE)&.captures
52
+
53
+ if litre && millilitre
54
+ UnitMeasurements::Volume.new(litre, "l") + UnitMeasurements::Volume.new(millilitre, "ml")
55
+ end
56
+ end
57
+ end
58
+
59
+ # Regex pattern for aliases of +litre+ unit.
60
+ #
61
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
62
+ # @since 0.4.0
63
+ LITRE_ALIASES = /(?:l|L|liter(?:s)?|litre(?:s)?)/.freeze
64
+
65
+ # Regex pattern for aliases of +millilitre+ unit.
66
+ #
67
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
68
+ # @since 0.4.0
69
+ MILLILITRE_ALIASES = /(?:ml|mL|milliliter(?:s)?|millilitre(?:s)?)/.freeze
70
+
71
+ # Regex pattern for parsing a volume measurement in the format of +litre-millilitre+.
72
+ #
73
+ # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
74
+ # @since 0.4.0
75
+ LITRE_MILLILITRE = /\A#{ANY_NUMBER}\s*#{LITRE_ALIASES}\s*#{ANY_NUMBER}\s*#{MILLILITRE_ALIASES}\z/.freeze
76
+
77
+ private_constant :LITRE_MILLILITRE, :LITRE_ALIASES, :MILLILITRE_ALIASES
78
+ end
79
+ end
@@ -6,7 +6,7 @@ require "unit_measurements/unit_groups/weight"
6
6
 
7
7
  module CompositeUnitMeasurements
8
8
  # A parser handling +weight+ measurements, particularly for composite units
9
- # like +pound-ounce+, +stone-pound+, +kilogramme-gramme+, etc.
9
+ # like +kilogramme-gramme+, +pound-ounce+, +stone-pound+ etc.
10
10
  #
11
11
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
12
12
  # @since 0.2.0
@@ -14,6 +14,13 @@ 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
+ # @example Parse 'pound-ounce' measurement:
20
+ # CompositeUnitMeasurements::Weight.parse("8 lb 12 oz") #=> 8.75 lb
21
+ # @example Parse 'stone-pound' measurement:
22
+ # CompositeUnitMeasurements::Weight.parse("2 st 6 lb") #=> 2.428571428571429 st
23
+ #
17
24
  # @param [String] string The string to parse for weight measurement.
18
25
  # @return [UnitMeasurements::Weight]
19
26
  # Returns a UnitMeasurements::Weight object if parsing is successful.
@@ -34,7 +41,8 @@ module CompositeUnitMeasurements
34
41
  private
35
42
 
36
43
  # @private
37
- # Parses a +string+ representing a weight in the format of +pound-ounce+.
44
+ # Parses a +string+ representing a weight in the format of +pound-ounce+
45
+ # into a +UnitMeasurements::Weight+ object.
38
46
  #
39
47
  # @param [String] string
40
48
  # The string representing weight measurement in the format of *pound-ounce*.
@@ -53,7 +61,8 @@ module CompositeUnitMeasurements
53
61
  end
54
62
 
55
63
  # @private
56
- # Parses a +string+ representing a weight in the format of +stone-pound+.
64
+ # Parses a +string+ representing a weight in the format of +stone-pound+
65
+ # into a +UnitMeasurements::Weight+ object.
57
66
  #
58
67
  # @param [String] string
59
68
  # The string representing weight measurement in the format of *stone-pound*.
@@ -72,7 +81,8 @@ module CompositeUnitMeasurements
72
81
  end
73
82
 
74
83
  # @private
75
- # Parses a +string+ representing a weight in the format of +kilogramme-gramme+.
84
+ # Parses a +string+ representing a weight in the format of +kilogramme-gramme+
85
+ # into a +UnitMeasurements::Weight+ object.
76
86
  #
77
87
  # @param [String] string
78
88
  # The string representing weight measurement in the format of *kilogramme-gramme*.
@@ -91,8 +101,6 @@ module CompositeUnitMeasurements
91
101
  end
92
102
  end
93
103
 
94
- private
95
-
96
104
  # Regex pattern for aliases of +pound+ unit.
97
105
  #
98
106
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
@@ -140,5 +148,8 @@ module CompositeUnitMeasurements
140
148
  # @author {Harshal V. Ladhe}[https://shivam091.github.io/]
141
149
  # @since 0.3.0
142
150
  KILOGRAMME_GRAMME = /\A#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\s*#{ANY_NUMBER}\s*#{GRAMME_ALIASES}\z/.freeze
151
+
152
+ private_constant :KILOGRAMME_GRAMME, :POUND_ALIASES, :OUNCE_ALIASES, :STONE_ALIASES ,
153
+ :GRAMME_ALIASES, :KILOGRAMME_ALIASES, :POUND_OUNCE, :STONE_POUND
143
154
  end
144
155
  end
@@ -7,3 +7,4 @@ require "composite_unit_measurements/base"
7
7
  require "composite_unit_measurements/length"
8
8
  require "composite_unit_measurements/time"
9
9
  require "composite_unit_measurements/weight"
10
+ require "composite_unit_measurements/volume"
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.3.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-11-27 00:00:00.000000000 Z
11
+ date: 2023-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -124,6 +124,7 @@ files:
124
124
  - lib/composite_unit_measurements/length.rb
125
125
  - lib/composite_unit_measurements/time.rb
126
126
  - lib/composite_unit_measurements/version.rb
127
+ - lib/composite_unit_measurements/volume.rb
127
128
  - lib/composite_unit_measurements/weight.rb
128
129
  homepage: https://github.com/shivam091/composite_unit_measurements
129
130
  licenses: