class Groupdate::Magic
Constants
- DAYS
Attributes
Public Class Methods
Source
# File lib/groupdate/magic.rb, line 9 def initialize(period:, **options) @period = period @options = options validate_keywords validate_arguments if options[:n] raise ArgumentError, "n must be a positive integer" if !options[:n].is_a?(Integer) || options[:n] < 1 @period = :custom @n_seconds = options[:n].to_i @n_seconds *= 60 if period == :minute end end
Source
# File lib/groupdate/magic.rb, line 101 def self.validate_period(period, permit) permitted_periods = ((permit || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s) raise ArgumentError, "Unpermitted period" unless permitted_periods.include?(period.to_s) end
Public Instance Methods
Source
# File lib/groupdate/magic.rb, line 68 def day_start @day_start ||= ((options[:day_start] || Groupdate.day_start).to_f * 3600).round end
Source
# File lib/groupdate/magic.rb, line 72 def range @range ||= begin time_range = options[:range] if time_range.is_a?(Range) && time_range.begin.nil? && time_range.end.nil? nil else time_range end end end
Source
# File lib/groupdate/magic.rb, line 84 def series_builder @series_builder ||= SeriesBuilder.new( **options, period: period, time_zone: time_zone, range: range, day_start: day_start, week_start: week_start, n_seconds: n_seconds ) end
Source
# File lib/groupdate/magic.rb, line 97 def time_range series_builder.time_range end
Source
# File lib/groupdate/magic.rb, line 53 def time_zone @time_zone ||= begin time_zone = "Etc/UTC" if options[:time_zone] == false time_zone ||= options[:time_zone] || Groupdate.time_zone || (Groupdate.time_zone == false && "Etc/UTC") || Time.zone || "Etc/UTC" time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone] end end
Source
# File lib/groupdate/magic.rb, line 46 def validate_arguments # TODO better messages raise ArgumentError, "Unrecognized time zone" unless time_zone raise ArgumentError, "Unrecognized :week_start option" unless week_start raise ArgumentError, ":day_start must be between 0 and 24" if (day_start / 3600) < 0 || (day_start / 3600) >= 24 end
Source
# File lib/groupdate/magic.rb, line 24 def validate_keywords known_keywords = [:time_zone, :series, :format, :locale, :range, :expand_range, :reverse] if %i[week day_of_week].include?(period) known_keywords << :week_start end if %i[day week month quarter year day_of_week hour_of_day day_of_month day_of_year month_of_year].include?(period) known_keywords << :day_start else # prevent Groupdate.day_start from applying @day_start = 0 end if %i[second minute].include?(period) known_keywords << :n end unknown_keywords = options.keys - known_keywords raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? end
Source
# File lib/groupdate/magic.rb, line 61 def week_start @week_start ||= begin v = (options[:week_start] || Groupdate.week_start).to_sym DAYS.index(v) || [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index(v) end end