cypher_builder 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab4195a4df744da8eb1f73ce1a4fd4572ba6dc52
4
- data.tar.gz: 15140e002d3c33d9fbbefa43d44b9a65f1c27188
3
+ metadata.gz: a296dcf5a03aae2c6ad43c178ed2150808b7cf56
4
+ data.tar.gz: 586c6549b516bb866d454a3f1bdb26a9b9069bdd
5
5
  SHA512:
6
- metadata.gz: f7458389e9d48d6c078a84338707f755397ed93f76260dd48c633d2c8eb1169b36cb89d6a2d50ad025226d68bc3345d819828a74719e24e4d71ad602a1388160
7
- data.tar.gz: fbb03ca65fe4c12ee72b037c703e6e0a6444e8d13b0b93c0c67ae537000567cc5f1993d41a9eb1048740b35fabd584380333fe3fc55ebd35c899542ffd1285fc
6
+ metadata.gz: bbc86c29b2ee15020301b4317a95bdd74889624a272e8917a3b4732f87a8ed060ed65aa987700aa408d3c19053b4f65d6b3fb2e4f560e1e2401105ecd5c9db82
7
+ data.tar.gz: c7b25acf4446a29861dadc71905c3af1458d05583c453be7f64481196ca4615b9a62dca49a9374a14208fa8d8045c6da17b03cce683bc1a7ed05f0ed6773dc5a
@@ -12,10 +12,12 @@ if defined?(::Neography)
12
12
  end
13
13
 
14
14
  require 'cypher_builder/payload'
15
+ require 'cypher_builder/context'
15
16
  require 'cypher_builder/resolver'
16
17
  require 'cypher_builder/runner'
17
18
  require 'cypher_builder/cypher'
18
19
 
20
+ require 'cypher_builder/as_is'
19
21
  require 'cypher_builder/opt'
20
22
  require 'cypher_builder/field'
21
23
  require 'cypher_builder/node'
@@ -11,8 +11,8 @@ module CypherBuilder
11
11
  @an_alias = an_alias
12
12
  end
13
13
 
14
- def as_cypher(opts)
15
- sprintf('%s AS %s', resolve(@field, ** opts), @an_alias)
14
+ def as_cypher(payload:, context: )
15
+ sprintf('%s AS %s', resolve(@field, payload: payload, context: context.add(self)), @an_alias)
16
16
  end
17
17
  end
18
18
  end
@@ -10,8 +10,8 @@ module CypherBuilder
10
10
  @parts = wrap(*parts)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- resolve(@parts, separator: ' AND ', ** opts)
13
+ def as_cypher(payload:, context: )
14
+ resolve(@parts, separator: ' AND ', payload: payload, context: context.add(self))
15
15
  end
16
16
  end
17
17
  end
@@ -0,0 +1,16 @@
1
+ module CypherBuilder
2
+ def AsIs(*args)
3
+ AsIs.new(*args)
4
+ end
5
+
6
+ class AsIs
7
+ include Resolver
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def as_cypher(_)
13
+ @value.to_s
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ class CypherBuilder::Context
2
+ def initialize(context = [])
3
+ @parents = context
4
+ end
5
+
6
+ def add(part)
7
+ Context.new([part] + @parents)
8
+ end
9
+
10
+ def ancestor?(a_class)
11
+ @parents.find { |p| a_class === p }
12
+ end
13
+ end
@@ -12,8 +12,8 @@ module CypherBuilder
12
12
  @parts = wrap(*parts)
13
13
  end
14
14
 
15
- def as_cypher(opts)
16
- resolve(@parts, ** opts)
15
+ def as_cypher(payload:, context:)
16
+ resolve(@parts, payload: payload, context: context.add(self))
17
17
  end
18
18
  end
19
19
  end
@@ -10,8 +10,10 @@ module CypherBuilder
10
10
  @left, @right = wrap(left, right)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- sprintf('%s = %s', resolve(@left, ** opts), resolve(@right, ** opts))
