class MonsterQueries::Query

Constants

SKIP_CACHE

Public Class Methods

exists?(scope) click to toggle source
# File lib/monster_queries/query.rb, line 55
def self.exists? scope
  if !@exists_cache[scope] || SKIP_CACHE
    file_name = locate_file(scope)
    @exists_cache[scope] = file_name && File.file?(file_name)
  end
  @exists_cache[scope]
end
handlebars() click to toggle source
# File lib/monster_queries/query.rb, line 63
def self.handlebars
  return @handlebars if @handlebars
  @handlebars = Handlebars::Context.new
  @handlebars.register_helper :include        , &(method(:helper_include).to_proc)
  @handlebars.register_helper :paginate       , &(method(:helper_paginate).to_proc)
  @handlebars.register_helper :paginate_offset, &(method(:helper_paginate_offset).to_proc)
  @handlebars.register_helper :wildcard       , &(method(:helper_wildcard).to_proc)
  @handlebars.register_helper :quote          , &(method(:helper_quote).to_proc)
  @handlebars.register_helper :int            , &(method(:helper_int).to_proc)
  @handlebars.register_helper :float          , &(method(:helper_float).to_proc)
  @handlebars.register_helper :array          , &(method(:helper_array).to_proc)
  @handlebars.register_helper :object         , &(method(:helper_object).to_proc)
  @handlebars
end
helper_array(context, block, options) click to toggle source
# File lib/monster_queries/query.rb, line 124
    def self.helper_array context, block, options
      content =
      if block.is_a?(String)
        "\n" + helper_include(context, block, options)
      else
        block.fn context
      end
      <<-HEREDOC
(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
#{content}
) array_row)
      HEREDOC
    end
helper_float(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 120
def self.helper_float context, value, options
  value.to_f
end
helper_include(context, name, options) click to toggle source
# File lib/monster_queries/query.rb, line 78
def self.helper_include context, name, options
  vars = {}
  context.each do |k,v|
    vars[k] = v
  end
  options['hash'].each do |k,v|
    vars[k] = v
  end if options
  parts = name.split('.')
  MonsterQueries::Builder.with_scope(parts).to_s vars
end
helper_int(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 116
def self.helper_int context, value, options
  value.to_i
end
helper_object(context, block, options) click to toggle source
# File lib/monster_queries/query.rb, line 138
    def self.helper_object context, block, options
      content =
      if block.is_a?(String)
        "\n" + helper_include(context, block, options)
      else
        block.fn context
      end
      <<-HEREDOC
(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
#{content}
) object_row)
      HEREDOC
    end
helper_paginate(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 90
def self.helper_paginate context, value, options
  if value.is_a?(String)
    count = !!context["count"]
    name = count ? 'pagination.select' : value
    self.helper_include context, name, options
  else
    value.fn context
  end
end
helper_paginate_offset(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 100
def self.helper_paginate_offset context, value, options
  self.helper_include context, 'pagination.offset', options
end
helper_quote(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 108
def self.helper_quote context, value, options
  if value.is_a?(V8::Array)
    value.collect{|v| ::ActiveRecord::Base.connection.quote v}.join(',')
  else
    ::ActiveRecord::Base.connection.quote value
  end
end
helper_wildcard(context, value, options) click to toggle source
# File lib/monster_queries/query.rb, line 104
def self.helper_wildcard context, value, options
  ::ActiveRecord::Base.connection.quote "%#{value.gsub('\\','\\\\\\')}%"
end
locate_file(scope) click to toggle source
# File lib/monster_queries/query.rb, line 36
def self.locate_file scope
  search_paths = [Rails.root,MonsterQueries::Engine.root]
  search_paths.push(MonsterQueries::Query.root) if MonsterQueries::Query.root
  search_paths.each do |path|
    file = path.join('app','queries',*scope.compact.map(&:to_s)).to_s + '.sql'
    return file if File.exists?(file)
  end
  return false
end
method_missing(name) click to toggle source

Method Missing is used to create a chain path to the query eg. Q.admin.users.index

# File lib/monster_queries/query.rb, line 22
def self.method_missing name
  MonsterQueries::Builder.new name
end
paginate() click to toggle source
# File lib/monster_queries/query.rb, line 26
def self.paginate
  q = MonsterQueries::Builder.new nil
  q.paginate = true
  q
end
root() click to toggle source
# File lib/monster_queries/query.rb, line 32
def self.root
  # override this method to include your own queries path
end
template(scope) click to toggle source
# File lib/monster_queries/query.rb, line 46
def self.template scope
  if !@template_cache[scope.join('.')] || SKIP_CACHE
    file_name = locate_file(scope)
    data = File.read file_name
    @template_cache[scope.join('.')] = handlebars.compile(data, noEscape: true)
  end
  @template_cache[scope.join('.')]
end
template_from_parts(parts, vars) click to toggle source
# File lib/monster_queries/query.rb, line 10
def self.template_from_parts parts, vars
  MonsterQueries::Builder.with_scope(parts).to_s vars
end
template_from_string(string, vars) click to toggle source
# File lib/monster_queries/query.rb, line 14
def self.template_from_string string, vars
  template = handlebars.compile string, noEscape: true
  result   = template.call vars
  result
end