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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Magic.

Raises:

  • (ArgumentError)

7
8
9
10
11
12
13
14
15
16
17
# File 'lib/groupdate/magic.rb', line 7

def initialize(period:, **options)
  @period = period
  @options = options

  unknown_keywords = options.keys - [:day_start, :time_zone, :dates, :series, :week_start, :format, :locale, :range, :reverse]
  raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any?

  raise Groupdate::Error, "Unrecognized time zone" unless time_zone
  raise Groupdate::Error, "Unrecognized :week_start option" if period == :week && !week_start
  raise Groupdate::Error, "Cannot use endless range for :range option" if options[:range].is_a?(Range) && !options[:range].end
end

Instance Attribute Details

#group_indexObject

Returns the value of attribute group_index


5
6
7
# File 'lib/groupdate/magic.rb', line 5

def group_index
  @group_index
end

#optionsObject

Returns the value of attribute options


5
6
7
# File 'lib/groupdate/magic.rb', line 5

def options
  @options
end

#periodObject

Returns the value of attribute period


5
6
7
# File 'lib/groupdate/magic.rb', line 5

def period
  @period
end

Instance Method Details

#day_startObject


31
32
33
# File 'lib/groupdate/magic.rb', line 31

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

#series_builderObject


35
36
37
38
39
40
41
42
43
44
# File 'lib/groupdate/magic.rb', line 35

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

#time_rangeObject


46
47
48
# File 'lib/groupdate/magic.rb', line 46

def time_range
  series_builder.time_range
end

#time_zoneObject


19
20
21
22
23
24
25
# File 'lib/groupdate/magic.rb', line 19

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

#week_startObject


27
28
29
# File 'lib/groupdate/magic.rb', line 27

def week_start
  @week_start ||= [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:week_start] || options[:start] || Groupdate.week_start).to_sym)
end