Class: Timely::DateGroup
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Timely::DateGroup
- Defined in:
- lib/timely/rails/date_group.rb
Class Method Summary collapse
Instance Method Summary collapse
- #applicable_for_duration?(date_range) ⇒ Boolean
- #dates ⇒ Object
- #includes_date?(date) ⇒ Boolean
-
#pattern ⇒ Object
—————- Date intervals and patterns —————–#.
- #to_s ⇒ Object
Class Method Details
.from_patterns(patterns) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/timely/rails/date_group.rb', line 66 def self.from_patterns(patterns) date_groups = [] Array.wrap(patterns).each do |pattern| if pattern.frequency.unit == :weeks weekdays = pattern.intervals.map { |i| i.first_datetime.wday }.each_with_object({}) do |wday, hash| hash[wday] = 1 end date_groups << DateGroup.new( start_date: pattern.first_datetime.to_date, end_date: pattern.last_datetime.to_date, weekdays: weekdays ) elsif pattern.frequency.unit == :days && pattern.frequency.duration == 1.day date_groups << DateGroup.new( start_date: pattern.first_datetime.to_date, end_date: pattern.last_datetime.to_date, weekdays: 127 ) else pattern.datetimes.each do |datetimes| datetimes.group_by(&:week).values.each do |dates| weekdays = dates.map(&:wday).each_with_object({}) do |wday, hash| hash[wday] = 1 end date_groups << DateGroup.new( start_date: dates.min.to_date.beginning_of_week, end_date: dates.max.to_date.end_of_week, weekdays: weekdays ) end end end end date_groups end |
Instance Method Details
#applicable_for_duration?(date_range) ⇒ Boolean
37 38 39 40 41 42 43 44 45 |
# File 'lib/timely/rails/date_group.rb', line 37 def applicable_for_duration?(date_range) if date_range.first > end_date || date_range.last < start_date false elsif weekdays.all_days? true else date_range.intersecting_dates(start_date..end_date).any? { |d| weekdays.applies_for_date?(d) } end end |
#dates ⇒ Object
47 48 49 |
# File 'lib/timely/rails/date_group.rb', line 47 def dates start_date.upto(end_date).select { |d| weekdays.applies_for_date?(d) } end |
#includes_date?(date) ⇒ Boolean
33 34 35 |
# File 'lib/timely/rails/date_group.rb', line 33 def includes_date?(date) date >= start_date && date <= end_date && weekdays.applies_for_date?(date) end |
#pattern ⇒ Object
—————- Date intervals and patterns —————–#
61 62 63 64 |
# File 'lib/timely/rails/date_group.rb', line 61 def pattern ranges = dates.group_by(&:wday).values.map { |weekdates| (weekdates.min..weekdates.max) } TemporalPatterns::Pattern.new(ranges, 1.week) end |
#to_s ⇒ Object
51 52 53 54 55 |
# File 'lib/timely/rails/date_group.rb', line 51 def to_s str = start_date && end_date ? (start_date..end_date).to_date_range.to_s : (start_date || end_date).to_s str += " on #{weekdays}" unless weekdays.all_days? str end |