module PgJbuilder

Constants

VERSION

Public Class Methods

connection() click to toggle source
# File lib/pg_jbuilder.rb, line 36
def self.connection
  if @connection.is_a?(Proc)
    @connection.call
  else
    @connection
  end
end
connection=(value) click to toggle source
# File lib/pg_jbuilder.rb, line 32
def self.connection= value
  @connection = value
end
paths() click to toggle source
# File lib/pg_jbuilder.rb, line 28
def self.paths
  @paths
end
render(query, variables={}) click to toggle source
# File lib/pg_jbuilder.rb, line 22
def self.render query, variables={}, options={}
  contents = get_query_contents(query)
  compiled = handlebars.compile(contents, noEscape: true)
  compiled.call(variables)
end
render_array(*args) click to toggle source
# File lib/pg_jbuilder.rb, line 16
def self.render_array *args
  result = render(*args)
  "SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json)\nFROM (\n#{result}\n) array_row"
end
render_object(*args) click to toggle source
# File lib/pg_jbuilder.rb, line 11
def self.render_object *args
  result = render(*args)
  "SELECT COALESCE(row_to_json(object_row),'{}'::json)\nFROM (\n#{result}\n) object_row"
end

Private Class Methods

get_query_contents(query) click to toggle source
# File lib/pg_jbuilder.rb, line 46
def self.get_query_contents query
  File.read path_name(query)
end
handlebars() click to toggle source
# File lib/pg_jbuilder.rb, line 70
def self.handlebars
  unless @handlebars
    @handlebars = Handlebars::Context.new
    @handlebars.register_helper :include do |context,value,options|
      render_helper context, value, options
    end

    @handlebars.register_helper :quote do |context,value,options|
      connection.quote value
    end

    @handlebars.register_helper :object do |context,value,options|
      if value.is_a?(String)
        content = render_helper(context,value,options)
        content = "\n#{content}\n"
      else
        content = value.fn(context)
      end
      "(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (" +
        content +
        ")object_row)"
    end

    @handlebars.register_helper :array do |context,value,options|
      if value.is_a?(String)
        content = render_helper(context,value,options)
        content = "\n#{content}\n"
      else
        content = value.fn(context)
      end
      "(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (" +
        content +
        ")array_row)"
    end
  end
  @handlebars
end
path_name(*args) click to toggle source
# File lib/pg_jbuilder.rb, line 50
def self.path_name *args
  last_arg = args.pop
  query_name = last_arg
  last_arg += '.sql'
  args.push last_arg
  @paths.each do |path|
    file = File.join(path,*args)
    if File.exists?(file) && File.file?(file)
      return file
    end
  end
  raise TemplateNotFound.new("Template #{query_name} was not found in any source paths")
end
render_helper(context, value, options) click to toggle source
# File lib/pg_jbuilder.rb, line 64
def self.render_helper context, value, options
  variables = Hash[context.collect{|k,v|[k,v]}]
  options['hash'].each{|k,v| variables[k] = v} if options
  PgJbuilder.render value, variables
end