class Timely::DateGroup

Public Class Methods

from_patterns(patterns) click to toggle source
# 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

Public Instance Methods

applicable_for_duration?(date_range) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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

Private Instance Methods

validate_date_range!() click to toggle source
# File lib/timely/rails/date_group.rb, line 104
def validate_date_range!
  return unless start_date && end_date && start_date > end_date

  raise ArgumentError, "Incorrect date range #{start_date} is before #{end_date}"
end