cypher_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/Guardfile +50 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +11 -0
- data/cypher_builder.gemspec +24 -0
- data/lib/cypher_builder/adapter/neography.rb +15 -0
- data/lib/cypher_builder/alias.rb +18 -0
- data/lib/cypher_builder/and.rb +17 -0
- data/lib/cypher_builder/cypher.rb +19 -0
- data/lib/cypher_builder/eql.rb +17 -0
- data/lib/cypher_builder/field.rb +15 -0
- data/lib/cypher_builder/like.rb +17 -0
- data/lib/cypher_builder/literal.rb +17 -0
- data/lib/cypher_builder/match.rb +17 -0
- data/lib/cypher_builder/node.rb +23 -0
- data/lib/cypher_builder/opt.rb +21 -0
- data/lib/cypher_builder/param.rb +16 -0
- data/lib/cypher_builder/payload.rb +60 -0
- data/lib/cypher_builder/resolver.rb +15 -0
- data/lib/cypher_builder/return.rb +17 -0
- data/lib/cypher_builder/runner.rb +19 -0
- data/lib/cypher_builder/version.rb +3 -0
- data/lib/cypher_builder/where.rb +17 -0
- data/lib/cypher_builder.rb +27 -0
- data/spec/cypher_builder/cypher_spec.rb +61 -0
- data/spec/cypher_builder/node_spec.rb +31 -0
- data/spec/spec_helper.rb +3 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aff9aa777fa33ad8ecf66a47753c814e3c000848
|
4
|
+
data.tar.gz: 4cad00730445aa56fd112026abfdc2d40ea238f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e9d553e03ee2d52518776d8d644301e4ddcfbf5e0f723924155a10645f46259ef04289160bf877f750f7c0d80c8ac630994be642da81daf56513d804a86df720
|
7
|
+
data.tar.gz: a2287427a2418fdc15cf70881f370cbc642e965f97d3dac96d309c4e26e3c09004c4a4140d146782e1c60f5961c80229380e66cf6778f2563baa7fd7a81cba23
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
cypher_builder
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec feature)
|
6
|
+
|
7
|
+
## Uncomment to clear the screen before every task
|
8
|
+
clearing :on
|
9
|
+
|
10
|
+
## Guard internally checks for changes in the Guardfile and exits.
|
11
|
+
## If you want Guard to automatically start up again, run guard in a
|
12
|
+
## shell loop, e.g.:
|
13
|
+
##
|
14
|
+
## $ while bundle exec guard; do echo "Restarting Guard..."; done
|
15
|
+
##
|
16
|
+
## Note: if you are using the `directories` clause above and you are not
|
17
|
+
## watching the project directory ('.'), the you will want to move the Guardfile
|
18
|
+
## to a watched dir and symlink it back, e.g.
|
19
|
+
#
|
20
|
+
# $ mkdir config
|
21
|
+
# $ mv Guardfile config/
|
22
|
+
# $ ln -s config/Guardfile .
|
23
|
+
#
|
24
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
25
|
+
|
26
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
27
|
+
# rspec may be run, below are examples of the most common uses.
|
28
|
+
# * bundler: 'bundle exec rspec'
|
29
|
+
# * bundler binstubs: 'bin/rspec'
|
30
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
31
|
+
# installed the spring binstubs per the docs)
|
32
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
33
|
+
# * 'just' rspec: 'rspec'
|
34
|
+
|
35
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
36
|
+
require 'guard/rspec/dsl'
|
37
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
38
|
+
|
39
|
+
# Feel free to open issues for suggestions and improvements
|
40
|
+
|
41
|
+
# RSpec files
|
42
|
+
rspec = dsl.rspec
|
43
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
44
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
45
|
+
watch(rspec.spec_files)
|
46
|
+
|
47
|
+
# Ruby files
|
48
|
+
ruby = dsl.ruby
|
49
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
50
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Ronie Uliana
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# CypherBuilder
|
2
|
+
|
3
|
+
Build Cypher query classes (Neo4j).
|
4
|
+
|
5
|
+
It creates Command classes that executes Cypher queries using Neography. The goal is make class creation easier.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cypher_builder'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cypher_builder
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
include CypherBuilder
|
26
|
+
|
27
|
+
MyQuery = Cypher(Match('c:something'),
|
28
|
+
Where(Eql('c.name', Param('full_name'))),
|
29
|
+
Return('c.name',
|
30
|
+
Alias('c.stuff', 'description')))
|
31
|
+
|
32
|
+
Then, later:
|
33
|
+
|
34
|
+
query = MyQuery.new(Adapter::Neography.new)
|
35
|
+
array_of_hashes = query.execute(full_name: 'My Stuff')
|
36
|
+
|
37
|
+
Or:
|
38
|
+
|
39
|
+
# Default adapter to "Adapter::Neography"
|
40
|
+
array_of_hashes = MyQuery.exec(full_name: 'My Stuff')
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( https://github.com/ruliana/cypher_builder/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cypher_builder/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'cypher_builder'
|
8
|
+
spec.version = CypherBuilder::VERSION
|
9
|
+
spec.authors = ['Ronie Uliana']
|
10
|
+
spec.email = ['ronie.uliana@gmail.com']
|
11
|
+
spec.summary = %q{Build Cypher query classes (Neo4j).}
|
12
|
+
spec.description = %q{Build Cypher query classes (Neo4j).}
|
13
|
+
spec.homepage = 'https://github.com/ruliana/cypher_builder'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.3'
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CypherBuilder::Adapter::Neography
|
2
|
+
def initialize
|
3
|
+
@neo = ::Neography::Rest.new
|
4
|
+
end
|
5
|
+
|
6
|
+
# @param query [#to_s]
|
7
|
+
# @param params [#to_h]
|
8
|
+
# @return [[{}]]
|
9
|
+
def execute(query, params)
|
10
|
+
result = @neo.execute(query.to_s, params.to_h)
|
11
|
+
columns = result['columns']
|
12
|
+
data = result['data']
|
13
|
+
data.map { |values| columns.zip(values).to_h }
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Alias(field, an_alias)
|
3
|
+
Alias.new(field, an_alias)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Alias
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(field, an_alias)
|
10
|
+
@field = wrap(field)
|
11
|
+
@an_alias = an_alias
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_cypher(opts)
|
15
|
+
sprintf('%s AS %s', resolve(@field, ** opts), @an_alias)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def And(*parts)
|
3
|
+
And.new(*parts)
|
4
|
+
end
|
5
|
+
|
6
|
+
class And
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(*parts)
|
10
|
+
@parts = wrap(*parts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
resolve(@parts, separator: ' AND ', ** opts)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Cypher(*args)
|
3
|
+
Class.new(Runner).tap do |the_class|
|
4
|
+
the_class.cypher = Cypher.new(*args)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Cypher
|
9
|
+
include Resolver
|
10
|
+
|
11
|
+
def initialize(*parts)
|
12
|
+
@parts = wrap(*parts)
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_cypher(opts)
|
16
|
+
resolve(@parts, ** opts)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Eql(left, right)
|
3
|
+
Eql.new(left, right)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Eql
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(left, right)
|
10
|
+
@left, @right = wrap(left, right)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
sprintf('%s = %s', resolve(@left, ** opts), resolve(@right, ** opts))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Field(prefix = nil, name)
|
3
|
+
Field.new(prefix, name)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Field
|
7
|
+
def initialize(prefix = nil, name)
|
8
|
+
@prefix, @name = prefix, name
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_cypher(_)
|
12
|
+
[@prefix, @name].compact.join('.')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Like(left, right)
|
3
|
+
Like.new(left, right)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Like
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(left, right)
|
10
|
+
@left, @right = wrap(left, right)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
sprintf('%s LIKE %s', resolve(@left, ** opts), resolve(@right, ** opts))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Match(*args)
|
3
|
+
Match.new(*args)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Match
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(*parts)
|
10
|
+
@parts = wrap(*parts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
resolve(@parts, format: 'MATCH (%s)', separator: ', ', ** opts)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Node(prefix, labels: [])
|
3
|
+
Node.new(prefix, labels: labels)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Node < BasicObject
|
7
|
+
def initialize(prefix, labels: [])
|
8
|
+
@prefix, @labels = prefix, ::Kernel.Array(labels)
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_cypher(_ = nil)
|
12
|
+
[@prefix, *@labels].compact.join(':')
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond_to_missing?(name, include_private = false)
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(name, *_)
|
20
|
+
::Field.new(@prefix, name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Opt(*parts)
|
3
|
+
Opt.new(*parts)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Opt
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(** params_and_parts)
|
10
|
+
@params_and_parts = Hash[params_and_parts.map { |k, v| [k, wrap(v)] }]
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(payload:)
|
14
|
+
param, part = @params_and_parts.find { |k, _| payload.include?(k) }
|
15
|
+
part = @params_and_parts.values.first unless part
|
16
|
+
payload.already_used(param) if param
|
17
|
+
|
18
|
+
resolve(part, payload: payload)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
class CypherBuilder::Payload
|
2
|
+
class WithStatus < Struct.new(:value, :status)
|
3
|
+
def to_status(new_status)
|
4
|
+
WithStatus.new(value, new_status)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(params)
|
9
|
+
@params = params.map { |k, v| [k.to_sym, with_status(v, :pending)] }.to_h
|
10
|
+
end
|
11
|
+
|
12
|
+
def will_be_used(param_name)
|
13
|
+
set_status(param_name, :will_be_used)
|
14
|
+
end
|
15
|
+
|
16
|
+
def already_used(param_name)
|
17
|
+
set_status(param_name, :already_used) unless get_status(param_name) == :will_be_used
|
18
|
+
end
|
19
|
+
|
20
|
+
def include?(param_name)
|
21
|
+
@params.keys.include?(param_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
def pendings
|
25
|
+
select(:pending)
|
26
|
+
end
|
27
|
+
|
28
|
+
def necessary
|
29
|
+
select(:will_be_used)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def get(key)
|
35
|
+
@params.fetch(key.to_sym)
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_status(key)
|
39
|
+
get(key).status
|
40
|
+
end
|
41
|
+
|
42
|
+
def set(key, value)
|
43
|
+
@params[key.to_sym] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_status(key, status)
|
47
|
+
set(key, get(key).to_status(status))
|
48
|
+
end
|
49
|
+
|
50
|
+
def with_status(value, status)
|
51
|
+
WithStatus.new(value, status)
|
52
|
+
end
|
53
|
+
|
54
|
+
def select(status)
|
55
|
+
@params.
|
56
|
+
select { |_, v| v.status == status }.
|
57
|
+
map { |k, v| [k, v.value] }.
|
58
|
+
to_h
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CypherBuilder::Resolver
|
2
|
+
def wrap(*values)
|
3
|
+
values.map do |v|
|
4
|
+
if v.respond_to?(:as_cypher)
|
5
|
+
v
|
6
|
+
else
|
7
|
+
Literal.new(v)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def resolve(parts, format: '%s', separator: ' ', payload:)
|
13
|
+
Array(parts).map { |p| sprintf(format, p.as_cypher(payload: payload)) }.join(separator).strip
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Return(*parts)
|
3
|
+
Return.new(*parts)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Return
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(*parts)
|
10
|
+
@parts = wrap(*parts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
sprintf('RETURN %s', resolve(@parts, separator: ', ', **opts))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
class Runner
|
3
|
+
class << self
|
4
|
+
# @return [Cypher]
|
5
|
+
attr_accessor :cypher
|
6
|
+
end
|
7
|
+
|
8
|
+
# @param adapter [Adapter]
|
9
|
+
def initialize(adapter = Adapter::DEFAULT)
|
10
|
+
@adapter = adapter
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute(** params)
|
14
|
+
payload = Payload.new(params)
|
15
|
+
cypher = self.class.cypher.as_cypher(payload: payload)
|
16
|
+
@adapter.execute(cypher, payload.necessary)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CypherBuilder
|
2
|
+
def Where(*parts)
|
3
|
+
Where.new(*parts)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Where
|
7
|
+
include Resolver
|
8
|
+
|
9
|
+
def initialize(*parts)
|
10
|
+
@parts = wrap(*parts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_cypher(opts)
|
14
|
+
resolve(@parts, format: 'WHERE %s', separator: ' AND ', ** opts)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'cypher_builder/version'
|
2
|
+
|
3
|
+
module CypherBuilder
|
4
|
+
module Adapter
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'cypher_builder/adapter/neography'
|
9
|
+
CypherBuilder::Adapter::DEFAULT = CypherBuilder::Adapter::Neography.new if defined?(::Neography)
|
10
|
+
|
11
|
+
require 'cypher_builder/payload'
|
12
|
+
require 'cypher_builder/resolver'
|
13
|
+
require 'cypher_builder/runner'
|
14
|
+
require 'cypher_builder/cypher'
|
15
|
+
|
16
|
+
require 'cypher_builder/opt'
|
17
|
+
require 'cypher_builder/field'
|
18
|
+
require 'cypher_builder/node'
|
19
|
+
require 'cypher_builder/param'
|
20
|
+
require 'cypher_builder/literal'
|
21
|
+
require 'cypher_builder/match'
|
22
|
+
require 'cypher_builder/where'
|
23
|
+
require 'cypher_builder/and'
|
24
|
+
require 'cypher_builder/eql'
|
25
|
+
require 'cypher_builder/like'
|
26
|
+
require 'cypher_builder/return'
|
27
|
+
require 'cypher_builder/alias'
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cypher do
|
4
|
+
describe '#execute' do
|
5
|
+
let(:adapter) { instance_spy('Adapter') }
|
6
|
+
it 'executes an empty query' do
|
7
|
+
cypher_class = Cypher()
|
8
|
+
cypher_class.new(adapter).execute
|
9
|
+
expect(adapter).to have_received(:execute).with('', {})
|
10
|
+
end
|
11
|
+
it 'executes a simple query' do
|
12
|
+
c = Node('c')
|
13
|
+
cypher_class = Cypher(Match(c),
|
14
|
+
Return(c.name))
|
15
|
+
cypher_class.new(adapter).execute
|
16
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
17
|
+
end
|
18
|
+
it 'executes the most complex query possible (exercises everything currently implemented)' do
|
19
|
+
c = Node('c', labels: 'what')
|
20
|
+
cypher_class = Cypher(Match(c),
|
21
|
+
Where(And(Eql(c.stuff, Param('thing')),
|
22
|
+
Like(c.staff, 'test%'))),
|
23
|
+
Return(c.name, Alias(c.stuff, 'something')))
|
24
|
+
cypher_class.new(adapter).execute(thing: 'of course')
|
25
|
+
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'})
|
26
|
+
end
|
27
|
+
context 'with Opt' do
|
28
|
+
before do
|
29
|
+
c = Node('c')
|
30
|
+
@cypher_class = Cypher(Match(c),
|
31
|
+
Opt(name: Return(c.name),
|
32
|
+
thing: Return(c.thing)))
|
33
|
+
|
34
|
+
@cypher_class2 = Cypher(Match(c),
|
35
|
+
Where(Opt(name: Eql(c.name, Param('name')),
|
36
|
+
thing: Eql(c.thing, Param('thing')))),
|
37
|
+
Return(Opt(name: c.name, thing: c.thing)))
|
38
|
+
end
|
39
|
+
it 'generates first option' do
|
40
|
+
@cypher_class.new(adapter).execute(name: true)
|
41
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
42
|
+
end
|
43
|
+
it 'generates first option with multiple uses' do
|
44
|
+
@cypher_class2.new(adapter).execute(name: 'Testing Test')
|
45
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.name = {name} RETURN c.name', {name: 'Testing Test'})
|
46
|
+
end
|
47
|
+
it 'generates second option' do
|
48
|
+
@cypher_class.new(adapter).execute(thing: true)
|
49
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.thing', {})
|
50
|
+
end
|
51
|
+
it 'generates second option with multiple uses' do
|
52
|
+
@cypher_class2.new(adapter).execute(thing: 123)
|
53
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) WHERE c.thing = {thing} RETURN c.thing', {thing: 123})
|
54
|
+
end
|
55
|
+
it 'defaults to firts option' do
|
56
|
+
@cypher_class.new(adapter).execute
|
57
|
+
expect(adapter).to have_received(:execute).with('MATCH (c) RETURN c.name', {})
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Node do
|
4
|
+
let(:node) { Node('x', labels: 'uga') }
|
5
|
+
|
6
|
+
describe '#as_cypher' do
|
7
|
+
let(:node_prefix_only) { Node('x') }
|
8
|
+
let(:node_multiple_labels) { Node('x', labels: ['uga', 'buga']) }
|
9
|
+
|
10
|
+
it 'converts to be used in "match"' do
|
11
|
+
expect(node.as_cypher).to eq 'x:uga'
|
12
|
+
expect(node_prefix_only.as_cypher).to eq 'x'
|
13
|
+
expect(node_multiple_labels.as_cypher).to eq 'x:uga:buga'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#respond_to?' do
|
18
|
+
it 'is true for #as_cypher' do
|
19
|
+
expect(node.respond_to?(:as_cypher)).to be_truthy
|
20
|
+
end
|
21
|
+
it 'is true for any method' do
|
22
|
+
expect(node.respond_to?(:anything)).to be_truthy
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'any method' do
|
27
|
+
it 'responds with a Field' do
|
28
|
+
expect(node.some_field).to be_instance_of Field
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cypher_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ronie Uliana
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.3'
|
55
|
+
description: Build Cypher query classes (Neo4j).
|
56
|
+
email:
|
57
|
+
- ronie.uliana@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".ruby-gemset"
|
64
|
+
- ".ruby-version"
|
65
|
+
- Gemfile
|
66
|
+
- Guardfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- cypher_builder.gemspec
|
71
|
+
- lib/cypher_builder.rb
|
72
|
+
- lib/cypher_builder/adapter/neography.rb
|
73
|
+
- lib/cypher_builder/alias.rb
|
74
|
+
- lib/cypher_builder/and.rb
|
75
|
+
- lib/cypher_builder/cypher.rb
|
76
|
+
- lib/cypher_builder/eql.rb
|
77
|
+
- lib/cypher_builder/field.rb
|
78
|
+
- lib/cypher_builder/like.rb
|
79
|
+
- lib/cypher_builder/literal.rb
|
80
|
+
- lib/cypher_builder/match.rb
|
81
|
+
- lib/cypher_builder/node.rb
|
82
|
+
- lib/cypher_builder/opt.rb
|
83
|
+
- lib/cypher_builder/param.rb
|
84
|
+
- lib/cypher_builder/payload.rb
|
85
|
+
- lib/cypher_builder/resolver.rb
|
86
|
+
- lib/cypher_builder/return.rb
|
87
|
+
- lib/cypher_builder/runner.rb
|
88
|
+
- lib/cypher_builder/version.rb
|
89
|
+
- lib/cypher_builder/where.rb
|
90
|
+
- spec/cypher_builder/cypher_spec.rb
|
91
|
+
- spec/cypher_builder/node_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: https://github.com/ruliana/cypher_builder
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.5
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Build Cypher query classes (Neo4j).
|
117
|
+
test_files:
|
118
|
+
- spec/cypher_builder/cypher_spec.rb
|
119
|
+
- spec/cypher_builder/node_spec.rb
|
120
|
+
- spec/spec_helper.rb
|