periodoxical 1.1.0 → 1.2.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: 2b67da50d1bba17e1d25babaa937440ccad41bf152d1df8d3adb50ff8c9199b3
4
- data.tar.gz: 954c91d5da06a9d4955fdb35576b11f1e209a8ab152825c3f09657e891787a5d
3
+ metadata.gz: c9f062ec6b1b65c56a01d48386370fe8f1e12b91f0426228ca625bac03272d1b
4
+ data.tar.gz: 101efa2d001ca56deb4ff3d5cef552f4aeaa98e6cecbe043ea1f990b38b8bc36
5
5
  SHA512:
6
- metadata.gz: ab9f0eb8538ea18407171b2d9dc435935821370f3f3d229f59d718e3e11f3eba6b6f926129274f33c5100b22a70d937ba6f9693d714f52abaf0f2a7be4965d28
7
- data.tar.gz: 64503a74a1879ae8d0cda934ad511530ff6c22e5f1aeb9fe331f0dda99ccf8d7b81c735e2d10a9c6ce468010ea64674c1d8bc86a24fa8f18aa5382cfd29facdd
6
+ metadata.gz: 7f0a45cb869fd3ec84bd097f0c793f1f0561ce292f48a3bc2521ad714685cd3cc1582956cc572fc4b23c045ea2fe853706467dcbb39fc328254d5855fcf4da2e
7
+ data.tar.gz: 4ab479448dbfc960996e8535f962cefd58392452f32f9e116d6faf1a373767c6062949e2959f9d64844fe0168c0e92b3edf313a9b5a3499e4c29771c6b0bd4bd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- periodoxical (1.1.0)
4
+ periodoxical (1.2.0)
5
5
  tzinfo (~> 2.0, >= 2.0.0)
6
6
  week_of_month (= 1.2.6)
7
7
 
