Class: Groupdate::SeriesBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(period:, time_zone:, day_start:, week_start:, n_seconds:, **options) ⇒ SeriesBuilder

Returns a new instance of SeriesBuilder.



5
6
7
8
9
10
11
12
13
# File 'lib/groupdate/series_builder.rb', line 5

def initialize(period:, time_zone:, day_start:, week_start:, n_seconds:, **options)
  @period = period
  @time_zone = time_zone
  @week_start = week_start
  @day_start = day_start
  @n_seconds = n_seconds
  @options = options
  @week_start_key = Groupdate::Magic::DAYS[@week_start] if @week_start
end

Instance Attribute Details

#day_startObject (readonly)

Returns the value of attribute day_start.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def day_start
  @day_start
end

#n_secondsObject (readonly)

Returns the value of attribute n_seconds.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def n_seconds
  @n_seconds
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def options
  @options
end

#periodObject (readonly)

Returns the value of attribute period.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def period
  @period
end

#time_zoneObject (readonly)

Returns the value of attribute time_zone.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def time_zone
  @time_zone
end

#week_startObject (readonly)

Returns the value of attribute week_start.



3
4
5
# File 'lib/groupdate/series_builder.rb', line 3

def week_start
  @week_start
end

Instance Method Details

#generate(data, default_value:, series_default: true, multiple_groups: false, group_index: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/groupdate/series_builder.rb', line 15

def generate(data, default_value:, series_default: true, multiple_groups: false, group_index: nil)
  series = generate_series(data, multiple_groups, group_index)
  series = handle_multiple(data, series, multiple_groups, group_index)

  verified_data = {}
  series.each do |k|
    verified_data[k] = data.delete(k)
  end

  unless entire_series?(series_default)
    series = series.select { |k| verified_data[k] }
  end

  value = 0
  result = series.to_h do |k|
    value = verified_data[k] || (@options[:carry_forward] && value) || default_value
    key =
      if multiple_groups
        k[0...group_index] + [key_format.call(k[group_index])] + k[(group_index + 1)..-1]
      else
        key_format.call(k)
      end

    [key, value]
  end

  result
end

#round_time(time) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/groupdate/series_builder.rb', line 44

def round_time(time)
  if period == :custom
    return time_zone.at((time.to_time.to_i / n_seconds) * n_seconds)
  end

  time = time.to_time.in_time_zone(time_zone)

  if day_start != 0
    # apply day_start to a time object that's not affected by DST
    time = time.change(zone: utc)
    time -= day_start.seconds
  end

  time =
    case period
    when :second
      time.change(usec: 0)
    when :minute
      time.change(sec: 0)
    when :hour
      time.change(min: 0)
    when :day
      time.beginning_of_day
    when :week
      time.beginning_of_week(@week_start_key)
    when :month
      time.beginning_of_month
    when :quarter
      time.beginning_of_quarter
    when :year
      time.beginning_of_year
    when :hour_of_day
      time.hour
    when :minute_of_hour
      time.min
    when :day_of_week
      time.days_to_week_start(@week_start_key)
    when :day_of_month
      time.day
    when :month_of_year
      time.month
    when :day_of_year
      time.yday
    else
      raise Groupdate::Error, "Invalid period"
    end

  if day_start != 0 && time.is_a?(Time)
    time += day_start.seconds
    time = time.change(zone: time_zone)
  end

  time
end

#time_rangeObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/groupdate/series_builder.rb', line 99

def time_range
  @time_range ||= begin
    time_range = options[:range]

    if time_range.is_a?(Range)
      # check types
      [time_range.begin, time_range.end].each do |v|
        case v
        when nil, Date, Time
          # good
        else
          raise ArgumentError, "Range bounds should be Date or Time, not #{v.class.name}"
        end
      end

      start = time_range.begin
      start = start.in_time_zone(time_zone) if start

      exclude_end = time_range.exclude_end?

      finish = time_range.end
      finish = finish.in_time_zone(time_zone) if finish
      if time_range.end.is_a?(Date) && !time_range.end.is_a?(DateTime) && !exclude_end
        finish += 1.day
        exclude_end = true
      end

      if options[:expand_range]
        start = round_time(start) if start
        if finish && !(finish == round_time(finish) && exclude_end)
          finish = round_time(finish) + step
          exclude_end = true
        end
      end

      time_range = Range.new(start, finish, exclude_end)
    elsif !time_range && options[:last]
      step = step()
      raise ArgumentError, "Cannot use last option with #{period}" unless step

      # loop instead of multiply to change start_at - see #151
      start_at = now
      (options[:last].to_i - 1).times do
        start_at -= step
      end

      time_range =
        if options[:current] == false
          round_time(start_at - step)...round_time(now)
        else
          # extend to end of current period
          round_time(start_at)...(round_time(now) + step)
        end
    end
    time_range
  end
end