Skip to content
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
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ before_script:
- sudo /etc/init.d/rethinkdb restart
script: "bundle exec rake spec"
rvm:
- 2.1.10
- 2.2.5
- 2.3.1
- 2.5.0
- 2.4.2
- 2.3.6
- rbx
- jruby-9000
- jruby-9.1.13.0
matrix:
allow_failures:
- rvm: rbx
- rvm: jruby-9000
- rvm: jruby-9.1.13.0
notifications:
webhooks:
urls:
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
source 'https://rubygems.org'
git_source :github do |repo|
"https://github.com/#{repo}.git"
end

gemspec

gem 'rom', github: 'rom-rb/rom', branch: 'master'

group :test do
gem 'virtus'
gem 'rspec', '~> 3.1'
gem 'codeclimate-test-reporter', require: false
end
Expand Down
1 change: 0 additions & 1 deletion lib/rom/rethinkdb/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Create < ROM::Commands::Create
def execute(tuples)
insert_tuples = [tuples].flatten.map do |tuple|
attributes = input[tuple]
validator.call(attributes)
attributes.to_h
end

Expand Down
8 changes: 2 additions & 6 deletions lib/rom/rethinkdb/commands/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ class Delete < ROM::Commands::Delete
adapter :rethinkdb

def execute
deleted = dataset.to_a
dataset.delete
deleted = relation.to_a
source.dataset.delete
deleted
end

def dataset
relation.dataset
end
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/rom/rethinkdb/commands/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Update < ROM::Commands::Update
adapter :rethinkdb

def execute(tuple)
attributes = input[tuple]
validator.call(attributes)
tuple = attributes.to_h
tuple = input[tuple].to_h

update(tuple)
end
Expand Down
13 changes: 8 additions & 5 deletions lib/rom/rethinkdb/dataset.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
require_relative 'functions'

module ROM
module RethinkDB
# Dataset for RethinkDB
#
# @api public
class Dataset
extend Forwardable
include Enumerable

attr_reader :scope, :rql, :gateway

def initialize(scope, rql, gateway)
Expand All @@ -13,12 +18,10 @@ def initialize(scope, rql, gateway)
end

def to_a
gateway.run(scope).to_a
Functions.symbolize_hashes(Array(gateway.run(scope).to_a))
end

def each(&block)
to_a.each(&block)
end
def_instance_delegators :to_a, :each, :first, :last

def insert(tuples)
gateway.run(scope.insert(tuples))
Expand All @@ -36,7 +39,7 @@ def count
gateway.run(scope.count)
end

[:filter, :pluck, :order_by].each do |method_name|
%i[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,
gateway)
Expand Down
16 changes: 16 additions & 0 deletions lib/rom/rethinkdb/functions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'transproc'

module ROM
module RethinkDB
module Functions
extend Transproc::Registry

import Transproc::ArrayTransformations
import Transproc::HashTransformations

def self.symbolize_hashes(input)
self[:map_array, self[:symbolize_keys]].(input)
end
end
end
end
17 changes: 5 additions & 12 deletions lib/rom/rethinkdb/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
require 'rom/rethinkdb/commands'
require 'connection_pool'
require 'thread'
require 'dry/core/cache'

module ROM
module RethinkDB
class DatasetCache
def initialize
@mutex = Mutex.new
@datasets = {}
end

def fetch(name)
@mutex.synchronize{ @datasets.fetch(name.to_s) { |k| @datasets[k] = yield(k) } }
end
end
class Gateway < ROM::Gateway
extend Dry::Core::Cache
adapter :rethinkdb

# RethinkDB gateway interface
#
# @overload connect(options)
Expand All @@ -29,7 +23,6 @@ class Gateway < ROM::Gateway
#
# @api public
def initialize(options)
@datasets = DatasetCache.new
@options = options
@rql = ::RethinkDB::RQL.new
pool_size = options.fetch(:pool_size, 5).to_i
Expand All @@ -45,7 +38,7 @@ def initialize(options)
#
# @api public
def dataset(name)
@datasets.fetch(name) { |n| Dataset.new(rql.table(n), rql, self) }
fetch_or_store(name) { Dataset.new(rql.table(name), rql, self) }
end

alias :[] :dataset
Expand Down
22 changes: 6 additions & 16 deletions lib/rom/rethinkdb/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@ module RethinkDB
#
# @api public
class Relation < ROM::Relation
adapter :rethinkdb

def rql
dataset.rql
end
extend Forwardable

def count
dataset.count
end
adapter :rethinkdb