data/README.md CHANGED
@@ -47,20 +47,20 @@ Periodoxical.generate(
47
47
  #=>
48
48
  [
49
49
  {
50
- start_time: #<DateTime: 2024-05-23T09:00:00-0700>,
51
- end_time: #<DateTime: 2024-05-23T10:30:00-0700>,
50
+ start: #<DateTime: 2024-05-23T09:00:00-0700>,
51
+ end: #<DateTime: 2024-05-23T10:30:00-0700>,
52
52
  },
53
53
  {
54
- start_time: #<DateTime: 2024-05-24T09:00:00-0700>,
55
- end_time: #<DateTime: 2024-05-24T10:30:00-0700>,
54
+ start: #<DateTime: 2024-05-24T09:00:00-0700>,
55
+ end: #<DateTime: 2024-05-24T10:30:00-0700>,
56
56
  },
57
57
  {
58
- start_time: #<DateTime: 2024-05-25T09:00:00-0700>,
59
- end_time: #<DateTime: 2024-05-25T10:30:00-0700>,
58
+ start: #<DateTime: 2024-05-25T09:00:00-0700>,
59
+ end: #<DateTime: 2024-05-25T10:30:00-0700>,
60
60
  },
61
61
  {
62
- start_time: #<DateTime: 2024-05-26T09:00:00-0700>,
63
- end_time: #<DateTime: 2024-05-26T10:30:00-0700>,
62
+ start: #<DateTime: 2024-05-26T09:00:00-0700>,
63
+ end: #<DateTime: 2024-05-26T10:30:00-0700>,
64
64
  }
65
65
  ]
66
66
  ```
@@ -474,6 +474,50 @@ Periodoxical.generate(
474
474
  ]
475
475
  ```
476
476
 
477
+ ### Use the `duration` key, to automatically partition the provided `time_blocks` into smaller chunks to the given duration.
478
+
479
+ As a Ruby dev, I want to generate **30 minute** time blocks between **9:00AM - 1:00PM, and 2:00PM - 5:00PM**. Because it is too tedious to generate all 14 of these time blocks, I prefer to pass in the `duration` key and have `periodoxical` generate them for me.
480
+
481
+ N.B. If you provide a duration that conflicts with your time blocks, `periodoxical` will not return any time blocks. For example, if you specify **9:00AM - 10:00AM** but set your **duration** as 90 minutes, no time blocks are generated since we can't fit 90 minutes into an hour!
482
+
483
+
484
+ ```rb
485
+ Periodoxical.generate(
486
+ time_zone: 'America/Los_Angeles',
487
+ time_blocks: [
488
+ {
489
+ start_time: '9:00AM',
490
+ end_time: '1:00PM'
491
+ },
492
+ {
493
+ start_time: '2:00PM',
494
+ end_time: '5:00PM'
495
+ },
496
+ ],
497
+ duration: 30, #(minutes)
498
+ starting_from: '2024-05-23',
499
+ ending_at: '2024-05-26',
500
+ )
501
+ # => [
502
+ {
503
+ start: #<DateTime: 2024-05-23T09:00:00--0700>
504
+ end: #<DateTime: 2024-05-23T09:30:00--0700>
505
+ },
506
+ {
507
+ start: #<DateTime: 2024-05-23T09:30:00--0700>
508
+ end: #<DateTime: 2024-05-23T10:00:00--0700>
509
+ },
510
+ {
511
+ start: #<DateTime: 2024-05-23T10:00:00--0700>
512
+ end: #<DateTime: 2024-05-23T10:30:00--0700>
513
+ },
514
+ {
515
+ start: #<DateTime: 2024-05-23T10:30:00--0700>
516
+ end: #<DateTime: 2024-05-23T11:00:00--0700>
517
+ }
518
+ ]
519
+ ```
520
+
477
521
  ### Having Some Fun
478
522
 
479
523
  Generate all the Friday the 13ths ever since May 1980 (when the first Friday the 13th film was released).
@@ -1,3 +1,3 @@
1
1
  module Periodoxical
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/periodoxical.rb CHANGED
@@ -66,6 +66,12 @@ module Periodoxical
66
66
  # tue: { start_time: '11:30PM', end_time: '12:00AM' },
67
67
  # fri: { start_time: '7:00PM', end_time: '9:00PM' },
68
68
  # }
69
+ # @param [Integer] duration
70
+ # Splits the time_blocks into this duration (in minutes). For example, if time_block is 9:00AM - 10:00AM,
71
+ # and duration is 20 minutes. It creates 3 timeblocks of:
72
+ # - 9:00AM - 9:20AM
73
+ # - 9:20AM - 9:40AM
74
+ # - 9:40AM - 10:00AM
69
75
  def initialize(
70
76
  starting_from:,
71
77
  ending_at: nil,
@@ -79,6 +85,7 @@ module Periodoxical
79
85
  nth_day_of_week_in_month: nil,
80
86
  days_of_month: nil,
81
87
  weeks_of_month: nil,
88
+ duration: nil,
82
89
  months: nil
83
90
  )
84
91
 
@@ -97,6 +104,14 @@ module Periodoxical
97
104
  @starting_from = date_object_from(starting_from)
98
105
  @ending_at = date_object_from(ending_at)
99
106
  @limit = limit
107
+
108
+ if duration
109
+ unless duration.is_a?(Integer)
110
+ raise "duration must be an integer"
111
+ else
112
+ @duration = duration
113
+ end
114
+ end
100
115
  @exclusion_dates = if exclusion_dates && !exclusion_dates.empty?
101
116
  exclusion_dates.map { |ed| Date.parse(ed) }
102
117
  end
@@ -201,19 +216,31 @@ module Periodoxical
201
216
  return if overlaps_with_an_excluded_time?(time_block)
202
217
  return if before_starting_from_or_after_ending_at?(time_block)
203
218
 
204
- @output << {
205
- start: time_str_to_object(@current_date, time_block[:start_time]),
206
- end: time_str_to_object(@current_date, time_block[:end_time])
207
- }
219
+ strtm = time_str_to_object(@current_date, time_block[:start_time])
220
+ endtm = time_str_to_object(@current_date, time_block[:end_time])
208
221
 
209
- # increment count, if `limit` is used to stop generating
210
- @current_count = @current_count + 1
211
- if @limit && @current_count == @limit
212
- @keep_generating = false
213
- throw :done
222
+ if @duration
223
+ split_by_duration_and_append(strtm, endtm)
224
+ else
225
+ @output << {
226
+ start: strtm,
227
+ end: endtm
228
+ }
229
+ increment_and_check_limit
214
230
  end
215
231
  end
216
232
 
233
+ # increment count, if `limit` is used to stop generating
234
+ def increment_and_check_limit
235
+ return unless @limit
236
+
237
+ @current_count += 1
238
+ return if @current_count < @limit
239
+
240
+ @keep_generating = false
241
+ throw :done
242
+ end
243
+
217
244
  def advance_current_date_and_check_if_reached_end_date
218
245
  @current_date = @current_date + 1
219
246
 
@@ -409,5 +436,20 @@ module Periodoxical
409
436
 
410
437
  false
411
438
  end
439
+
440
+ def split_by_duration_and_append(strtm, endtm)
441
+ delta = Rational(@duration, 24 * 60)
442
+ si = strtm
443
+ ei = strtm + delta
444
+ while ei <= endtm
445
+ @output << {
446
+ start: si,
447
+ end: ei
448
+ }
449
+ si += delta
450
+ ei += delta
451
+ increment_and_check_limit
452
+ end
453
+ end
412
454
  end
413
455
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: periodoxical
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Li
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-12 00:00:00.000000000 Z
11
+ date: 2024-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tzinfo