Class: Querylet::Tree::Partial

Inherits:
Object
  • Object
show all
Defined in:
lib/querylet/tree.rb

Instance Method Summary collapse

Instance Method Details

#_eval(context) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/querylet/tree.rb', line 38

def _eval(context)
  data = {}
  [parameters].flatten.map(&:values).map do |vals|
    # we have to set it as sym so it overrides correctly
    data[vals.first.to_sym] = vals.last._eval(context)
  end
  content = context.get_partial(partial.to_s, path, data)
  if partial == 'array'
  <<-HEREDOC.chomp
(SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
#{content}
) array_row)
  HEREDOC
  elsif partial == 'object'
  <<-HEREDOC.chomp
(SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
#{content}
) object_row)
HEREDOC
  elsif partial == 'include'
    content
  end
end