rom-csv 0.1.0 → 0.2.0
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 +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +10 -1
- data/examples/find_user.rb +3 -0
- data/lib/rom/csv.rb +1 -1
- data/lib/rom/csv/commands.rb +3 -0
- data/lib/rom/csv/commands/create.rb +40 -0
- data/lib/rom/csv/commands/delete.rb +32 -0
- data/lib/rom/csv/commands/update.rb +48 -0
- data/lib/rom/csv/dataset.rb +27 -0
- data/lib/rom/csv/{repository.rb → gateway.rb} +14 -7
- data/lib/rom/csv/relation.rb +6 -0
- data/lib/rom/csv/version.rb +1 -1
- data/rom-csv.gemspec +1 -1
- data/spec/fixtures/addresses.csv +5 -0
- data/spec/fixtures/users.csv +1 -1
- data/spec/integration/commands/create_spec.rb +72 -0
- data/spec/integration/commands/delete_spec.rb +48 -0
- data/spec/integration/commands/update_spec.rb +64 -0
- data/spec/integration/relation_spec.rb +35 -0
- data/spec/integration/repository_spec.rb +61 -7
- data/spec/spec_helper.rb +2 -0
- data/spec/unit/repository_spec.rb +3 -3
- metadata +26 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f29f9955ae5aa228873154052cb12ea961f582
|
4
|
+
data.tar.gz: 3979e8a009b8725a7a5280703e29bc89259ac512
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03fb8e82792ea55ed6b72ff437d4c8c1381a24e980f82b0fe32c7df16b9a0473ea8e87cdb907498abe3d703cf1289f746b09493e059e22427d413e73173fff35
|
7
|
+
data.tar.gz: 9d6f4ab54cef76ed4f27b8c28a142dd6999f905ac1cb2d4daceab3c9d7bb32ae502b435048d790072878f57f32f11b66402dc5e0dcaa10be91c2bfff5178e462
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## v0.2.0 2015-08-19
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Support for Create, Update and Delete commands (wafcio)
|
6
|
+
* Support for in-memory joins (wafcio)
|
7
|
+
|
8
|
+
[Compare v0.1.0...v0.2.0](https://github.com/rom-rb/rom-csv/compare/v0.1.0...v0.2.0)
|
9
|
+
|
1
10
|
## v0.1.0 2015-04-16
|
2
11
|
|
3
12
|
### Added
|
@@ -8,7 +17,7 @@
|
|
8
17
|
|
9
18
|
* Improve test coverage (wafcio)
|
10
19
|
|
11
|
-
[Compare v0.0.1...v0.1.0](https://github.com/rom-rb/rom/compare/v0.0.1...v0.1.0)
|
20
|
+
[Compare v0.0.1...v0.1.0](https://github.com/rom-rb/rom-csv/compare/v0.0.1...v0.1.0)
|
12
21
|
|
13
22
|
## v0.0.1 2015-03-22
|
14
23
|
|
data/examples/find_user.rb
CHANGED
data/lib/rom/csv.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/create'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module CSV
|
6
|
+
module Commands
|
7
|
+
class Create < ROM::Commands::Create
|
8
|
+
adapter :csv
|
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
|
+
insert_tuples
|
19
|
+
end
|
20
|
+
|
21
|
+
def insert(tuples)
|
22
|
+
tuples.each { |tuple| dataset << new_row(tuple) }
|
23
|
+
dataset.sync!
|
24
|
+
end
|
25
|
+
|
26
|
+
def new_row(tuple)
|
27
|
+
::CSV::Row.new(dataset.data.headers, ordered_data(tuple))
|
28
|
+
end
|
29
|
+
|
30
|
+
def ordered_data(tuple)
|
31
|
+
dataset.data.headers.map { |header| tuple[header] }
|
32
|
+
end
|
33
|
+
|
34
|
+
def dataset
|
35
|
+
relation.dataset
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/delete'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module CSV
|
6
|
+
module Commands
|
7
|
+
class Delete < ROM::Commands::Delete
|
8
|
+
adapter :csv
|
9
|
+
|
10
|
+
def execute
|
11
|
+
original_data = original_dataset.to_a
|
12
|
+
|
13
|
+
dataset.each do |dataset_tuple|
|
14
|
+
index = original_data.index(dataset_tuple)
|
15
|
+
original_dataset.data.delete(index)
|
16
|
+
end
|
17
|
+
|
18
|
+
original_dataset.sync!
|
19
|
+
dataset.data
|
20
|
+
end
|
21
|
+
|
22
|
+
def dataset
|
23
|
+
relation.dataset
|
24
|
+
end
|
25
|
+
|
26
|
+
def original_dataset
|
27
|
+
source.dataset
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rom/commands'
|
2
|
+
require 'rom/commands/update'
|
3
|
+
|
4
|
+
module ROM
|
5
|
+
module CSV
|
6
|
+
module Commands
|
7
|
+
class Update < ROM::Commands::Update
|
8
|
+
adapter :csv
|
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
|
+
original_data = original_dataset.to_a
|
20
|
+
output = []
|
21
|
+
|
22
|
+
dataset.each do |dataset_tuple|
|
23
|
+
index = original_data.index(dataset_tuple)
|
24
|
+
update_dataset(index, tuple)
|
25
|
+
output << original_dataset.data[index].to_hash
|
26
|
+
end
|
27
|
+
|
28
|
+
original_dataset.sync!
|
29
|
+
output
|
30
|
+
end
|
31
|
+
|
32
|
+
def update_dataset(index, tuple)
|
33
|
+
tuple.each do |key, value|
|
34
|
+
original_dataset.data[index][key] = value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def dataset
|
39
|
+
relation.dataset
|
40
|
+
end
|
41
|
+
|
42
|
+
def original_dataset
|
43
|
+
source.dataset
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/rom/csv/dataset.rb
CHANGED
@@ -6,12 +6,39 @@ module ROM
|
|
6
6
|
#
|
7
7
|
# @api public
|
8
8
|
class Dataset < ROM::Memory::Dataset
|
9
|
+
option :path, reader: true
|
10
|
+
option :file_options, reader: true
|
11
|
+
|
9
12
|
# Convert each CSV::Row to a hash
|
10
13
|
#
|
11
14
|
# @api public
|
12
15
|
def self.row_proc
|
13
16
|
-> row { row.to_hash }
|
14
17
|
end
|
18
|
+
|
19
|
+
def reload!
|
20
|
+
@data = load_data
|
21
|
+
end
|
22
|
+
|
23
|
+
def sync!
|
24
|
+
write_data && reload!
|
25
|
+
end
|
26
|
+
|
27
|
+
def write_data
|
28
|
+
::CSV.open(path, 'wb', file_options) do |csv|
|
29
|
+
data.to_a.each do |tuple|
|
30
|
+
csv << tuple
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def load_data
|
36
|
+
::CSV.table(path, file_options).by_row!
|
37
|
+
end
|
38
|
+
|
39
|
+
def count
|
40
|
+
data.count
|
41
|
+
end
|
15
42
|
end
|
16
43
|
end
|
17
44
|
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
require 'rom/
|
1
|
+
require 'rom/gateway'
|
2
2
|
require 'rom/csv/dataset'
|
3
|
+
require 'rom/csv/commands'
|
3
4
|
|
4
5
|
# Ruby Object Mapper
|
5
6
|
#
|
@@ -35,12 +36,12 @@ module ROM
|
|
35
36
|
#
|
36
37
|
# @api public
|
37
38
|
module CSV
|
38
|
-
# CSV
|
39
|
+
# CSV gateway interface
|
39
40
|
#
|
40
41
|
# @api public
|
41
|
-
class
|
42
|
+
class Gateway < ROM::Gateway
|
42
43
|
# Expect a path to a single csv file which will be registered by rom to
|
43
|
-
# the given name or :default as the
|
44
|
+
# the given name or :default as the gateway.
|
44
45
|
#
|
45
46
|
# Uses CSV.table which passes the following csv options:
|
46
47
|
# * headers: true
|
@@ -55,6 +56,8 @@ module ROM
|
|
55
56
|
# @see CSV.table
|
56
57
|
def initialize(path, options = {})
|
57
58
|
@datasets = {}
|
59
|
+
@path = path
|
60
|
+
@options = options
|
58
61
|
@connection = ::CSV.table(path, options).by_row!
|
59
62
|
end
|
60
63
|
|
@@ -68,7 +71,7 @@ module ROM
|
|
68
71
|
datasets[name]
|
69
72
|
end
|
70
73
|
|
71
|
-
# Register a dataset in the
|
74
|
+
# Register a dataset in the gateway
|
72
75
|
#
|
73
76
|
# If dataset already exists it will be returned
|
74
77
|
#
|
@@ -77,7 +80,7 @@ module ROM
|
|
77
80
|
#
|
78
81
|
# @api public
|
79
82
|
def dataset(name)
|
80
|
-
datasets[name] = Dataset.new(connection)
|
83
|
+
datasets[name] = Dataset.new(connection, dataset_options)
|
81
84
|
end
|
82
85
|
|
83
86
|
# Check if dataset exists
|
@@ -91,8 +94,12 @@ module ROM
|
|
91
94
|
|
92
95
|
private
|
93
96
|
|
97
|
+
def dataset_options
|
98
|
+
{ path: path, file_options: options }
|
99
|
+
end
|
100
|
+
|
94
101
|
# @api private
|
95
|
-
attr_reader :datasets
|
102
|
+
attr_reader :datasets, :path, :options
|
96
103
|
end
|
97
104
|
end
|
98
105
|
end
|
data/lib/rom/csv/relation.rb
CHANGED
data/lib/rom/csv/version.rb
CHANGED
data/rom-csv.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_runtime_dependency 'rom', '~> 0.
|
21
|
+
spec.add_runtime_dependency 'rom', '~> 0.9', '>= 0.9.0'
|
22
22
|
|
23
23
|
spec.add_development_dependency 'bundler'
|
24
24
|
spec.add_development_dependency 'rake'
|
data/spec/fixtures/users.csv
CHANGED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe 'Commands / Create' do
|
5
|
+
subject(:rom) { setup.finalize }
|
6
|
+
|
7
|
+
let(:original_path) { File.expand_path('./spec/fixtures/users.csv') }
|
8
|
+
let(:path) { File.expand_path('./spec/fixtures/testing.csv') }
|
9
|
+
|
10
|
+
# If :csv is not passed in the gateway is named `:default`
|
11
|
+
let(:setup) { ROM.setup(:csv, path) }
|
12
|
+
|
13
|
+
subject(:users) { rom.commands.users }
|
14
|
+
|
15
|
+
before do
|
16
|
+
FileUtils.copy(original_path, path)
|
17
|
+
|
18
|
+
setup.relation(:users)
|
19
|
+
|
20
|
+
class User
|
21
|
+
include Virtus.model
|
22
|
+
|
23
|
+
attribute :id, Integer
|
24
|
+
attribute :name, String
|
25
|
+
attribute :email, String
|
26
|
+
end
|
27
|
+
|
28
|
+
setup.mappers do
|
29
|
+
define(:users) do
|
30
|
+
model User
|
31
|
+
register_as :entity
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
setup.commands(:users) do
|
36
|
+
define(:create) do
|
37
|
+
result :one
|
38
|
+
end
|
39
|
+
|
40
|
+
define(:create_many, type: :create) do
|
41
|
+
result :many
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns a single tuple when result is set to :one' do
|
47
|
+
result = users.try do
|
48
|
+
users.create.call(user_id: 4, name: 'John', email: 'john@doe.org')
|
49
|
+
end
|
50
|
+
expect(result.value).to eql(user_id: 4, name: 'John', email: 'john@doe.org')
|
51
|
+
|
52
|
+
result = rom.relation(:users).as(:entity).to_a
|
53
|
+
expect(result.count).to eql(4)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns tuples when result is set to :many' do
|
57
|
+
result = users.try do
|
58
|
+
users.create_many.call([
|
59
|
+
{ user_id: 4, name: 'Jane', email: 'jane@doe.org' },
|
60
|
+
{ user_id: 5, name: 'Jack', email: 'jack@doe.org' }
|
61
|
+
])
|
62
|
+
end
|
63
|
+
|
64
|
+
expect(result.value.to_a).to match_array([
|
65
|
+
{ user_id: 4, name: 'Jane', email: 'jane@doe.org' },
|
66
|
+
{ user_id: 5, name: 'Jack', email: 'jack@doe.org' }
|
67
|
+
])
|
68
|
+
|
69
|
+
result = rom.relation(:users).as(:entity).to_a
|
70
|
+
expect(result.count).to eql(5)
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Commands / Delete' do
|
4
|
+
subject(:rom) { setup.finalize }
|
5
|
+
|
6
|
+
let(:original_path) { File.expand_path('./spec/fixtures/users.csv') }
|
7
|
+
let(:path) { File.expand_path('./spec/fixtures/testing.csv') }
|
8
|
+
|
9
|
+
# If :csv is not passed in the gateway is named `:default`
|
10
|
+
let(:setup) { ROM.setup(:csv, path) }
|
11
|
+
|
12
|
+
subject(:users) { rom.commands.users }
|
13
|
+
|
14
|
+
before do
|
15
|
+
FileUtils.copy(original_path, path)
|
16
|
+
|
17
|
+
setup.relation(:users) do
|
18
|
+
def by_id(id)
|
19
|
+
restrict(user_id: id)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
setup.commands(:users) do
|
24
|
+
define(:delete) do
|
25
|
+
result :one
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'raises error when tuple count does not match expectation' do
|
31
|
+
result = users.try { users.delete.call }
|
32
|
+
|
33
|
+
expect(result.value).to be(nil)
|
34
|
+
expect(result.error).to be_instance_of(ROM::TupleCountMismatchError)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'deletes all tuples in a restricted relation' do
|
38
|
+
result = users.try { users.delete.by_id(1).call }
|
39
|
+
|
40
|
+
expect(result.value)
|
41
|
+
.to eql(user_id: 1, name: "Julie", email: "julie.andrews@example.com")
|
42
|
+
|
43
|
+
rom.relation(:users).dataset.reload!
|
44
|
+
|
45
|
+
result = rom.relation(:users).to_a
|
46
|
+
expect(result.count).to eql(2)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe 'Commands / Updates' do
|
5
|
+
subject(:rom) { setup.finalize }
|
6
|
+
|
7
|
+
let(:original_path) { File.expand_path('./spec/fixtures/users.csv') }
|
8
|
+
let(:path) { File.expand_path('./spec/fixtures/testing.csv') }
|
9
|
+
|
10
|
+
# If :csv is not passed in the gateway is named `:default`
|
11
|
+
let(:setup) { ROM.setup(:csv, path) }
|
12
|
+
|
13
|
+
subject(:users) { rom.commands.users }
|
14
|
+
|
15
|
+
let(:relation) { rom.relations.users }
|
16
|
+
let(:first_data) { relation.by_id(1).to_a.first }
|
17
|
+
let(:new_data) { { name: 'Peter' } }
|
18
|
+
let(:output_data) do
|
19
|
+
[{ user_id: 1, name: 'Julie', email: 'tester@example.com' }]
|
20
|
+
end
|
21
|
+
|
22
|
+
before do
|
23
|
+
FileUtils.copy(original_path, path)
|
24
|
+
|
25
|
+
setup.relation(:users) do
|
26
|
+
def by_id(id)
|
27
|
+
restrict(user_id: id)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class User
|
32
|
+
include Virtus.model
|
33
|
+
|
34
|
+
attribute :id, Integer
|
35
|
+
attribute :name, String
|
36
|
+
attribute :email, String
|
37
|
+
end
|
38
|
+
|
39
|
+
setup.mappers do
|
40
|
+
define(:users) do
|
41
|
+
model User
|
42
|
+
register_as :entity
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
setup.commands(:users) do
|
47
|
+
define(:update)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'updates everything when there is no original tuple' do
|
52
|
+
result = users.try do
|
53
|
+
users.update.by_id(1).call(email: 'tester@example.com')
|
54
|
+
end
|
55
|
+
|
56
|
+
expect(result.value.to_a).to match_array(output_data)
|
57
|
+
|
58
|
+
# FIXME: reload! should not be necessary
|
59
|
+
rom.relation(:users).dataset.reload!
|
60
|
+
|
61
|
+
result = rom.relation(:users).as(:entity).by_id(1).to_a.first
|
62
|
+
expect(result.email).to eql('tester@example.com')
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'virtus'
|
3
|
+
|
4
|
+
describe 'CSV gateway' do
|
5
|
+
context 'without extra options' do
|
6
|
+
# If :csv is not passed in the gateway is named `:default`
|
7
|
+
let(:users_path) { File.expand_path('./spec/fixtures/users.csv') }
|
8
|
+
let(:setup) do
|
9
|
+
ROM.setup(
|
10
|
+
users: [:csv, users_path]
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
before do
|
15
|
+
module TestPlugin; end
|
16
|
+
|
17
|
+
ROM.plugins do
|
18
|
+
adapter :csv do
|
19
|
+
register :test_plugin, TestPlugin, type: :relation
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'specify relation with plugin' do
|
25
|
+
it "shouldn't raise error" do
|
26
|
+
expect {
|
27
|
+
setup.relation(:users) do
|
28
|
+
gateway :users
|
29
|
+
use :test_plugin
|
30
|
+
end
|
31
|
+
}.not_to raise_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,15 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'virtus'
|
3
3
|
|
4
|
-
describe 'CSV
|
4
|
+
describe 'CSV gateway' do
|
5
5
|
context 'without extra options' do
|
6
|
-
# If :csv is not passed in the
|
7
|
-
let(:
|
8
|
-
let(:
|
6
|
+
# If :csv is not passed in the gateway is named `:default`
|
7
|
+
let(:users_path) { File.expand_path('./spec/fixtures/users.csv') }
|
8
|
+
let(:addresses_path) { File.expand_path('./spec/fixtures/addresses.csv') }
|
9
|
+
let(:setup) do
|
10
|
+
ROM.setup(
|
11
|
+
users: [:csv, users_path],
|
12
|
+
addresses: [:csv, addresses_path]
|
13
|
+
)
|
14
|
+
end
|
9
15
|
subject(:rom) { setup.finalize }
|
10
16
|
|
11
17
|
before do
|
12
18
|
setup.relation(:users) do
|
19
|
+
gateway :users
|
20
|
+
|
13
21
|
def by_name(name)
|
14
22
|
restrict(name: name)
|
15
23
|
end
|
@@ -21,14 +29,38 @@ describe 'CSV repository' do
|
|
21
29
|
def ordered
|
22
30
|
order(:name, :email)
|
23
31
|
end
|
32
|
+
|
33
|
+
def with_addresses
|
34
|
+
join(addresses)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
setup.relation(:addresses) do
|
39
|
+
gateway :addresses
|
24
40
|
end
|
25
41
|
|
26
42
|
class User
|
27
43
|
include Virtus.model
|
28
44
|
|
29
|
-
attribute :
|
45
|
+
attribute :user_id, Integer
|
46
|
+
attribute :name, String
|
47
|
+
attribute :email, String
|
48
|
+
end
|
49
|
+
|
50
|
+
class UserWithAddress
|
51
|
+
include Virtus.model
|
52
|
+
|
53
|
+
attribute :user_id, Integer
|
30
54
|
attribute :name, String
|
31
55
|
attribute :email, String
|
56
|
+
attribute :addresses
|
57
|
+
end
|
58
|
+
|
59
|
+
class Address
|
60
|
+
include Virtus.model
|
61
|
+
|
62
|
+
attribute :address_id, Integer
|
63
|
+
attribute :street, String
|
32
64
|
end
|
33
65
|
|
34
66
|
setup.mappers do
|
@@ -36,6 +68,18 @@ describe 'CSV repository' do
|
|
36
68
|
model User
|
37
69
|
register_as :entity
|
38
70
|
end
|
71
|
+
|
72
|
+
define(:users_with_address, parent: :users) do
|
73
|
+
model UserWithAddress
|
74
|
+
register_as :entity_with_address
|
75
|
+
|
76
|
+
group :addresses do
|
77
|
+
model Address
|
78
|
+
|
79
|
+
attribute :address_id
|
80
|
+
attribute :street
|
81
|
+
end
|
82
|
+
end
|
39
83
|
end
|
40
84
|
end
|
41
85
|
|
@@ -43,7 +87,6 @@ describe 'CSV repository' do
|
|
43
87
|
it 'returns restricted and mapped object' do
|
44
88
|
jane = rom.relation(:users).as(:entity).by_name('Jane').to_a.first
|
45
89
|
|
46
|
-
expect(jane.id).to eql(3)
|
47
90
|
expect(jane.name).to eql('Jane')
|
48
91
|
expect(jane.email).to eql('jane@doe.org')
|
49
92
|
end
|
@@ -51,7 +94,7 @@ describe 'CSV repository' do
|
|
51
94
|
it 'returns specified attributes in mapped object' do
|
52
95
|
jane = rom.relation(:users).as(:entity).only_name.to_a.first
|
53
96
|
|
54
|
-
expect(jane.
|
97
|
+
expect(jane.user_id).to be_nil
|
55
98
|
expect(jane.name).not_to be_nil
|
56
99
|
expect(jane.email).to be_nil
|
57
100
|
end
|
@@ -68,6 +111,17 @@ describe 'CSV repository' do
|
|
68
111
|
expect(results[2].name).to eql('Julie')
|
69
112
|
expect(results[2].email).to eq('julie@doe.org')
|
70
113
|
end
|
114
|
+
|
115
|
+
it 'returns joined data' do
|
116
|
+
results = rom.relation(:users).as(:entity_with_address)
|
117
|
+
.with_addresses.first
|
118
|
+
|
119
|
+
expect(results.attributes.keys.sort)
|
120
|
+
.to eq([:user_id, :name, :email, :addresses].sort)
|
121
|
+
|
122
|
+
expect(results.addresses.first.attributes.keys.sort)
|
123
|
+
.to eq([:address_id, :street].sort)
|
124
|
+
end
|
71
125
|
end
|
72
126
|
|
73
127
|
context 'with custom options' do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,11 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
require 'rom/lint/spec'
|
4
4
|
|
5
|
-
describe ROM::CSV::
|
6
|
-
let(:
|
5
|
+
describe ROM::CSV::Gateway do
|
6
|
+
let(:gateway) { ROM::CSV::Gateway }
|
7
7
|
let(:uri) { File.expand_path('./spec/fixtures/users.csv') }
|
8
8
|
|
9
|
-
it_behaves_like "a rom
|
9
|
+
it_behaves_like "a rom gateway" do
|
10
10
|
let(:identifier) { :csv }
|
11
11
|
end
|
12
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Don Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rom
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: '0.9'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.9.0
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
29
|
+
version: '0.9'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.9.0
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: bundler
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,15 +94,24 @@ files:
|
|
88
94
|
- examples/users.csv
|
89
95
|
- lib/rom-csv.rb
|
90
96
|
- lib/rom/csv.rb
|
97
|
+
- lib/rom/csv/commands.rb
|
98
|
+
- lib/rom/csv/commands/create.rb
|
99
|
+
- lib/rom/csv/commands/delete.rb
|
100
|
+
- lib/rom/csv/commands/update.rb
|
91
101
|
- lib/rom/csv/dataset.rb
|
102
|
+
- lib/rom/csv/gateway.rb
|
92
103
|
- lib/rom/csv/relation.rb
|
93
|
-
- lib/rom/csv/repository.rb
|
94
104
|
- lib/rom/csv/version.rb
|
95
105
|
- rakelib/examples.rake
|
96
106
|
- rakelib/rubocop.rake
|
97
107
|
- rom-csv.gemspec
|
108
|
+
- spec/fixtures/addresses.csv
|
98
109
|
- spec/fixtures/users.csv
|
99
110
|
- spec/fixtures/users.utf-8.csv
|
111
|
+
- spec/integration/commands/create_spec.rb
|
112
|
+
- spec/integration/commands/delete_spec.rb
|
113
|
+
- spec/integration/commands/update_spec.rb
|
114
|
+
- spec/integration/relation_spec.rb
|
100
115
|
- spec/integration/repository_spec.rb
|
101
116
|
- spec/spec_helper.rb
|
102
117
|
- spec/unit/dataset_spec.rb
|
@@ -121,13 +136,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
136
|
version: '0'
|
122
137
|
requirements: []
|
123
138
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.4.5
|
125
140
|
signing_key:
|
126
141
|
specification_version: 4
|
127
142
|
summary: CSV support for Ruby Object Mapper
|
128
143
|
test_files:
|
144
|
+
- spec/fixtures/addresses.csv
|
129
145
|
- spec/fixtures/users.csv
|
130
146
|
- spec/fixtures/users.utf-8.csv
|
147
|
+
- spec/integration/commands/create_spec.rb
|
148
|
+
- spec/integration/commands/delete_spec.rb
|
149
|
+
- spec/integration/commands/update_spec.rb
|
150
|
+
- spec/integration/relation_spec.rb
|
131
151
|
- spec/integration/repository_spec.rb
|
132
152
|
- spec/spec_helper.rb
|
133
153
|
- spec/unit/dataset_spec.rb
|