Class: Groupdate::Magic

Inherits:
Object
  • Object
show all
Defined in:
lib/groupdate/magic.rb

Direct Known Subclasses

Enumerable, Relation

Defined Under Namespace

Classes: Enumerable, Relation

Constant Summary collapse

DAYS =
[:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period:, **options) ⇒ Magic

Returns a new instance of Magic.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#group_indexObject

Returns the value of attribute group_index.



7
8
9
# File 'lib/groupdate/magic.rb', line 7

def group_index
  @group_index
end

#n_secondsObject

Returns the value of attribute n_seconds.



7
8
9
# File 'lib/groupdate/magic.rb', line 7

def n_seconds
  @n_seconds
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/groupdate/magic.rb', line 7

def options
  @options
end

#periodObject

Returns the value of attribute period.



7
8
9
# File 'lib/groupdate/magic.rb', line 7

def period
  @period
end

Class Method Details

.validate_period(period, permit) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
# File 'lib/groupdate/magic.rb', line 88

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

Instance Method Details

#day_startObject



68
69
70
# File 'lib/groupdate/magic.rb', line 68

def day_start
  @day_start ||= ((options[:day_start] || Groupdate.day_start).to_f * 3600).round
end

#series_builderObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/groupdate/magic.rb', line 72

def series_builder
  @series_builder ||=
    SeriesBuilder.new(
      **options,
      period: period,
      time_zone: time_zone,
      day_start: day_start,
      week_start: week_start,
      n_seconds: n_seconds
    )
end

#time_rangeObject



84
85
86
# File 'lib/groupdate/magic.rb', line 84

def time_range
  series_builder.time_range
end

#time_zoneObject



53
54
55
56
57
58
59
# 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

#validate_argumentsObject

Raises:

  • (ArgumentError)


46
47
48
49
50
51
# 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

#validate_keywordsObject

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/groupdate/magic.rb', line 24

def validate_keywords
  known_keywords = [:time_zone, :dates, :series, :format, :locale, :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

#week_startObject



61
62
63
64
65
66
# 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