13
+ def as_cypher(payload:, context: )
14
+ sprintf('%s = %s',
15
+ resolve(@left, payload: payload, context: context.add(self)),
16
+ resolve(@right, payload: payload, context: context.add(self)))
15
17
  end
16
18
  end
17
19
  end
@@ -4,12 +4,26 @@ module CypherBuilder
4
4
  end
5
5
 
6
6
  class Field
7
+ attr_reader :name
8
+
7
9
  def initialize(prefix = nil, name)
8
10
  @prefix, @name = prefix, name
9
11
  end
10
12
 
11
- def as_cypher(_)
13
+ def as_cypher(payload:, context:)
14
+ if context.ancestor?(Return) && !context.ancestor?(Alias)
15
+ aliased
16
+ else
17
+ prefixed_name
18
+ end
19
+ end
20
+
21
+ def prefixed_name
12
22
  [@prefix, @name].compact.join('.')
13
23
  end
24
+
25
+ def aliased
26
+ sprintf('%s AS %s', prefixed_name, name)
27
+ end
14
28
  end
15
29
  end
@@ -10,8 +10,10 @@ module CypherBuilder
10
10
  @left, @right = wrap(left, right)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- sprintf('%s LIKE %s', resolve(@left, ** opts), resolve(@right, ** opts))
13
+ def as_cypher(payload:, context: )
14
+ sprintf('%s LIKE %s',
15
+ resolve(@left, payload: payload, context: context.add(self)),
16
+ resolve(@right, payload: payload, context: context.add(self)))
15
17
  end
16
18
  end
17
19
  end
@@ -10,8 +10,8 @@ module CypherBuilder
10
10
  @parts = wrap(*parts)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- resolve(@parts, format: 'MATCH (%s)', separator: ', ', ** opts)
13
+ def as_cypher(payload:, context: )
14
+ resolve(@parts, format: 'MATCH (%s)', separator: ', ', payload: payload, context: context.add(self))
15
15
  end
16
16
  end
17
17
  end
@@ -10,12 +10,12 @@ module CypherBuilder
10
10
  @params_and_parts = Hash[params_and_parts.map { |k, v| [k, wrap(v)] }]
11
11
  end
12
12
 
13
- def as_cypher(payload:)
13
+ def as_cypher(payload:, context:)
14
14
  param, part = @params_and_parts.find { |k, _| payload.include?(k) }
15
15
  part = @params_and_parts.values.first unless part
16
16
  payload.already_used(param) if param
17
17
 
18
- resolve(part, payload: payload)
18
+ resolve(part, payload: payload, context: context.add(self))
19
19
  end
20
20
  end
21
21
  end
@@ -8,7 +8,7 @@ module CypherBuilder
8
8
  @name = name
9
9
  end
10
10
 
11
- def as_cypher(payload:)
11
+ def as_cypher(payload:, context:)
12
12
  payload.will_be_used(@name)
13
13
  sprintf('{%s}', @name)
14
14
  end
@@ -9,7 +9,9 @@ module CypherBuilder::Resolver
9
9
  end
10
10
  end
11
11
 
12
- def resolve(parts, format: '%s', separator: ' ', payload:)
13
- Array(parts).map { |p| sprintf(format, p.as_cypher(payload: payload)) }.join(separator).strip
12
+ def resolve(parts, format: '%s', separator: ' ', ** opts)
13
+ Array(parts).map do |p|
14
+ sprintf(format, p.as_cypher(** opts))
15
+ end.join(separator).strip
14
16
  end
15
17
  end
@@ -10,8 +10,8 @@ module CypherBuilder
10
10
  @parts = wrap(*parts)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- sprintf('RETURN %s', resolve(@parts, separator: ', ', **opts))
13
+ def as_cypher(payload:, context: )
14
+ sprintf('RETURN %s', resolve(@parts, separator: ', ', payload: payload, context: context.add(self)))
15
15
  end
16
16
  end
17
17
  end
@@ -16,7 +16,8 @@ module CypherBuilder
16
16
 
17
17
  def execute(** params)
18
18
  payload = Payload.new(params)
