Skip to content

Commit

Permalink
Merge pull request #20 from rom-rb/rom-4
Browse files Browse the repository at this point in the history
Upgrade to rom 4.0
  • Loading branch information
solnic authored Jun 30, 2017
2 parents f82991a + 33f887d commit 86effa6
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 104 deletions.
9 changes: 7 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ source 'https://rubygems.org'

gemspec

gem 'rom', git: 'https://github.com/rom-rb/rom.git', branch: 'master'
gem 'rom-repository', git: 'https://github.com/rom-rb/rom-repository.git', branch: 'master'
gem 'dry-types', git: 'https://github.com/dry-rb/dry-types', branch: 'master'

gem 'rom', git: 'https://github.com/rom-rb/rom', branch: 'master' do
gem 'rom-core'
gem 'rom-mapper'
gem 'rom-repository', group: :tools
end

group :test do
gem 'inflecto'
Expand Down
2 changes: 1 addition & 1 deletion lib/rom/mongo.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rom'
require 'rom/core'

require 'rom/mongo/version'
require 'rom/mongo/relation'
Expand Down
5 changes: 5 additions & 0 deletions lib/rom/mongo/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def each
view.each { |doc| yield(doc) }
end

# @api private
def map(&block)
to_a.map(&block)
end

def insert(data)
collection.insert_one(data)
end
Expand Down
32 changes: 22 additions & 10 deletions lib/rom/mongo/relation.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
require 'rom/plugins/relation/key_inference'
require 'rom/relation'
require 'rom/mongo/struct'
require 'rom/mongo/schema'

module ROM
module Mongo
class Relation < ROM::Relation
# @api private
def self.inherited(klass)
super
adapter :mongo

klass.auto_curry :by_pk
end
struct_namespace ROM::Mongo

adapter :mongo
schema_class Mongo::Schema

forward :insert, :find, :skip, :limit, :where, :order

use :key_inference
# @api private
def self.view_methods
super + [:by_pk]
end

forward :insert, :find, :only, :without, :skip, :limit, :where, :order
# @api public
def only(*fields)
schema.project(*fields).(self)
end

# @api public
def without(*fields)
schema.project(*(schema.map(&:name) - fields)).(self)
end

# @!method by_pk(id)
# Return a relation restricted by _id
Expand All @@ -24,7 +36,7 @@ def self.inherited(klass)
# @return [Mongo::Relation]
#
# @api public
def by_pk(id)
auto_curry def by_pk(id)
find(_id: id)
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/rom/mongo/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rom/schema'

module ROM
module Mongo
class Schema < ROM::Schema
# @api public
def call(relation)
relation.new(relation.dataset.only(map(&:name)), schema: self)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/rom/mongo/struct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rom/struct'

module ROM
module Mongo
class Struct < ROM::Struct
constructor_type :symbolized
end
end
end
2 changes: 1 addition & 1 deletion rom-mongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "rom", "~> 3.2"
spec.add_runtime_dependency "rom-core", "~> 4.0.0.beta1"
spec.add_runtime_dependency "mongo", "~> 2.2"
spec.add_runtime_dependency "origin"

Expand Down
36 changes: 17 additions & 19 deletions spec/integration/gateway_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
require 'spec_helper'
require 'rom-repository'
require 'rom/repository'

RSpec.describe 'Mongo gateway' do
include_context 'database'
include_context 'users'

let(:gateway) { container.gateways[:default] }

describe 'env#relation' do
describe 'auto-struct' do
it 'returns mapped object' do
jane = users.as(:model).by_name('Jane').one!
jane = users.by_name('Jane').one!

expect(jane.id)
.to eql(container.relation(:users) { |r| r.find(name: 'Jane') }.one['_id'].to_s)
expect(jane._id.to_s).to eql(users.find(name: 'Jane').one._id.to_s)

expect(jane.name).to eql('Jane')
expect(jane.email).to eql('jane@doe.org')
Expand All @@ -30,7 +28,7 @@
jane = repo.users.by_name('Jane').one!

expect(jane._id.to_s)
.to eql(container.relation(:users) { |r| r.find(name: 'Jane') }.one['_id'].to_s)
.to eql(users.find(name: 'Jane').one['_id'].to_s)

expect(jane.name).to eql('Jane')
expect(jane.email).to eql('jane@doe.org')
Expand All @@ -39,7 +37,7 @@
it 'uses #by_pk for update commands' do
repo.update(jane_id, name: 'Jane Doe')

expect(repo.users.by_pk(jane_id).one!.name).to eql('Jane Doe')
expect(users.by_pk(jane_id).one!.name).to eql('Jane Doe')
end
end

Expand All @@ -54,7 +52,7 @@
end

describe 'commands' do
let(:commands) { container.command(:users) }
let(:commands) { container.commands[:users] }

describe 'create' do
it 'inserts a document into collection' do
Expand All @@ -71,14 +69,14 @@

describe 'update' do
it 'updates a document in the collection' do
jane = container.relation(:users).as(:model).by_name('Jane').one!
jane = users.by_name('Jane').one!

