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
47
48
49
# 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.match?(/\A\d+\z/)
          # check no leading zeros (when not zero)
          if value == value.to_i.to_s
            value = value.to_i
          end
        elsif value.match?(/\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



55
56
57
# File 'lib/blazer/statement.rb', line 55

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

#bindObject



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

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

#clear_cacheObject



75
76
77
78
# File 'lib/blazer/statement.rb', line 75

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

#cohort_analysis?Boolean

Returns:

  • (Boolean)


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

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

#display_statementObject



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

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



61
62
63
64
65
# File 'lib/blazer/statement.rb', line 61

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