19
- cypher = self.class.cypher.as_cypher(payload: payload)
19
+ context = Context.new
20
+ cypher = self.class.cypher.as_cypher(payload: payload, context: context)
20
21
  @adapter.execute(cypher, payload.necessary)
21
22
  end
22
23
  end
@@ -1,3 +1,3 @@
1
1
  module CypherBuilder
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -10,8 +10,8 @@ module CypherBuilder
10
10
  @parts = wrap(*parts)
11
11
  end
12
12
 
13
- def as_cypher(opts)
14
- resolve(@parts, format: 'WHERE %s', separator: ' AND ', ** opts)
13
+ def as_cypher(payload:, context: )
14
+ resolve(@parts, format: 'WHERE %s', separator: ' AND ', payload: payload, context: context.add(self))
15
15
  end
16
16
  end
17
17
  end
@@ -8,7 +8,7 @@ describe Cypher do
8
8
  cypher_class = Cypher(Match(c),
9
9
  Return(c.name))
10
10
  cypher_class.exec(adapter)
11
- expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
11
+ expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
12
12
  end
13
13
  end
14
14
  describe '#execute' do
@@ -22,7 +22,7 @@ describe Cypher do
22
22
  cypher_class = Cypher(Match(c),
23
23
  Return(c.name))
24
24
  cypher_class.new(adapter).execute
25
- expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
25
+ expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
26
26
  end
27
27
  it 'executes the most complex query possible (exercises everything currently implemented)' do
28
28
  c = Node('c', labels: 'what')
@@ -31,7 +31,7 @@ describe Cypher do
31
31
  Like(c.staff, 'test%'))),
32
32
  Return(c.name, Alias(c.stuff, 'something')))
33
33
  cypher_class.new(adapter).execute(thing: 'of course')
34
- expect(adapter).to have_received(:execute).with('MATCH (c:what) WHERE c.stuff = {thing} AND c.staff LIKE "test%" RETURN c.name, c.stuff AS something', {thing: 'of course'})
34
+ expect(adapter).to have_received(:execute).with('MATCH (c:what) WHERE c.stuff = {thing} AND c.staff LIKE "test%" RETURN c.name AS name, c.stuff AS something', {thing: 'of course'})
35
35
  end
36
36
  context 'with Opt' do
37
37
  before do
@@ -47,23 +47,23 @@ describe Cypher do
47
47
  end
48
48
  it 'generates first option' do
49
49
  @cypher_class.new(adapter).execute(name: true)
50
- expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
50
+ expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
51
51
  end
52
52
  it 'generates first option with multiple uses' do
53
53
  @cypher_class2.new(adapter).execute(name: 'Testing Test')
54
- expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.name = {name} RETURN c.name', {name: 'Testing Test'})
54
+ expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.name = {name} RETURN c.name AS name', {name: 'Testing Test'})
55
55
  end
56
56
  it 'generates second option' do
57
57
  @cypher_class.new(adapter).execute(thing: true)
58
- expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.thing', {})
58
+ expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.thing AS thing', {})
59
59
  end
60
60
  it 'generates second option with multiple uses' do
61
61
  @cypher_class2.new(adapter).execute(thing: 123)
62
- expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.thing = {thing} RETURN c.thing', {thing: 123})
62
+ expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.thing = {thing} RETURN c.thing AS thing', {thing: 123})
63
63
  end
64
64
  it 'defaults to firts option' do
65
65
  @cypher_class.new(adapter).execute
66
- expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
66
+ expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name AS name', {})
67
67
  end
68
68
  end
69
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cypher_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronie Uliana
@@ -86,6 +86,8 @@ files:
86
86
  - lib/cypher_builder/adapter/neography.rb
87
87
  - lib/cypher_builder/alias.rb
88
88
  - lib/cypher_builder/and.rb
89
+ - lib/cypher_builder/as_is.rb
90
+ - lib/cypher_builder/context.rb
89
91
  - lib/cypher_builder/cypher.rb
90
92
  - lib/cypher_builder/eql.rb
91
93
  - lib/cypher_builder/field.rb