Class: Groupdate::SeriesBuilder
- Inherits:
-
Object
- Object
- Groupdate::SeriesBuilder
- Defined in:
- lib/groupdate/series_builder.rb
Constant Summary collapse
- CHECK_PERIODS =
[:day, :week, :month, :quarter, :year]
Instance Attribute Summary collapse
-
#day_start ⇒ Object
readonly
Returns the value of attribute day_start.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#time_zone ⇒ Object
readonly
Returns the value of attribute time_zone.
-
#week_start ⇒ Object
readonly
Returns the value of attribute week_start.
Instance Method Summary collapse
- #generate(data, default_value:, series_default: true, multiple_groups: false, group_index: nil) ⇒ Object
-
#initialize(period:, time_zone:, day_start:, week_start:, **options) ⇒ SeriesBuilder
constructor
A new instance of SeriesBuilder.
- #round_time(time) ⇒ Object
- #time_range ⇒ Object
Constructor Details
#initialize(period:, time_zone:, day_start:, week_start:, **options) ⇒ SeriesBuilder
Returns a new instance of SeriesBuilder.
7 8 9 10 11 12 13 14 |
# File 'lib/groupdate/series_builder.rb', line 7 def initialize(period:, time_zone:, day_start:, week_start:, **) @period = period @time_zone = time_zone @week_start = week_start @day_start = day_start @options = @round_time = {} end |
Instance Attribute Details
#day_start ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/groupdate/series_builder.rb', line 3 def @options end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
3 4 5 |
# File 'lib/groupdate/series_builder.rb', line 3 def period @period end |
#time_zone ⇒ Object (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_start ⇒ Object (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
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 43 44 |
# File 'lib/groupdate/series_builder.rb', line 16 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) unless entire_series?(series_default) series = series.select { |k| data[k] } end value = 0 result = Hash[series.map do |k| value = data.delete(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] # only check for database # only checks remaining keys to avoid expensive calls to round_time if series_default && CHECK_PERIODS.include?(period) check_consistent_time_zone_info(data, multiple_groups, group_index) end result end |
#round_time(time) ⇒ Object
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 |
# File 'lib/groupdate/series_builder.rb', line 46 def round_time(time) time = time.to_time.in_time_zone(time_zone) # only if day_start != 0 for performance time -= day_start.seconds if day_start != 0 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 # same logic as MySQL group weekday = (time.wday - 1) % 7 (time - ((7 - week_start + weekday) % 7).days).midnight 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.wday - 1 - week_start) % 7 when :day_of_month time.day when :month_of_year time.month else raise Groupdate::Error, "Invalid period" end # only if day_start != 0 for performance time += day_start.seconds if day_start != 0 && time.is_a?(Time) time end |
#time_range ⇒ Object
92 93 94 95 96 97 98 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 |
# File 'lib/groupdate/series_builder.rb', line 92 def time_range @time_range ||= begin time_range = [:range] if time_range.is_a?(Range) && time_range.first.is_a?(Date) # convert range of dates to range of times # use parsing instead of in_time_zone due to Rails < 4 last = time_zone.parse(time_range.last.to_s) last += 1.day unless time_range.exclude_end? time_range = Range.new(time_zone.parse(time_range.first.to_s), last, true) elsif !time_range && [:last] if period == :quarter step = 3.months elsif 1.respond_to?(period) step = 1.send(period) else raise ArgumentError, "Cannot use last option with #{period}" end if step # loop instead of multiply to change start_at - see #151 start_at = now ([:last].to_i - 1).times do start_at -= step end time_range = if [:current] == false round_time(start_at - step)...round_time(now) else round_time(start_at)..now end end end time_range end end |