Class: Blazer::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/statement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement, data_source = nil) ⇒ Statement

Returns a new instance of Statement.



6
7
8
9
10
# File 'lib/blazer/statement.rb', line 6

def initialize(statement, data_source = nil)
  @statement = statement
  @data_source = data_source.is_a?(String) ? Blazer.data_sources[data_source] : data_source
  @values = {}
end

Instance Attribute Details

#bind_statementObject (readonly)

Returns the value of attribute bind_statement.



3
4
5
# File 'lib/blazer/statement.rb', line 3

def bind_statement
  @bind_statement
end

#bind_valuesObject (readonly)

Returns the value of attribute bind_values.



3
4
5
# File 'lib/blazer/statement.rb', line 3

def bind_values
  @bind_values
end

#data_sourceObject (readonly)

Returns the value of attribute data_source.



3
4
5
# File 'lib/blazer/statement.rb', line 3

def data_source
  @data_source
end

#statementObject (readonly)

Returns the value of attribute statement.



3
4
5
# File 'lib/blazer/statement.rb', line 3

def statement
  @statement
end

#valuesObject

Returns the value of attribute values.



4
5
6
# File 'lib/blazer/statement.rb', line 4

def values
  @values
end

Instance Method Details

#add_values(var_params) ⇒ Object



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
45
46
# File 'lib/blazer/statement.rb', line 18

def add_values(var_params)
  variables.each do |var|
    value = var_params[var].presence
    value = nil unless value.is_a?(String) # ignore arrays and hashes
    if value
      if ["start_time", "end_time"].include?(var)
        value = value.to_s.gsub(" ", "+") # fix for Quip bug
      end

      if var.end_with?("_at")
        begin
          value = Blazer.time_zone.parse(value)
        rescue
          # do nothing
        end
      end

      unless value.is_a?(ActiveSupport::TimeWithZone)
        if value =~ /\A\d+\z/
          value = value.to_i
        elsif value =~ /\A\d+\.\d+\z/
          value = value.to_f
        end
      end
    end
    value = Blazer.transform_variable.call(var, value) if Blazer.transform_variable
    @values[var] = value
  end
end

#apply_cohort_analysis(period:, days:) ⇒ Object



52
53
54
# File 'lib/blazer/statement.rb', line 52

def apply_cohort_analysis(period:, days:)
  @statement = data_source.cohort_analysis_statement(statement, period: period, days: days).sub("{placeholder}") { statement }
end

#bindObject



64
65
66
# File 'lib/blazer/statement.rb', line 64

def bind
  @bind_statement, @bind_values = data_source.bind_params(transformed_statement, values)
end

#clear_cacheObject



72
73
74
75
# File 'lib/blazer/statement.rb', line 72

def clear_cache
  bind if bind_statement.nil?
  data_source.clear_cache(self)
end

#cohort_analysis?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/blazer/statement.rb', line 48

def cohort_analysis?
  /\/\*\s*cohort analysis\s*\*\//i.match?(statement)
end

#display_statementObject



68
69
70
# File 'lib/blazer/statement.rb', line 68

def display_statement
  data_source.sub_variables(transformed_statement, values)
end

#transformed_statementObject

should probably transform before cohort analysis but keep previous order for now



58
59
60
61
62
# File 'lib/blazer/statement.rb', line 58

def transformed_statement
  statement = self.statement.dup
  Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  statement
end

#variablesObject



12
13
14
15
16
# File 'lib/blazer/statement.rb', line 12

def variables
  # strip commented out lines
  # and regex {1} or {1,2}
  @variables ||= statement.to_s.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
end