Class: Groupdate::Magic::Relation

Inherits:
Groupdate::Magic show all
Defined in:
lib/groupdate/magic.rb

Instance Attribute Summary

Attributes inherited from Groupdate::Magic

#group_index, #options, #period

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Groupdate::Magic

#day_start, #series_builder, #time_range, #time_zone, #week_start

Constructor Details

#initialize(**options) ⇒ Relation

Returns a new instance of Relation.


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

def initialize(**options)
  super(**options.reject { |k, _| [:default_value, :carry_forward, :last, :current].include?(k) })
  @options = options
end

Class Method Details

.generate_relation(relation, field:, **options) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/groupdate/magic.rb', line 133

def self.generate_relation(relation, field:, **options)
  magic = Groupdate::Magic::Relation.new(**options)

  # generate ActiveRecord relation
  relation =
    RelationBuilder.new(
      relation,
      column: field,
      period: magic.period,
      time_zone: magic.time_zone,
      time_range: magic.time_range,
      week_start: magic.week_start,
      day_start: magic.day_start
    ).generate

  # add Groupdate info
  magic.group_index = relation.group_values.size - 1
  (relation.groupdate_values ||= []) << magic

  relation
end

.process_result(relation, result, **options) ⇒ Object

allow any options to keep flexible for future


156
157
158
159
160
161
# File 'lib/groupdate/magic.rb', line 156

def self.process_result(relation, result, **options)
  relation.groupdate_values.reverse.each do |gv|
    result = gv.perform(relation, result, default_value: options[:default_value])
  end
  result
end

Instance Method Details

#cast_methodObject


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/groupdate/magic.rb', line 85

def cast_method
  @cast_method ||= begin
    case period
    when :day_of_week
      lambda { |k| (k.to_i - 1 - week_start) % 7 }
    when :hour_of_day, :day_of_month, :month_of_year, :minute_of_hour
      lambda { |k| k.to_i }
    else
      utc = ActiveSupport::TimeZone["UTC"]
      lambda { |k| (k.is_a?(String) || !k.respond_to?(:to_time) ? utc.parse(k.to_s) : k.to_time).in_time_zone(time_zone) }
    end
  end
end

#cast_result(result, multiple_groups) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/groupdate/magic.rb', line 99

def cast_result(result, multiple_groups)
  new_result = {}
  result.each do |k, v|
    if multiple_groups
      k[group_index] = cast_method.call(k[group_index])
    else
      k = cast_method.call(k)
    end
    new_result[k] = v
  end
  new_result
end

#check_nils(result, multiple_groups, relation) ⇒ Object


122
123
124
125
126
127
128
129
130
131
# File 'lib/groupdate/magic.rb', line 122

def check_nils(result, multiple_groups, relation)
  has_nils = multiple_groups ? (result.keys.first && result.keys.first[group_index].nil?) : result.key?(nil)
  if has_nils
    if time_zone_support?(relation)
      raise Groupdate::Error, "Invalid query - be sure to use a date or time column"
    else
      raise Groupdate::Error, "Database missing time zone support for #{time_zone.tzinfo.name} - see https://github.com/ankane/groupdate#for-mysql"
    end
  end
end

#perform(relation, result, default_value:) ⇒ Object


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

def perform(relation, result, default_value:)
  multiple_groups = relation.group_values.size > 1

  check_nils(result, multiple_groups, relation)
  result = cast_result(result, multiple_groups)

  series_builder.generate(
    result,
    default_value: options.key?(:default_value) ? options[:default_value] : default_value,
    multiple_groups: multiple_groups,
    group_index: group_index
  )
end

#time_zone_support?(relation) ⇒ Boolean

Returns:

  • (Boolean)

112
113
114
115
116
117
118
119
120
# File 'lib/groupdate/magic.rb', line 112

def time_zone_support?(relation)
  if relation.connection.adapter_name =~ /mysql/i
    # need to call klass for Rails < 5.2
    sql = relation.klass.send(:sanitize_sql_array, ["SELECT CONVERT_TZ(NOW(), '+00:00', ?)", time_zone.tzinfo.name])
    !relation.connection.select_all(sql).first.values.first.nil?
  else
    true
  end
end