Sha256: 0e262d14e81731e34da16677bd3f44d5bdfbefacd8bfa1e8bf31f0356629cf9f

Contents?: true

Size: 1001 Bytes

Versions: 16

Compression:

Stored size: 1001 Bytes

Contents

class SQLCompiler
  attr_accessor :parameters

  CompilerResult = Struct.new(:body, :effective_values, :error)

  def initialize(options)
    @body = options[:body]
    @parameter_reporters = options[:parameters]
  end

  def compile(substitution_values = {})
    compile!(substitution_values)
  rescue CompilableParameter::CompilerArgumentError => e
    CompilerResult.new(nil, nil, e)
  end

  def compile!(substitution_values = {})
    effective_values = get_effective_values(substitution_values)
    body = effective_values.inject(@body) do |body, (name, value)|
      body.gsub(/{\s*#{name}\s*}/) { value }
    end
    CompilerResult.new(body, effective_values)
  end

  protected

  def get_effective_values(substitution_values)
    substitution_values = substitution_values.stringify_keys
    @parameter_reporters.each_with_object({}) do |parameter_reporter, hash|
      hash[parameter_reporter.name] = parameter_reporter.compile(substitution_values[parameter_reporter.name])
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
aleph_analytics-0.4.9.pre.dev lib/sql_compiler.rb
aleph_analytics-0.4.8 lib/sql_compiler.rb
aleph_analytics-0.4.7 lib/sql_compiler.rb
aleph_analytics-0.4.4 lib/sql_compiler.rb
aleph_analytics-0.4.2 lib/sql_compiler.rb
aleph_analytics-0.4.1 lib/sql_compiler.rb
aleph_analytics-0.3.0 lib/sql_compiler.rb
aleph_analytics-0.2.0 lib/sql_compiler.rb
aleph_analytics-0.1.0 lib/sql_compiler.rb
aleph_analytics-0.0.6 lib/sql_compiler.rb
aleph_analytics-0.0.5 lib/sql_compiler.rb
aleph_analytics-0.0.4 lib/sql_compiler.rb
aleph_analytics-0.0.3 lib/sql_compiler.rb
aleph_analytics-0.0.2 lib/sql_compiler.rb
aleph_analytics-0.0.1.alpha lib/sql_compiler.rb
aleph_analytics-0.0.0.alpha lib/sql_compiler.rb