ruby-rails-extensions 2.1.0 → 2.1.1

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: 3445f7f15fe0a736b52ccbaa2f176fd017b2b5fe0b58e42115a9a4d003599b32
4
- data.tar.gz: 66ea1200362e7c25c1cb88496ac0273ba960a4ed3ed24d6ab8027fe867b0e014
3
+ metadata.gz: a7bd4a821e87ee1a9683247bfd699e1e95ce101a09e864be821f829cb72fd9f8
4
+ data.tar.gz: 0eb83cf0df52e2134930577cae1006071fdbfea14e83e0bace927eb77c531bf2
5
5
  SHA512:
6
- metadata.gz: 15599ad4fc3f8a9d17fa9d3ceefca301d91a257fc79763cd29a46ef52664278a8e947b4bd717b33f54fe3434a67c45c2debfaba209717bc808c4b1c4242660a6
7
- data.tar.gz: 8a01c3eae7ecd21bc27fcfe34302a2dd33946e5b213037e4b41ce29a3c03241e2a17f058f1967b474485ef36617ccba5d01088b540487b13d4bafc8201c6c1d0
6
+ metadata.gz: 5c8d9cd934041ee7dd40746beb4c8ba06fa9f7012e874370de548d238f23bbd5978cea9e66deba458118451b84e7b79f509fa9d56cb38c2d2895200e77755b69
7
+ data.tar.gz: 8c9d3eff37ed8db19f5328f5a21360e5059c1128df87e84ec76aeca3be815153395b8c635a0fb0c8529160e9e9a1bd6ad220157e834d144fd0d14d9a5565b0d3
data/CHANGELOG.md CHANGED
@@ -1,7 +1,12 @@
1
1
  Unreleased Changes
2
2
  ------------------
3
3
 
4
- 2.1.0 (Unreleased)
4
+ 2.1.1 (2024-10-11)
5
+ ------------------
6
+
7
+ * Fix bad `2.1.0` publish
8
+
9
+ 2.1.0 (2024-10-11)
5
10
  ------------------
6
11
 
7
12
  * Modules added:
@@ -19,6 +24,7 @@ Unreleased Changes
19
24
  * `Time#quarter_str` and `DateTime#quarter_str`
20
25
  * `String#to_new_date`, `String#to_new_date_safe`, and `String#to_new_date_mm_yy`
21
26
  * `String#cutoff`
27
+ * `String#to_new_time`
22
28
  * Other changes:
23
29
  * Initial test framework added
24
30
  * Initial benchmark framework added
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StringToNewTime
4
+ NEW_TIME_REGEXES = {
5
+ # Supports: 9:00am, 12:15 pm, 10:12am
6
+ hh_mm_p: /^(1[0-2]|0?[1-9]).([0-5]?[0-9])(.?[AaPp][Mm])?$/i,
7
+ # Supports: 16:0, 23:23, 01:00
8
+ hh_mm: /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/i,
9
+ # Supports: 1p, 12a, 5a, 9p
10
+ h_p: /^[0-1][0-9]?(.?[AaPp][Mm]?)?$/i
11
+ }.freeze
12
+
13
+ # Converts a string to a date object when following one of the
14
+ # following patterns (no space delimiters):
15
+ # - HH:MM AM/PM
16
+ # - HH:MM (24 hour)
17
+ # - HH AM/PM
18
+ #
19
+ # @param options [Hash]
20
+ #
21
+ # @option options [Integer] :year Year to use as the base time object.
22
+ # This will default to today's year.
23
+ #
24
+ # @option options [Integer] :month Month to use as the base time object.
25
+ # This will default to today's month.
26
+ #
27
+ # @option options [Integer] :day Day to use as the base time object.
28
+ # This will default to today's day.
29
+ #
30
+ # @return [Date, String, nil]
31
+ #
32
+ def to_new_time(options = {})
33
+ opts = options || {}
34
+ today = Date.today
35
+ year = opts.fetch(:year, today.year)
36
+ month = opts.fetch(:month, today.month)
37
+ day = opts.fetch(:day, today.day)
38
+ parts = extract_time
39
+
40
+ Time.use_zone(RubyRailsExtensions.configuration.default_time_zone || 'Eastern Time (US & Canada)') do
41
+ # Use {Time#parse} as there is no `Time.zone.new` method
42
+ Time.zone.parse("#{year}-#{month}-#{day} #{parts[:hour]}:#{parts[:minute]} #{parts[:ampm]}")
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ # Checks the string for any supported format,
49
+ # then converts the string time parts.
50
+ #
51
+ # @return [Hash, nil]
52
+ #
53
+ def extract_time
54
+ key = NEW_TIME_REGEXES.find { |_key, value| match?(value) }&.first
55
+
56
+ return if key.nil?
57
+
58
+ __send__(:"#{key}_time")
59
+ end
60
+
61
+ # @return [Hash]
62
+ def hh_mm_p_time
63
+ parts = scan(NEW_TIME_REGEXES[:hh_mm_p])
64
+
65
+ parts.flatten!
66
+
67
+ parts.map!(&:strip)
68
+
69
+ hour, min, ampm = parts
70
+
71
+ {
72
+ hour: hour,
73
+ minute: min,
74
+ second: 0,
75
+ ampm: ampm
76
+ }
77
+ end
78
+
79
+ # @return [Hash]
80
+ def hh_mm_time
81
+ parts = scan(NEW_TIME_REGEXES[:hh_mm])
82
+
83
+ parts.flatten!
84
+
85
+ parts.map!(&:strip)
86
+
87
+ hour, min = parts
88
+
89
+ {
90
+ hour: hour,
91
+ minute: min,
92
+ second: 0,
93
+ ampm: nil
94
+ }
95
+ end
96
+
97
+ # @return [Hash]
98
+ def h_p_time
99
+ {
100
+ hour: gsub(/[apm]/i, ''),
101
+ minute: 0,
102
+ second: 0,
103
+ ampm: match?(/a/i) ? 'am' : 'pm'
104
+ }
105
+ end
106
+ end
107
+
108
+ String.class_eval do
109
+ include StringToNewTime
110
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyRailsExtensions
4
- VERSION = '2.1.0'
4
+ VERSION = '2.1.1'
5
5
  end
@@ -63,6 +63,7 @@ module RubyRailsExtensions
63
63
  to_money
64
64
  to_negative_i
65
65
  to_new_date
66
+ to_new_time
66
67
  to_nonzero_i
67
68
  to_or_sentence
68
69
  to_percentage
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-rails-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brands Insurance
@@ -77,6 +77,7 @@ files:
77
77
  - lib/ruby-rails-extensions/extensions/to_money.rb
78
78
  - lib/ruby-rails-extensions/extensions/to_negative_i.rb
79
79
  - lib/ruby-rails-extensions/extensions/to_new_date.rb
80
+ - lib/ruby-rails-extensions/extensions/to_new_time.rb
80
81
  - lib/ruby-rails-extensions/extensions/to_nonzero_i.rb
81
82
  - lib/ruby-rails-extensions/extensions/to_or_sentence.rb
82
83
  - lib/ruby-rails-extensions/extensions/to_percentage.rb