result = commands.try do
commands.update.by_name('Jane').call(email: 'jane.doe@test.com')
end

expect(result).to match_array(
[{ '_id' => BSON::ObjectId.from_string(jane.id),
[{ '_id' => BSON::ObjectId.from_string(jane._id),
'name' => 'Jane',
'email' => 'jane.doe@test.com' }]
)
Expand All @@ -87,18 +85,18 @@

describe 'delete' do
it 'deletes documents from the collection' do
jane = container.relation(:users).as(:model).by_name('Jane').one!
joe = container.relation(:users).as(:model).by_name('Joe').one!
jane = users.by_name('Jane').one!
joe = users.by_name('Joe').one!

result = commands.try { commands.delete.by_name('Joe') }
result = commands.delete.by_name('Joe').call

expect(result).to match_array(
[{ '_id' => BSON::ObjectId.from_string(joe.id),
'name' => 'Joe',
'email' => 'a.joe@doe.org' }]
expect(result.map(&:to_h)).to match_array(
[{ _id: BSON::ObjectId.from_string(joe._id),
name: 'Joe',
email: 'a.joe@doe.org' }]
)

expect(container.relation(:users).as(:model).all).to match_array([jane])
expect(users.all).to match_array([jane])
end
end
end
Expand Down
35 changes: 0 additions & 35 deletions spec/integration/inference_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/shared/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
let(:configuration) { ROM::Configuration.new(:mongo, connection) }
let(:connection) { ::Mongo::Client.new(MONGO_URI) }

let(:users) { container.relation(:users) }
let(:users) { container.relations[:users].with(auto_struct: true) }
let(:jane_id) { BSON::ObjectId.new }
end
26 changes: 5 additions & 21 deletions spec/shared/users.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require 'dry-struct'

RSpec.shared_context 'users' do
let(:users) { container.relation(:users) }
let(:users) { container.relations[:users] }
let(:jane_id) { BSON::ObjectId.new }

before do
connection[:users].drop

configuration.relation(:users) do
auto_struct true

schema do
# TODO: we need ROM::Mongo::Types (similar to ROM::SQL::Types)
attribute :_id, ROM::Types.Definition(BSON::ObjectId)
Expand All @@ -30,25 +32,7 @@ def all
define(:delete)
end

user_model = Class.new(Dry::Struct) do
attribute :id, 'coercible.string'
attribute :name, 'strict.string'
attribute :email, 'strict.string'
end

configuration.mappers do
define(:users) do
model(user_model)

register_as :model

attribute :id, from: '_id'
attribute :name, from: 'name'
attribute :email, from: 'email'
end
end

container.relations.users.insert(_id: jane_id, name: 'Jane', email: 'jane@doe.org')
container.relations.users.insert(name: 'Joe', email: 'a.joe@doe.org')
users.insert(_id: jane_id, name: 'Jane', email: 'jane@doe.org')
users.insert(name: 'Joe', email: 'a.joe@doe.org')
end
end
26 changes: 12 additions & 14 deletions spec/unit/relation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
require 'rom/mongo/relation'

RSpec.describe ROM::Mongo::Relation do
include_context 'database'
include_context 'users'

describe '#by_pk' do
it 'fetches a document by _id' do
expect(users.by_pk(jane_id).one!).
to eql(
'_id' => jane_id,
'name' => 'Jane',
'email' => 'jane@doe.org'
)
expect(users.by_pk(jane_id).one!.to_h).
to eql(_id: jane_id, name: 'Jane', email: 'jane@doe.org')
end
end

describe '#order' do
it 'sorts documents' do
expect(users.order(name: :asc).only(:name).without(:_id).to_a).
to eql([{'name' => 'Jane',}, {'name' => 'Joe'}])
expect(users.order(name: :asc).only(:name).without(:_id).to_a.map(&:to_h)).
to eql([{name: 'Jane',}, {name: 'Joe'}])

expect(users.order(name: :desc).only(:name).without(:_id).to_a).
to eql([{'name' => 'Joe',}, {'name' => 'Jane'}])
expect(users.order(name: :desc).only(:name).without(:_id).to_a.map(&:to_h)).
to eql([{name: 'Joe'}, {name: 'Jane'}])
end

it 'supports mutli-field sorting' do
expect(users.order(name: :asc, email: :asc).only(:name).without(:_id).to_a).
to eql([{'name' => 'Jane',}, {'name' => 'Joe'}])
expect(users.order(name: :asc, email: :asc).only(:name).without(:_id).to_a.map(&:to_h)).
to eql([{name: 'Jane',}, {name: 'Joe'}])

expect(users.order(email: :asc, name: :asc).only(:name).without(:_id).to_a).
to eql([{'name' => 'Joe',}, {'name' => 'Jane'}])
expect(users.order(email: :asc, name: :asc).only(:name).without(:_id).to_a.map(&:to_h)).
to eql([{name: 'Joe',}, {name: 'Jane'}])
end
end
end

0 comments on commit 86effa6

Please sign in to comment.