def filter(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
end
forward :filter, :pluck, :order_by

def pluck(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
end
def_instance_delegators :dataset, :rql, :count

def order_by(*args, &block)
__new__(dataset.__send__(__method__, *args, &block))
def pluck(*names)
new(dataset.pluck(*names), schema: schema.project(*names))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rom-rethinkdb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rethinkdb'
spec.add_runtime_dependency 'rom', '~> 2.0.0'
spec.add_runtime_dependency 'rom', '~> 4.0'
spec.add_runtime_dependency 'connection_pool', '>= 0.2'

spec.add_development_dependency 'bundler'
Expand Down
50 changes: 17 additions & 33 deletions spec/integration/commands/create_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'virtus'

describe 'Commands / Create' do
include_context 'db setup'
Expand All @@ -9,20 +8,11 @@
before do
create_table('test_db', 'users') unless table_exist?('test_db', 'users')

configuration.relation(:users)

class User
include Virtus.model

attribute :id, Integer
attribute :name, String
attribute :street, String
end

configuration.mappers do
define(:users) do
model User
register_as :entity
configuration.relation(:users) do
schema do
attribute :id, ROM::Types::Integer
attribute :name, ROM::Types::String
attribute :street, ROM::Types::String
end
end

Expand All @@ -42,33 +32,27 @@ class User
end

it 'returns a single tuple when result is set to :one' do
result = users.try do
users.create.call('name' => 'John', 'street' => 'Main Street')
end
result = result.value
result.delete("id")
expect(result).to eql('name' => 'John', 'street' => 'Main Street')
result = users.create.call('name' => 'John', 'street' => 'Main Street')
result.delete(:id)
expect(result).to eql(name: 'John', street: 'Main Street')

result = container.relation(:users).as(:entity).to_a
result = container.relations[:users].to_a
expect(result.count).to eql(1)
end

it 'returns tuples when result is set to :many' do
result = users.try do
users.create_many.call([
{ 'name' => 'Jane', 'street' => 'Main Street' },
{ 'name' => 'Jack', 'street' => 'Main Street' }
])
end
result = result.value.to_a
result.each_with_index { |_, index| result[index].delete('id') }

expect(result).to match_array([
result = users.create_many.call([
{ 'name' => 'Jane', 'street' => 'Main Street' },
{ 'name' => 'Jack', 'street' => 'Main Street' }
])
result.each_with_index { |_, index| result[index].delete(:id) }

expect(result).to match_array([
{ name: 'Jane', street: 'Main Street' },
{ name: 'Jack', street: 'Main Street' }
])

result = container.relation(:users).as(:entity).to_a
result = container.relations[:users].to_a
expect(result.count).to eql(2)
end
end
11 changes: 5 additions & 6 deletions spec/integration/commands/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ def by_id(id)
end

it 'deletes all tuples in a restricted relation' do
element = container.relation(:users).to_a.first
element = container.relations[:users].to_a.first

result = users.try { users.delete.by_id(element['id']).call }
result = result.value
result.delete('id')
result = users.delete.by_id(element[:id]).call
result.delete(:id)

expect(result).to eql('name' => 'John', 'street' => 'Main Street')
expect(result).to eql(name: 'John', street: 'Main Street')

result = container.relation(:users).to_a
result = container.relations[:users].to_a
expect(result.count).to eql(0)
end
end
35 changes: 12 additions & 23 deletions spec/integration/commands/update_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'virtus'

describe 'Commands / Updates' do
include_context 'db setup'
Expand All @@ -10,23 +9,16 @@
create_table('test_db', 'users') unless table_exist?('test_db', 'users')

configuration.relation(:users) do
def by_id(id)
filter(id: id)
end
end

class User
include Virtus.model
auto_struct true

attribute :id, Integer
attribute :name, String
attribute :street, String
end
schema do
attribute :id, ROM::Types::Integer
attribute :name, ROM::Types::String
attribute :street, ROM::Types::String
end

configuration.mappers do
define(:users) do
model User
register_as :entity
def by_id(id)
filter(id: id)
end
end

Expand All @@ -43,17 +35,14 @@ class User
end

it 'updates everything when there is no original tuple' do
element = container.relation(:users).as(:entity).to_a.first
element = container.relations[:users].to_a.first
expect(element.name).to eql('John')
expect(element.street).to eql('Main Street')

result = users.try do
users.update.by_id(element.id).call(street: '2nd Street')
end
result = result.value.to_a
result.each_with_index { |_, index| result[index].delete('id') }
result = users.update.by_id(element.id).call(street: '2nd Street')
result.each_with_index { |_, index| result[index].delete(:id) }

expect(result)
.to match_array([{ 'name' => 'John', 'street' => '2nd Street' }])
.to match_array([{ name: 'John', street: '2nd Street' }])
end
end
Loading