querylet 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ec97dcd15beb09f85b656e57b8ab282ec722955f411afaaef3db77ac3682230
4
- data.tar.gz: 1d532fbbc2a5335703348f00d645f48ffe406943a0084ebf8ebf2ac97ba9c3c5
3
+ metadata.gz: 89d1a03b5028328b79f0d575c2aeadec57ebf47f1d743fe5722b4c4a4b165fc6
4
+ data.tar.gz: 5db0889a5a8fc5a21aca04bb27f406522a22de46f91ef1fd95af5f0137983156
5
5
  SHA512:
6
- metadata.gz: f48440d625e46181aea476dfe79f3e4f354c2848cb7d6b480f4e093ebc739d1b30a019cf55356bb338db2bab29bc1be54817d4691f528817c4b54a8607a00dd0
7
- data.tar.gz: 86552d7a090baf83feb29d94aff63474af7a8f43816027c74e07a05d35f67d3b329ffd9784348b98a6ce87d7c892cf80462755b6b5affedb50e2d29a38745a0d
6
+ metadata.gz: 296e9452984b0099681d8567ac291e1c71e9ee62cb706d96668927c0e2cab8062ac54f0c1186eed56bdf2e0a93ee463602a75928525fcb1d02eefe3089fae665
7
+ data.tar.gz: 3b1f422e8cabd1ee7dfbb977df2e3b77ed4d28a6b5bb93f0007cf20aff608737276a8504ca40239671dcd8b66d4e4f8bacaf2a8db92a62d746c200a5e7d7f687
@@ -1,5 +1,10 @@
1
1
  module Querylet
2
- module Context
2
+ class Context
3
+ def initialize(querylet, data)
4
+ @querylet = querylet
5
+ @data = data
6
+ end
7
+
3
8
  def get(value)
4
9
  @data.merge(locals)[value.to_sym]
5
10
  end
@@ -12,15 +17,21 @@ module Querylet
12
17
  hash.map { |k, v| add_item(k, v) }
13
18
  end
14
19
 
15
- def with_temporary_context(args = {})
16
- saved = args.keys.collect { |key| [key, get(key.to_s)] }.to_h
20
+ def get_partial name, dot_path, data
21
+ @querylet.get_partial name, dot_path, @data.merge(data)
22
+ end
17
23
 
18
- add_items(args)
19
- block_result = yield
20
- locals.merge!(saved)
24
+ # This never appears to be used
25
+ # commented out unless I find a use for it.
26
+ #def with_temporary_context(args = {})
27
+ # saved = args.keys.collect { |key| [key, get(key.to_s)] }.to_h
21
28
 
22
- block_result
23
- end
29
+ # add_items(args)
30
+ # block_result = yield
31
+ # locals.merge!(saved)
32
+
33
+ # block_result
34
+ #end
24
35
 
25
36
  private
26
37
 
@@ -1,4 +1,4 @@
1
- #require_relative 'context'
1
+ require_relative 'context'
2
2
 
3
3
  module Querylet
4
4
  class Template
@@ -7,13 +7,19 @@ module Querylet
7
7
  @ast = ast
8
8
  end
9
9
 
10
+ # In Ruby, the call method is used to invoke a block, proc, or lambda,
11
+ # which are all different types of callable objects in Ruby.
12
+ #
13
+ # This is how querylet will get called in a developers code.
14
+ #
15
+ # querylet = Querylet::Querylet.new path: 'path/to/sql'
16
+ # querylet.compile(template).call(data)
17
+ # @args is the initial values passed to the template
10
18
  def call(args = nil)
11
- if args
12
- @querylet.set_context(args)
13
- end
19
+ ctx = Context.new(@querylet, args)
14
20
 
15
21
  # AST should return a Querylet::Tree and call its eval method
16
- @ast.eval(@querylet)
22
+ @ast.eval(ctx)
17
23
  end
18
- end
19
- end
24
+ end # Template
25
+ end # Querylet
data/lib/querylet/tree.rb CHANGED
@@ -36,10 +36,12 @@ module Querylet
36
36
 
37
37
  class Partial < TreeItem.new(:partial, :path, :parameters)
38
38
  def _eval(context)
39
+ data = {}
39
40
  [parameters].flatten.map(&:values).map do |vals|
40
- context.add_item vals.first.to_s, vals.last._eval(context)
41
+ # we have to set it as sym so it overrides correctly
42
+ data[vals.first.to_sym] = vals.last._eval(context)
41
43
  end
42
- content = context.get_partial(partial.to_s, path)
44
+ content = context.get_partial(partial.to_s, path, data)
43
45
  if partial == 'array'
44
46
  <<-HEREDOC.chomp
