rom-rethinkdb 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +67 -0
- data/.rubocop_todo.yml +21 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +15 -0
- data/LICENSE +22 -0
- data/README.md +36 -0
- data/Rakefile +10 -0
- data/lib/rom-rethinkdb.rb +1 -0
- data/lib/rom/rethinkdb.rb +8 -0
- data/lib/rom/rethinkdb/commands.rb +3 -0
- data/lib/rom/rethinkdb/commands/create.rb +34 -0
- data/lib/rom/rethinkdb/commands/delete.rb +22 -0
- data/lib/rom/rethinkdb/commands/update.rb +29 -0
- data/lib/rom/rethinkdb/dataset.rb +35 -0
- data/lib/rom/rethinkdb/gateway.rb +74 -0
- data/lib/rom/rethinkdb/relation.rb +34 -0
- data/lib/rom/rethinkdb/version.rb +5 -0
- data/rom-rethinkdb.gemspec +26 -0
- data/spec/integration/commands/create_spec.rb +77 -0
- data/spec/integration/commands/delete_spec.rb +52 -0
- data/spec/integration/commands/update_spec.rb +68 -0
- data/spec/integration/repository_spec.rb +122 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/prepare_db.rb +50 -0
- data/spec/unit/gateway_spec.rb +24 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf51e41aa01e1f33029552faf947afdb96819a4b
|
4
|
+
data.tar.gz: 388abcfd2f07fdc77ec2c239d26554138e74a499
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 156ed915c53b0af035210b6cde5c9c347e87c642dc4153a7c70e6d44efd4b39fcf114741b8761c2cb685b0747cab336fb9d8243c289fea0fc558afbae0f08348
|
7
|
+
data.tar.gz: 84b853a2790d83828229e032e06237e74ae91890ed4d7ef7b1f9fb59d4cc22df61f9670bccc701cc307bfbb028183582af1122c7fcb9f68e2927ea97733addc7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by `rubocop --auto-gen-config`
|
2
|
+
inherit_from: .rubocop_todo.yml
|
3
|
+
|
4
|
+
# It’s quite readable when we know what we are doing
|
5
|
+
Lint/AssignmentInCondition:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
# No need to handle LoadError in rake
|
9
|
+
Lint/HandleExceptions:
|
10
|
+
Exclude:
|
11
|
+
- Rakefile
|
12
|
+
|
13
|
+
# The enforced style doesn’t match Vim’s defaults
|
14
|
+
Style/AlignParameters:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
# UTF-8 is perfectly fine in comments
|
18
|
+
Style/AsciiComments:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
# Allow using braces for value-returning blocks
|
22
|
+
Style/Blocks:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
# Documentation checked by Inch CI
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
# Early returns have their vices
|
30
|
+
Style/GuardClause:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
# Need to be skipped for >-> usage
|
34
|
+
Style/Lambda:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
# Multiline block chains are ok
|
38
|
+
Style/MultilineBlockChain:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
# Even a single escaped slash can be confusing
|
42
|
+
Style/RegexpLiteral:
|
43
|
+
MaxSlashes: 0
|
44
|
+
|
45
|
+
# Don’t introduce semantic fail/raise distinction
|
46
|
+
Style/SignalException:
|
47
|
+
EnforcedStyle: only_raise
|
48
|
+
|
49
|
+
# Need to be skipped for >-> usage
|
50
|
+
Style/SpaceAroundOperators:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
# Accept both single and double quotes
|
54
|
+
Style/StringLiterals:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
# Allow def self.foo; @foo; end
|
58
|
+
Style/TrivialAccessors:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
# Allow rom-rethinkdb
|
62
|
+
Style/FileName:
|
63
|
+
Enabled: false
|
64
|
+
|
65
|
+
# Allow logical `or`/`and` in conditionals
|
66
|
+
Style/AndOr:
|
67
|
+
EnforcedStyle: conditionals
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2015-05-01 14:40:38 +0200 using RuboCop version 0.30.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 2
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
# Offense count: 1
|
13
|
+
# Configuration parameters: Exclude.
|
14
|
+
Style/FileName:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
# Offense count: 16
|
18
|
+
# Cop supports --auto-correct.
|
19
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
20
|
+
Style/StringLiterals:
|
21
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without yard guard benchmarks
|
3
|
+
before_script:
|
4
|
+
- source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
|
5
|
+
- wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
|
6
|
+
- sudo apt-get update
|
7
|
+
- sudo apt-get install rethinkdb
|
8
|
+
- sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
|
9
|
+
- sudo /etc/init.d/rethinkdb restart
|
10
|
+
script: "bundle exec rake spec"
|
11
|
+
rvm:
|
12
|
+
- 2.0
|
13
|
+
- 2.1
|
14
|
+
- 2.2
|
15
|
+
- rbx-2
|
16
|
+
- jruby
|
17
|
+
- ruby-head
|
18
|
+
matrix:
|
19
|
+
allow_failures:
|
20
|
+
- rvm: ruby-head
|
21
|
+
notifications:
|
22
|
+
webhooks:
|
23
|
+
urls:
|
24
|
+
- https://webhooks.gitter.im/e/39e1225f489f38b0bd09
|
25
|
+
on_success: change
|
26
|
+
on_failure: always
|
27
|
+
on_start: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'rom', github: 'rom-rb/rom', branch: 'master'
|
7
|
+
gem 'virtus'
|
8
|
+
gem 'activesupport'
|
9
|
+
gem 'rspec', '~> 3.1'
|
10
|
+
gem 'codeclimate-test-reporter', require: false
|
11
|
+
end
|
12
|
+
|
13
|
+
group :tools do
|
14
|
+
gem 'rubocop', '~> 0.28.0'
|
15
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Ruby Object Mapper
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
[gem]: https://rubygems.org/gems/rom-rethinkdb
|
2
|
+
[travis]: https://travis-ci.org/rom-rb/rom-rethinkdb
|
3
|
+
[gemnasium]: https://gemnasium.com/rom-rb/rom-rethinkdb
|
4
|
+
[codeclimate]: https://codeclimate.com/github/rom-rb/rom-rethinkdb
|
5
|
+
[inchpages]: http://inch-ci.org/github/rom-rb/rom-rethinkdb
|
6
|
+
|
7
|
+
# ROM::Rethinkdb
|
8
|
+
|
9
|
+
[][gem]
|
10
|
+
[][travis]
|
11
|
+
[][gemnasium]
|
12
|
+
[][codeclimate]
|
13
|
+
[][codeclimate]
|
14
|
+
[][inchpages]
|
15
|
+
|
16
|
+
RethinkDB suport for [Ruby Object Mapper](https://github.com/rom-rb/rom).
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'rom-rethinkdb'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install rom-rethinkdb
|
33
|
+
|
34
|
+
## License
|
35
|
+
|
36
|
+
See `LICENSE` file.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rom/rethinkdb'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/create'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module RethinkDB
|
6
|
+
module Commands
|
7
|
+
class Create < ROM::Commands::Create
|
8
|
+
adapter :rethinkdb
|
9
|
+
|
10
|
+
def execute(tuples)
|
11
|
+
insert_tuples = [tuples].flatten.map do |tuple|
|
12
|
+
attributes = input[tuple]
|
13
|
+
validator.call(attributes)
|
14
|
+
attributes.to_h
|
15
|
+
end
|
16
|
+
|
17
|
+
insert(insert_tuples)
|
18
|
+
end
|
19
|
+
|
20
|
+
def insert(tuples)
|
21
|
+
pks = dataset.scope.insert(tuples)
|
22
|
+
.run(dataset.connection)["generated_keys"]
|
23
|
+
|
24
|
+
dataset.filter { |user| relation.rql.expr(pks).contains(user["id"]) }
|
25
|
+
.to_a
|
26
|
+
end
|
27
|
+
|
28
|
+
def dataset
|
29
|
+
relation.dataset
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/delete'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module RethinkDB
|
6
|
+
module Commands
|
7
|
+
class Delete < ROM::Commands::Delete
|
8
|
+
adapter :rethinkdb
|
9
|
+
|
10
|
+
def execute
|
11
|
+
deleted = dataset.to_a
|
12
|
+
dataset.scope.delete.run(dataset.connection)
|
13
|
+
deleted
|
14
|
+
end
|
15
|
+
|
16
|
+
def dataset
|
17
|
+
relation.dataset
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/update'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module RethinkDB
|
6
|
+
module Commands
|
7
|
+
class Update < ROM::Commands::Update
|
8
|
+
adapter :rethinkdb
|
9
|
+
|
10
|
+
def execute(tuple)
|
11
|
+
attributes = input[tuple]
|
12
|
+
validator.call(attributes)
|
13
|
+
tuple = attributes.to_h
|
14
|
+
|
15
|
+
update(tuple)
|
16
|
+
end
|
17
|
+
|
18
|
+
def update(tuple)
|
19
|
+
dataset.scope.update(tuple).run(dataset.connection)
|
20
|
+
dataset.to_a
|
21
|
+
end
|
22
|
+
|
23
|
+
def dataset
|
24
|
+
relation.dataset
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ROM
|
2
|
+
module RethinkDB
|
3
|
+
# Dataset for RethinkDB
|
4
|
+
#
|
5
|
+
# @api public
|
6
|
+
class Dataset
|
7
|
+
attr_reader :scope, :rql, :connection
|
8
|
+
|
9
|
+
def initialize(scope, rql, connection)
|
10
|
+
@scope = scope
|
11
|
+
@rql = rql
|
12
|
+
@connection = connection
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_a
|
16
|
+
scope.run(connection)
|
17
|
+
end
|
18
|
+
|
19
|
+
def each(&block)
|
20
|
+
to_a.each(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def count
|
24
|
+
scope.count.run(connection)
|
25
|
+
end
|
26
|
+
|
27
|
+
[:filter, :pluck, :order_by].each do |method_name|
|
28
|
+
define_method(method_name) do |*args, &block|
|
29
|
+
self.class.new(scope.send(method_name, *args, &block), rql,
|
30
|
+
connection)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rom/rethinkdb/dataset'
|
2
|
+
require 'rom/rethinkdb/commands'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module RethinkDB
|
6
|
+
class Gateway < ROM::Gateway
|
7
|
+
# RethinkDB gateway interface
|
8
|
+
#
|
9
|
+
# @overload connect(options)
|
10
|
+
# Connects to database passing options
|
11
|
+
#
|
12
|
+
# @param [Hash] options connection options
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# gateway = ROM::RethinkDB::Gateway.new({ host: 'localhost',
|
16
|
+
# port: 28015, db: 'database' })
|
17
|
+
#
|
18
|
+
# @api public
|
19
|
+
def initialize(options)
|
20
|
+
@datasets = {}
|
21
|
+
@options = options
|
22
|
+
@rql = ::RethinkDB::RQL.new
|
23
|
+
@connection = rql.connect(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Return dataset with the given name
|
27
|
+
#
|
28
|
+
# @param [String] name a dataset name
|
29
|
+
#
|
30
|
+
# @return [Dataset]
|
31
|
+
#
|
32
|
+
# @api public
|
33
|
+
def dataset(name)
|
34
|
+
rql.db(options[:db]).table(name.to_s).run(connection)
|
35
|
+
datasets[name] = Dataset.new(rql.table(name.to_s), rql, connection)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Return dataset with the given name
|
39
|
+
#
|
40
|
+
# @param [String] name dataset name
|
41
|
+
#
|
42
|
+
# @return [Dataset]
|
43
|
+
#
|
44
|
+
# @api public
|
45
|
+
def [](name)
|
46
|
+
datasets[name.to_s]
|
47
|
+
end
|
48
|
+
|
49
|
+
# Check if dataset exists
|
50
|
+
#
|
51
|
+
# @param [String] name dataset name
|
52
|
+
#
|
53
|
+
# @api public
|
54
|
+
def dataset?(name)
|
55
|
+
rql.db(options[:db]).table(name.to_s).run(connection)
|
56
|
+
true
|
57
|
+
rescue ::RethinkDB::RqlRuntimeError
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
61
|
+
# Disconnect from database
|
62
|
+
#
|
63
|
+
# @api public
|
64
|
+
def disconnect
|
65
|
+
connection.close
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# @api private
|
71
|
+
attr_reader :datasets, :options, :rql
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ROM
|
2
|
+
module RethinkDB
|
3
|
+
# Relation subclass of RethinkDB adapter
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# class Users < ROM::Relation[:rethinkdb]
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# @api public
|
10
|
+
class Relation < ROM::Relation
|
11
|
+
adapter :rethinkdb
|
12
|
+
|
13
|
+
def rql
|
14
|
+
dataset.rql
|
15
|
+
end
|
16
|
+
|
17
|
+
def count
|
18
|
+
dataset.count
|
19
|
+
end
|
20
|
+
|
21
|
+
def filter(*args, &block)
|
22
|
+
__new__(dataset.__send__(__method__, *args, &block))
|
23
|
+
end
|
24
|
+
|
25
|
+
def pluck(*args, &block)
|
26
|
+
__new__(dataset.__send__(__method__, *args, &block))
|
27
|
+
end
|
28
|
+
|
29
|
+
def order_by(*args, &block)
|
30
|
+
__new__(dataset.__send__(__method__, *args, &block))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rom/rethinkdb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rom-rethinkdb'
|
8
|
+
spec.version = ROM::RethinkDB::VERSION
|
9
|
+
spec.authors = ['Krzysztof Wawer']
|
10
|
+
spec.email = ['krzysztof.wawer@gmail.com']
|
11
|
+
spec.summary = 'RethinkDB support for ROM'
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = 'http://rom-rb.org'
|
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_runtime_dependency 'rethinkdb'
|
22
|
+
spec.add_runtime_dependency 'rom', '~> 0.9', '>= 0.9.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe 'Commands / Create' do
|
5
|
+
subject(:rom) { setup.finalize }
|
6
|
+
|
7
|
+
# If :rethinkdb is not passed in the gateway is named `:default`
|
8
|
+
let(:setup) { ROM.setup(:rethinkdb, db_options.merge(db: 'test_db')) }
|
9
|
+
|
10
|
+
subject(:users) { rom.commands.users }
|
11
|
+
|
12
|
+
before do
|
13
|
+
create_table('test_db', 'users')
|
14
|
+
|
15
|
+
setup.relation(:users)
|
16
|
+
|
17
|
+
class User
|
18
|
+
include Virtus.model
|
19
|
+
|
20
|
+
attribute :id, Integer
|
21
|
+
attribute :name, String
|
22
|
+
attribute :street, String
|
23
|
+
end
|
24
|
+
|
25
|
+
setup.mappers do
|
26
|
+
define(:users) do
|
27
|
+
model User
|
28
|
+
register_as :entity
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
setup.commands(:users) do
|
33
|
+
define(:create) do
|
34
|
+
result :one
|
35
|
+
end
|
36
|
+
|
37
|
+
define(:create_many, type: :create) do
|
38
|
+
result :many
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
after do
|
44
|
+
drop_table('test_db', 'users')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns a single tuple when result is set to :one' do
|
48
|
+
result = users.try do
|
49
|
+
users.create.call('name' => 'John', 'street' => 'Main Street')
|
50
|
+
end
|
51
|
+
result = result.value
|
52
|
+
result.delete("id")
|
53
|
+
expect(result).to eql('name' => 'John', 'street' => 'Main Street')
|
54
|
+
|
55
|
+
result = rom.relation(:users).as(:entity).to_a
|
56
|
+
expect(result.count).to eql(1)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns tuples when result is set to :many' do
|
60
|
+
result = users.try do
|
61
|
+
users.create_many.call([
|
62
|
+
{ 'name' => 'Jane', 'street' => 'Main Street' },
|
63
|
+
{ 'name' => 'Jack', 'street' => 'Main Street' }
|
64
|
+
])
|
65
|
+
end
|
66
|
+
result = result.value.to_a
|
67
|
+
result.each_with_index { |_, index| result[index].delete('id') }
|
68
|
+
|
69
|
+
expect(result).to match_array([
|
70
|
+
{ 'name' => 'Jane', 'street' => 'Main Street' },
|
71
|
+
{ 'name' => 'Jack', 'street' => 'Main Street' }
|
72
|
+
])
|
73
|
+
|
74
|
+
result = rom.relation(:users).as(:entity).to_a
|
75
|
+
expect(result.count).to eql(2)
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Commands / Delete' do
|
4
|
+
subject(:rom) { setup.finalize }
|
5
|
+
|
6
|
+
# If :rethinkdb is not passed in the gateway is named `:default`
|
7
|
+
let(:setup) { ROM.setup(:rethinkdb, db_options.merge(db: 'test_db')) }
|
8
|
+
|
9
|
+
subject(:users) { rom.commands.users }
|
10
|
+
let(:gateway) { rom.gateways[:default] }
|
11
|
+
|
12
|
+
before do
|
13
|
+
create_table('test_db', 'users')
|
14
|
+
|
15
|
+
setup.relation(:users) do
|
16
|
+
def by_id(id)
|
17
|
+
filter(id: id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
setup.commands(:users) do
|
22
|
+
define(:delete) do
|
23
|
+
result :one
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# fill table
|
28
|
+
[
|
29
|
+
{ name: 'John', street: 'Main Street' }
|
30
|
+
].each do |data|
|
31
|
+
gateway.send(:rql).table('users').insert(data)
|
32
|
+
.run(gateway.connection)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
drop_table('test_db', 'users')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'deletes all tuples in a restricted relation' do
|
41
|
+
element = rom.relation(:users).to_a.first
|
42
|
+
|
43
|
+
result = users.try { users.delete.by_id(element['id']).call }
|
44
|
+
result = result.value
|
45
|
+
result.delete('id')
|
46
|
+
|
47
|
+
expect(result).to eql('name' => 'John', 'street' => 'Main Street')
|
48
|
+
|
49
|
+
result = rom.relation(:users).to_a
|
50
|
+
expect(result.count).to eql(0)
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe 'Commands / Updates' do
|
5
|
+
subject(:rom) { setup.finalize }
|
6
|
+
|
7
|
+
# If :rethinkdb is not passed in the gateway is named `:default`
|
8
|
+
let(:setup) { ROM.setup(:rethinkdb, db_options.merge(db: 'test_db')) }
|
9
|
+
|
10
|
+
subject(:users) { rom.commands.users }
|
11
|
+
let(:gateway) { rom.gateways[:default] }
|
12
|
+
|
13
|
+
before do
|
14
|
+
create_table('test_db', 'users')
|
15
|
+
|
16
|
+
setup.relation(:users) do
|
17
|
+
def by_id(id)
|
18
|
+
filter(id: id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class User
|
23
|
+
include Virtus.model
|
24
|
+
|
25
|
+
attribute :id, Integer
|
26
|
+
attribute :name, String
|
27
|
+
attribute :street, String
|
28
|
+
end
|
29
|
+
|
30
|
+
setup.mappers do
|
31
|
+
define(:users) do
|
32
|
+
model User
|
33
|
+
register_as :entity
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
setup.commands(:users) do
|
38
|
+
define(:update)
|
39
|
+
end
|
40
|
+
|
41
|
+
# fill table
|
42
|
+
[
|
43
|
+
{ name: 'John', street: 'Main Street' }
|
44
|
+
].each do |data|
|
45
|
+
gateway.send(:rql).table('users').insert(data)
|
46
|
+
.run(gateway.connection)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
after do
|
51
|
+
drop_table('test_db', 'users')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'updates everything when there is no original tuple' do
|
55
|
+
element = rom.relation(:users).as(:entity).to_a.first
|
56
|
+
expect(element.name).to eql('John')
|
57
|
+
expect(element.street).to eql('Main Street')
|
58
|
+
|
59
|
+
result = users.try do
|
60
|
+
users.update.by_id(element.id).call(street: '2nd Street')
|
61
|
+
end
|
62
|
+
result = result.value.to_a
|
63
|
+
result.each_with_index { |_, index| result[index].delete('id') }
|
64
|
+
|
65
|
+
expect(result)
|
66
|
+
.to match_array([{ 'name' => 'John', 'street' => '2nd Street' }])
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'virtus'
|
4
|
+
|
5
|
+
describe 'RethinkDB gateway' do
|
6
|
+
include PrepareDB
|
7
|
+
|
8
|
+
let(:gateway) { rom.gateways[:default] }
|
9
|
+
let(:setup) { ROM.setup(:rethinkdb, db_options.merge(db: 'test_db')) }
|
10
|
+
subject(:rom) { setup.finalize }
|
11
|
+
|
12
|
+
before do
|
13
|
+
create_table('test_db', 'users')
|
14
|
+
|
15
|
+
setup.relation(:users) do
|
16
|
+
def with_name(name)
|
17
|
+
filter(name: name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def only_john_and_jane
|
21
|
+
filter { |user| rql.expr(%w(John Jane)).contains(user["name"]) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def only_names
|
25
|
+
pluck('name')
|
26
|
+
end
|
27
|
+
|
28
|
+
def by_name
|
29
|
+
order_by('name')
|
30
|
+
end
|
31
|
+
|
32
|
+
def names_on_street(street)
|
33
|
+
filter(street: street).order_by('name').pluck(:name)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class User
|
38
|
+
include Virtus.value_object
|
39
|
+
|
40
|
+
values do
|
41
|
+
attribute :id, Integer
|
42
|
+
attribute :name, String
|
43
|
+
attribute :street, String
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
setup.mappers do
|
48
|
+
define(:users) do
|
49
|
+
model(User)
|
50
|
+
register_as(:entity)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# fill table
|
55
|
+
[
|
56
|
+
{ name: 'John', street: 'Main Street' },
|
57
|
+
{ name: 'Joe', street: '2nd Street' },
|
58
|
+
{ name: 'Jane', street: 'Main Street' }
|
59
|
+
].each do |data|
|
60
|
+
gateway.send(:rql).table('users').insert(data)
|
61
|
+
.run(gateway.connection)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
after do
|
66
|
+
drop_table('test_db', 'users')
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'env#relation' do
|
70
|
+
it 'returns mapped object' do
|
71
|
+
jane = rom.relation(:users).as(:entity).with_name('Jane').to_a.first
|
72
|
+
|
73
|
+
expect(jane.name).to eql('Jane')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'returns John and Jane objects' do
|
77
|
+
users = rom.relation(:users).as(:entity).only_john_and_jane.to_a
|
78
|
+
|
79
|
+
expect(users.count).to eql(2)
|
80
|
+
expect(users.map(&:name).sort).to eql(%w(Jane John))
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'returns specified fields' do
|
84
|
+
user = rom.relation(:users).as(:entity).only_names.to_a.first
|
85
|
+
|
86
|
+
expect(user.id).to be_nil
|
87
|
+
expect(user.name).not_to be_nil
|
88
|
+
expect(user.street).to be_nil
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'returns ordered data' do
|
92
|
+
results = rom.relation(:users).as(:entity).by_name.to_a
|
93
|
+
|
94
|
+
expect(results[0].name).to eql('Jane')
|
95
|
+
expect(results[1].name).to eql('Joe')
|
96
|
+
expect(results[2].name).to eql('John')
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'returns data with combined conditions' do
|
100
|
+
results = rom.relation(:users).as(:entity).names_on_street('Main Street')
|
101
|
+
.to_a
|
102
|
+
|
103
|
+
expect(results[0].id).to be_nil
|
104
|
+
expect(results[0].name).to eql('Jane')
|
105
|
+
expect(results[0].street).to be_nil
|
106
|
+
|
107
|
+
expect(results[1].id).to be_nil
|
108
|
+
expect(results[1].name).to eql('John')
|
109
|
+
expect(results[1].street).to be_nil
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'gateway#dataset?' do
|
114
|
+
it 'returns true if a collection exists' do
|
115
|
+
expect(gateway.dataset?(:users)).to be(true)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'returns false if a does not collection exist' do
|
119
|
+
expect(gateway.dataset?(:not_here)).to be(false)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler.require
|
5
|
+
|
6
|
+
if RUBY_ENGINE == 'rbx'
|
7
|
+
require 'codeclimate-test-reporter'
|
8
|
+
CodeClimate::TestReporter.start
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rom-rethinkdb'
|
12
|
+
|
13
|
+
require_relative 'support/prepare_db'
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.include PrepareDB
|
17
|
+
|
18
|
+
config.before(:all) do
|
19
|
+
create_database('test_db')
|
20
|
+
end
|
21
|
+
|
22
|
+
config.after(:all) do
|
23
|
+
drop_database('test_db')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ROM.use :auto_registration
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module PrepareDB
|
2
|
+
def create_database(database)
|
3
|
+
drop_database(database)
|
4
|
+
|
5
|
+
rql.db_create(database).run(connection)
|
6
|
+
end
|
7
|
+
|
8
|
+
def drop_database(database)
|
9
|
+
database_exist?(database) && rql.db_drop(database).run(connection)
|
10
|
+
end
|
11
|
+
|
12
|
+
def database_exist?(database)
|
13
|
+
database_list.include?(database.to_s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def database_list
|
17
|
+
rql.db_list.run(connection)
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_table(database, table)
|
21
|
+
drop_table(database, table)
|
22
|
+
rql.db(database).table_create(table).run(connection)
|
23
|
+
end
|
24
|
+
|
25
|
+
def drop_table(database, table)
|
26
|
+
if table_exist?(database, table)
|
27
|
+
rql.db(database).table_drop(table).run(connection)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def table_exist?(database, table)
|
32
|
+
table_list(database).include?(table.to_s)
|
33
|
+
end
|
34
|
+
|
35
|
+
def table_list(database)
|
36
|
+
rql.db(database).table_list.run(connection)
|
37
|
+
end
|
38
|
+
|
39
|
+
def rql
|
40
|
+
@rql ||= ::RethinkDB::RQL.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def connection
|
44
|
+
@connection ||= rql.connect(db_options)
|
45
|
+
end
|
46
|
+
|
47
|
+
def db_options
|
48
|
+
{ host: 'localhost', port: '28015' }
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'rom/lint/spec'
|
4
|
+
|
5
|
+
describe ROM::RethinkDB::Gateway do
|
6
|
+
let(:gateway) { ROM::RethinkDB::Gateway }
|
7
|
+
let(:uri) { { host: 'localhost', port: '28015', db: 'database' } }
|
8
|
+
|
9
|
+
it_behaves_like "a rom gateway" do
|
10
|
+
let(:identifier) { :rethinkdb }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '.new' do
|
14
|
+
context 'default values' do
|
15
|
+
let(:connection) { gateway.new(uri).connection }
|
16
|
+
|
17
|
+
it 'returns them' do
|
18
|
+
expect(connection.default_db).to eql('database')
|
19
|
+
expect(connection.host).to eql('localhost')
|
20
|
+
expect(connection.port).to eql(28_015)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rom-rethinkdb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Wawer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rethinkdb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rom
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 0.9.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0.9'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.9.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
description: RethinkDB support for ROM
|
76
|
+
email:
|
77
|
+
- krzysztof.wawer@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".gitignore"
|
83
|
+
- ".rspec"
|
84
|
+
- ".rubocop.yml"
|
85
|
+
- ".rubocop_todo.yml"
|
86
|
+
- ".travis.yml"
|
87
|
+
- CHANGELOG.md
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- lib/rom-rethinkdb.rb
|
93
|
+
- lib/rom/rethinkdb.rb
|
94
|
+
- lib/rom/rethinkdb/commands.rb
|
95
|
+
- lib/rom/rethinkdb/commands/create.rb
|
96
|
+
- lib/rom/rethinkdb/commands/delete.rb
|
97
|
+
- lib/rom/rethinkdb/commands/update.rb
|
98
|
+
- lib/rom/rethinkdb/dataset.rb
|
99
|
+
- lib/rom/rethinkdb/gateway.rb
|
100
|
+
- lib/rom/rethinkdb/relation.rb
|
101
|
+
- lib/rom/rethinkdb/version.rb
|
102
|
+
- rom-rethinkdb.gemspec
|
103
|
+
- spec/integration/commands/create_spec.rb
|
104
|
+
- spec/integration/commands/delete_spec.rb
|
105
|
+
- spec/integration/commands/update_spec.rb
|
106
|
+
- spec/integration/repository_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
- spec/support/prepare_db.rb
|
109
|
+
- spec/unit/gateway_spec.rb
|
110
|
+
homepage: http://rom-rb.org
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.4.5
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: RethinkDB support for ROM
|
134
|
+
test_files:
|
135
|
+
- spec/integration/commands/create_spec.rb
|
136
|
+
- spec/integration/commands/delete_spec.rb
|
137
|
+
- spec/integration/commands/update_spec.rb
|
138
|
+
- spec/integration/repository_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/support/prepare_db.rb
|
141
|
+
- spec/unit/gateway_spec.rb
|