Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced version of adapter #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by `rubocop --auto-gen-config`
inherit_from: .rubocop_todo.yml

# Its quite readable when we know what we are doing
# It's quite readable when we know what we are doing
Lint/AssignmentInCondition:
Enabled: false

Expand All @@ -10,7 +10,7 @@ Lint/HandleExceptions:
Exclude:
- Rakefile

# The enforced style doesnt match Vims defaults
# The enforced style doesn't match Vim's defaults
Style/AlignParameters:
Enabled: false

Expand Down Expand Up @@ -42,7 +42,7 @@ Style/MultilineBlockChain:
Style/RegexpLiteral:
MaxSlashes: 0

# Dont introduce semantic fail/raise distinction
# Don't introduce semantic fail/raise distinction
Style/SignalException:
EnforcedStyle: only_raise

Expand Down
17 changes: 11 additions & 6 deletions lib/rom/rethinkdb/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ class Create < ROM::Commands::Create
adapter :rethinkdb

def execute(tuples)
insert_tuples = [tuples].flatten.map do |tuple|
insert_tuples = [tuples].flatten.map do |tuple|
attributes = input[tuple]
validator.call(attributes)
attributes.to_h
end

insert(insert_tuples)
pks = insert(insert_tuples)
get_by_pks(pks)
end

def insert(tuples)
pks = dataset.scope.insert(tuples)
.run(dataset.connection)["generated_keys"]
dataset.scope.insert(tuples)
.run(dataset.connection)
.fetch('generated_keys')
end

dataset.filter { |user| relation.rql.expr(pks).contains(user["id"]) }
.to_a
def get_by_pks(pks)
dataset.filter do |user|
relation.rql.expr(pks).contains(user['id'])
end.to_a
end

def dataset
Expand Down
20 changes: 12 additions & 8 deletions lib/rom/rethinkdb/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ def initialize(scope, rql, connection)
end

def to_a
scope.run(connection)
wrap_array(scope.run(connection))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Array[scope.run(connection)] ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nepalez look at source of wrap_array, it calls to_a for scope.run

end

def each(&block)
to_a.each(&block)
end

def count
scope.count.run(connection)
def method_missing(command, *args, &block)
self.class.new(scope.public_send(command, *args, &block),
rql, connection)
end

[:filter, :pluck, :order_by].each do |method_name|
define_method(method_name) do |*args, &block|
self.class.new(scope.send(method_name, *args, &block), rql,
connection)
end
private

def wrap_array(object)
return [object] if object.is_a?(Hash)
return object if object.is_a?(Array)
return [] if object.nil?

object.to_a
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/rom/rethinkdb/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def initialize(options)
#
# @api public
def dataset(name)
rql.db(options[:db]).table(name.to_s).run(connection)
datasets[name] = Dataset.new(rql.table(name.to_s), rql, connection)
end

Expand All @@ -52,10 +51,7 @@ def [](name)
#
# @api public
def dataset?(name)
rql.db(options[:db]).table(name.to_s).run(connection)
true
rescue ::RethinkDB::RqlRuntimeError
false
rql.db(options[:db]).table_list.run(connection).include?(name.to_s)
end

# Disconnect from database
Expand Down
14 changes: 3 additions & 11 deletions lib/rom/rethinkdb/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ def rql
end

def count
dataset.count
dataset.to_a.count
end

def filter(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
end

def pluck(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
end

def order_by(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
def method_missing(command, *args, &block)
__new__(dataset.public_send(command, *args, &block))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/commands/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
subject(:users) { rom.commands.users }

before do
create_table('test_db', 'users')
clean_table('test_db', 'users')

setup.relation(:users)

Expand Down Expand Up @@ -41,7 +41,7 @@ class User
end

after do
drop_table('test_db', 'users')
clean_table('test_db', 'users')
end

it 'returns a single tuple when result is set to :one' do
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/commands/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:gateway) { rom.gateways[:default] }

before do
create_table('test_db', 'users')
clean_table('test_db', 'users')

setup.relation(:users) do
def by_id(id)
Expand All @@ -34,7 +34,7 @@ def by_id(id)
end

after do
drop_table('test_db', 'users')
clean_table('test_db', 'users')
end

it 'deletes all tuples in a restricted relation' do
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/commands/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:gateway) { rom.gateways[:default] }

before do
create_table('test_db', 'users')
clean_table('test_db', 'users')

setup.relation(:users) do
def by_id(id)
Expand Down Expand Up @@ -48,7 +48,7 @@ class User
end

after do
drop_table('test_db', 'users')
clean_table('test_db', 'users')
end

it 'updates everything when there is no original tuple' do
Expand Down
28 changes: 23 additions & 5 deletions spec/integration/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
require 'spec_helper'

require 'virtus'

describe 'RethinkDB gateway' do
include PrepareDB

let(:gateway) { rom.gateways[:default] }
let(:setup) { ROM.setup(:rethinkdb, db_options.merge(db: 'test_db')) }
subject(:rom) { setup.finalize }

before do
create_table('test_db', 'users')
clean_table('test_db', 'users')

setup.relation(:users) do
def by_id(id)
get(id)
end

def with_name(name)
filter(name: name)
end
Expand All @@ -32,6 +33,10 @@ def by_name
def names_on_street(street)
filter(street: street).order_by('name').pluck(:name)
end

def undefined
undfn(with: 'args')
end
end

class User
Expand Down Expand Up @@ -63,10 +68,23 @@ class User
end

after do
drop_table('test_db', 'users')
clean_table('test_db', 'users')
end

describe 'env#relation' do
it 'raise error on undefined method' do
expect {
rom.relation(:users).undefined.to_a
}.to raise_error(NoMethodError)
end

it 'return data by primary key' do
pk = rom.relation(:users).to_a.map { |o| o.fetch('id') }.first
user = rom.relation(:users).as(:entity).by_id(pk).one

expect(user.id).to eq(pk)
end

it 'returns mapped object' do
jane = rom.relation(:users).as(:entity).with_name('Jane').to_a.first

Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
require_relative 'support/prepare_db'

RSpec.configure do |config|
config.include PrepareDB
include PrepareDB

config.before(:all) do
config.before(:suite) do
create_database('test_db')
create_table('test_db', 'users')
end

config.after(:all) do
config.after(:suite) do
drop_database('test_db')
end
end
Expand Down
27 changes: 10 additions & 17 deletions spec/support/prepare_db.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
module PrepareDB
def create_database(database)
drop_database(database)

rql.db_create(database).run(connection)
end

def drop_database(database)
database_exist?(database) && rql.db_drop(database).run(connection)
end

def database_exist?(database)
database_list.include?(database.to_s)
end

def database_list
rql.db_list.run(connection)
rql.db_drop(database).run(connection)
end

def create_table(database, table)
drop_table(database, table)
rql.db(database).table_create(table).run(connection)
end

def drop_table(database, table)
if table_exist?(database, table)
rql.db(database).table_drop(table).run(connection)
clean_table(database, table)
else
rql.db(database).table_create(table).run(connection)
end
end

def clean_table(database, table)
rql.db(database).table(table).delete.run(connection)
rescue RethinkDB::RqlOpFailedError
nil
end

def table_exist?(database, table)
table_list(database).include?(table.to_s)
end
Expand Down