45
47
  (SELECT COALESCE(array_to_json(array_agg(row_to_json(array_row))),'[]'::json) FROM (
@@ -1,4 +1,4 @@
1
1
  module Querylet
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
4
4
 
data/lib/querylet.rb CHANGED
@@ -6,7 +6,6 @@ require_relative 'querylet/context'
6
6
 
7
7
  module Querylet
8
8
  class Querylet
9
- include Context
10
9
  def initialize(path:)
11
10
  @sql_path = path
12
11
  end
@@ -19,17 +18,15 @@ module Querylet
19
18
  #puts deep_nested_hash
20
19
  abstract_syntax_tree = transform.apply deep_nested_hash
21
20
  #puts abstract_syntax_tree
22
- Template.new self, abstract_syntax_tree
23
- end
24
21
 
25
- def set_context(ctx)
26
- @data = ctx
22
+ # Querylet::Template
23
+ Template.new self, abstract_syntax_tree
27
24
  end
28
25
 
29
- def get_partial name, dot_path
26
+ def get_partial name, dot_path, data={}
30
27
  path = @sql_path + '/' + dot_path.to_s.split('.').join('/') + '.sql'
31
28
  template = File.read(path).to_s.chomp
32
- self.compile(template).call(@data)
29
+ self.compile(template).call(data)
33
30
  end
34
31
  end # class Querylet
35
32
  end # module Querylet
@@ -23,66 +23,40 @@ describe Querylet::Querylet do
23
23
 
24
24
  context 'helpers' do
25
25
  it 'include' do
26
- query = <<-SQL.chomp
27
- (SELECT
28
- users.email
29
- FROM users
30
- WHERE
31
- users.id = 1) as email
32
- SQL
26
+ query = "(SELECT users.email FROM users WHERE users.id = 1) as email"
33
27
  expect(evaluate("({{> include 'examples.include' }}) as email")).to eq(query)
34
28
  end
35
29
 
36
30
  it 'include with variables' do
37
- query = <<-SQL.chomp
38
- (SELECT
39
- users.email
40
- FROM users
41
- WHERE
42
- users.id = 100) as email
43
- SQL
31
+ query = "(SELECT users.email FROM users WHERE users.id = 100) as email"
44
32
  template = "({{> include 'examples.include_with_vars' }}) as email"
45
33
  expect(evaluate(template, {id: 100})).to eq(query)
46
34
  end
47
35
 
48
36
  it 'include with parameters' do
49
- query = <<-SQL.chomp
50
- (SELECT
51
- users.email,
52
- 'andrew' as name
53
- FROM users
54
- WHERE
55
- users.id = 200) as email
56
- SQL
37
+ query = "(SELECT users.email, 'andrew' as name FROM users WHERE users.id = 200) as email"
57
38
  template = "({{> include 'examples.include_with_params' id='200' name='andrew' }}) as email"
58
39
  expect(evaluate(template, {id: 100})).to eq(query)
59
40
  end
60
41
 
61
42
  it 'include with parameters variable' do
62
- query = <<-SQL.chomp
63
- (SELECT
64
- users.email
65
- FROM users
66
- WHERE
67
- users.id = 300) as email
68
- SQL
43
+ query = "(SELECT users.email FROM users WHERE users.id = 300) as email"
69
44
  template = "({{> include 'examples.include_with_params_vars' id={{user_id}} name='andrew' }}) as email"
70
45
  expect(evaluate(template, {user_id: 300})).to eq(query)
71
46
  end
72
47
 
73
48
  it 'include with parameters filter' do
74
- query = <<-SQL.chomp
75
- (SELECT
76
- users.email,
77
- 'Andrew' as name
78
- FROM users
79
- WHERE
80
- users.id = 100) as email
81
- SQL
49
+ query = "(SELECT users.email, 'Andrew' as name FROM users WHERE users.id = 100) as email"
82
50
  template = "({{> include 'examples.include_with_params_filter' name={{str my_name}} }}) as email"
83
51
  expect(evaluate(template, {my_name: "Andrew", id: 100})).to eq(query)
84
52
  end
85
53
 
54
+ it 'variable overrides' do
55
+ query = "(SELECT questions.id, (SELECT que.id FROM questions que WHERE que.id = 555) as sub FROM questions WHERE questions.id = 100"
56
+ template = "(SELECT questions.id, ({{> include 'examples.vars_overrides_include' xid={{sub_id}} }}) as sub FROM questions WHERE questions.id = {{xid}}"
57
+ expect(evaluate(template, {sub_id: 555, xid: 100})).to eq(query)
58
+ end
59
+
86
60
  it 'object' do
87
61
  query = <<-SQL.chomp
88
62
  (SELECT COALESCE(row_to_json(object_row),'{}'::json) FROM (
@@ -177,5 +151,5 @@ SQL
177
151
  end
178
152
 
179
153
  end # context 'helpers'
180
- end # context 'evaluating'
154
+ end # context 'evaluating'
181
155
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querylet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ExamPro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-04 00:00:00.000000000 Z
11
+ date: 2023-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet