class Predefined::Query

Attributes

name[R]

Public Class Methods

format_query(sql) click to toggle source
# File lib/predefined/query.rb, line 45
def self.format_query(sql)
  sql.to_s.split(/\n/).map(&:strip).join(' ')
end
new(name, options={}) click to toggle source
# File lib/predefined/query.rb, line 49
def initialize(name, options={})
  @name = name
  @options = options
end
template(name) click to toggle source
# File lib/predefined/query.rb, line 25
def self.template(name)
  @template ||= begin
    template_paths.each do |path|
      file = template_file(path, name)
      if File.exists?(file)
        return format_query(File.read(file))
      end
    end
    template_missing(name)
  end
end
template_file(path, name) click to toggle source
# File lib/predefined/query.rb, line 37
def self.template_file(path, name)
  File.join(path, "#{name}.sql")
end
template_missing(name) click to toggle source
# File lib/predefined/query.rb, line 41
def self.template_missing(name)
  raise MissingTemplateError.new("Could not find template for query #{name.inspect} in #{template_paths.inspect}")
end
template_paths() click to toggle source
# File lib/predefined/query.rb, line 11
def self.template_paths
  if defined? @template_paths
    @template_paths
  elsif superclass.respond_to?(:template_paths)
    superclass.template_paths
  else
    @template_paths ||= []
  end
end
template_paths=(paths) click to toggle source
# File lib/predefined/query.rb, line 21
def self.template_paths=(paths)
  @template_paths = Array(paths)
end

Public Instance Methods

template() click to toggle source
# File lib/predefined/query.rb, line 54
def template
  self.class.template(name)
end