Class: BlazerJsonAPI::ProcessStatementVariables

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer_json_api/process_statement_variables.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement, data_source, params) ⇒ ProcessStatementVariables

Returns a new instance of ProcessStatementVariables.



10
11
12
13
14
# File 'lib/blazer_json_api/process_statement_variables.rb', line 10

def initialize(statement, data_source, params)
  @statement = statement
  @data_source = data_source
  @params = params
end

Instance Attribute Details

#data_sourceObject (readonly)

Returns the value of attribute data_source.



8
9
10
# File 'lib/blazer_json_api/process_statement_variables.rb', line 8

def data_source
  @data_source
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/blazer_json_api/process_statement_variables.rb', line 8

def params
  @params
end

#statementObject (readonly)

Returns the value of attribute statement.



8
9
10
# File 'lib/blazer_json_api/process_statement_variables.rb', line 8

def statement
  @statement
end

Instance Method Details

#callObject



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

def call
  bind_variables.each do |variable|
    value = params[variable].presence
    if value
      if variable.end_with?('_at')
        begin
          value = Blazer.time_zone.parse(value)
        rescue StandardError
          # do nothing
        end
      end

      if /\A\d+\z/.match?(value.to_s)
        value = value.to_i
      elsif /\A\d+\.\d+\z/.match?(value.to_s)
        value = value.to_f
      end
    end
    if Blazer.transform_variable
      value = Blazer.transform_variable.call(variable, value)
  end
    statement.gsub!("{#{variable}}", ActiveRecord::Base.connection.quote(value))
  end
end