Class: MonsterQueries::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/monster_queries/query.rb

Constant Summary collapse

SKIP_CACHE =
true

Class Method Summary collapse

Class Method Details

.exists?(scope) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/monster_queries/query.rb', line 50

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

.handlebarsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/monster_queries/query.rb', line 58

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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/monster_queries/query.rb', line 119

def self.helper_array context, block, options
  content =
  if block.is_a?(String)
    "\n" + helper_include(context, block, options)
  else
    block.fn context
  end
  "(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (\n\#{content}\n) array_row)\n  HEREDOC\nend\n"

.helper_float(context, value, options) ⇒ Object



115
116
117
# File 'lib/monster_queries/query.rb', line 115

def self.helper_float context, value, options
  value.to_f
end

.helper_include(context, name, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/monster_queries/query.rb', line 73

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) ⇒ Object



111
112
113
# File 'lib/monster_queries/query.rb', line 111

def self.helper_int context, value, options
  value.to_i
end

.helper_object(context, block, options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/monster_queries/query.rb', line 133

def self.helper_object context, block, options
  content =
  if block.is_a?(String)
    "\n" + helper_include(context, block, options)
  else
    block.fn context
  end
  "(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (\n\#{content}\n) object_row)\n  HEREDOC\nend\n"

.helper_paginate(context, value, options) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/monster_queries/query.rb', line 85

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) ⇒ Object



95
96
97
# File 'lib/monster_queries/query.rb', line 95

def self.helper_paginate_offset context, value, options
  self.helper_include context, 'pagination.offset', options
end

.helper_quote(context, value, options) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/monster_queries/query.rb', line 103

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) ⇒ Object



99
100
101
# File 'lib/monster_queries/query.rb', line 99

def self.helper_wildcard context, value, options
  ::ActiveRecord::Base.connection.quote "%#{value.gsub('\\','\\\\\\')}%"
end

.locate_file(scope) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/monster_queries/query.rb', line 32

def self.locate_file scope
  search_paths = [Rails.root,MonsterQueries::Engine.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) ⇒ Object

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



22
23
24
# File 'lib/monster_queries/query.rb', line 22

def self.method_missing name
  MonsterQueries::Builder.new name
end

.paginateObject



26
27
28
29
30
# File 'lib/monster_queries/query.rb', line 26

def self.paginate
  q = MonsterQueries::Builder.new nil
  q.paginate = true
  q
end

.template(scope) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/monster_queries/query.rb', line 41

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) ⇒ Object



10
11
12
# 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) ⇒ Object



14
15
16
17
18
# 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