rom-mapper 0.3.0.beta1 → 0.3.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/lib/rom/header.rb +4 -0
- data/lib/rom/mapper.rb +1 -1
- data/lib/rom/mapper/attribute_dsl.rb +3 -2
- data/lib/rom/mapper/dsl.rb +2 -1
- data/lib/rom/mapper/version.rb +1 -1
- data/lib/rom/processor/transproc.rb +2 -1
- data/spec/integration/mapper_spec.rb +19 -4
- data/spec/unit/rom/mapper/dsl_spec.rb +13 -1
- data/spec/unit/rom/processor/transproc_spec.rb +23 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8445a0b1754d08c53cd69fb5e4d5ebc1101b81b1
|
4
|
+
data.tar.gz: 48e8e7ff9b54220e4e1680e9675db6e56a385639
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb63e1a42a59f4feee6d5457d729c52616f7c7ba4818da90bb904553d01b75920165bdb7923e1d7b77766a19945c8e221eb080c4cfa32b59aa5574c6879d403e
|
7
|
+
data.tar.gz: aca717bcb4b6d16c29b55a61ae5630581fd5f5f057ab4c6261e1dcf92d1c6730f1d5c121a73e4e0a8a98f23e78f2abb993c4870b38664edc59d0e0cdd5c2002d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
# v0.3.0 to-be-released
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Allow `attribute`'s `:from` option to take an array of other attribute names (hmadison)
|
6
|
+
|
7
|
+
### Changed
|
8
|
+
|
9
|
+
* Coercer blocks are now executed in the context of the mapper object (AMHOL)
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
* `model` will skip excluded attributes (chastell)
|
14
|
+
|
15
|
+
[Compare v0.2.0..HEAD](https://github.com/rom-rb/rom-mapper/compare/v0.2.0...HEAD)
|
16
|
+
|
1
17
|
# v0.2.0 2015-08-10
|
2
18
|
|
3
19
|
Import code from rom 0.8.1
|
data/lib/rom/header.rb
CHANGED
@@ -19,6 +19,9 @@ module ROM
|
|
19
19
|
# @api private
|
20
20
|
attr_reader :reject_keys
|
21
21
|
|
22
|
+
# @api private
|
23
|
+
attr_reader :copy_keys
|
24
|
+
|
22
25
|
# @api private
|
23
26
|
attr_reader :attributes
|
24
27
|
|
@@ -62,6 +65,7 @@ module ROM
|
|
62
65
|
def initialize(attributes, options = {})
|
63
66
|
@options = options
|
64
67
|
@model = options[:model]
|
68
|
+
@copy_keys = options.fetch(:copy_keys, false)
|
65
69
|
@reject_keys = options.fetch(:reject_keys, false)
|
66
70
|
|
67
71
|
@attributes = attributes
|
data/lib/rom/mapper.rb
CHANGED
@@ -8,7 +8,7 @@ module ROM
|
|
8
8
|
include DSL
|
9
9
|
include Dry::Equalizer(:transformers, :header)
|
10
10
|
|
11
|
-
defines :relation, :register_as, :symbolize_keys,
|
11
|
+
defines :relation, :register_as, :symbolize_keys, :copy_keys,
|
12
12
|
:prefix, :prefix_separator, :inherit_header, :reject_keys
|
13
13
|
|
14
14
|
inherit_header true
|
@@ -14,7 +14,7 @@ module ROM
|
|
14
14
|
class AttributeDSL
|
15
15
|
include ModelDSL
|
16
16
|
|
17
|
-
attr_reader :attributes, :options, :symbolize_keys, :reject_keys, :steps
|
17
|
+
attr_reader :attributes, :options, :copy_keys, :symbolize_keys, :reject_keys, :steps
|
18
18
|
|
19
19
|
# @param [Array] attributes accumulator array
|
20
20
|
# @param [Hash] options
|
@@ -23,6 +23,7 @@ module ROM
|
|
23
23
|
def initialize(attributes, options)
|
24
24
|
@attributes = attributes
|
25
25
|
@options = options
|
26
|
+
@copy_keys = options.fetch(:copy_keys)
|
26
27
|
@symbolize_keys = options.fetch(:symbolize_keys)
|
27
28
|
@prefix = options.fetch(:prefix)
|
28
29
|
@prefix_separator = options.fetch(:prefix_separator)
|
@@ -344,7 +345,7 @@ module ROM
|
|
344
345
|
#
|
345
346
|
# @api private
|
346
347
|
def header
|
347
|
-
Header.coerce(attributes, model: model, reject_keys: reject_keys)
|
348
|
+
Header.coerce(attributes, copy_keys: copy_keys, model: model, reject_keys: reject_keys)
|
348
349
|
end
|
349
350
|
|
350
351
|
private
|
data/lib/rom/mapper/dsl.rb
CHANGED
data/lib/rom/mapper/version.rb
CHANGED
@@ -353,9 +353,10 @@ module ROM
|
|
353
353
|
# @api private
|
354
354
|
def initialize_row_proc
|
355
355
|
@row_proc = compose { |ops|
|
356
|
+
alias_handler = header.copy_keys ? :copy_keys : :rename_keys
|
356
357
|
process_header_keys(ops)
|
357
358
|
|
358
|
-
ops << t(
|
359
|
+
ops << t(alias_handler, mapping) if header.aliased?
|
359
360
|
ops << header.map { |attr| visit(attr) }
|
360
361
|
ops << t(:constructor_inject, model) if model
|
361
362
|
}
|
@@ -17,7 +17,7 @@ describe ROM::Mapper do
|
|
17
17
|
let(:mapper_body) do
|
18
18
|
proc do
|
19
19
|
attribute(:key) { |key| [prefix, key].join('_') }
|
20
|
-
|
20
|
+
|
21
21
|
def prefix
|
22
22
|
'foo'
|
23
23
|
end
|
@@ -28,6 +28,21 @@ describe ROM::Mapper do
|
|
28
28
|
is_expected.to match_array(results)
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
context 'when copying aliased keys to multiple attributes' do
|
33
|
+
let(:tuples) { [{ key: 'bar' }] }
|
34
|
+
let(:results) { [{ key: 'bar', key2: 'bar', key3: 'bar' }] }
|
35
|
+
let(:mapper_body) do
|
36
|
+
proc do
|
37
|
+
copy_keys true
|
38
|
+
attribute([:key2, :key3], from: :key)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'creates attributes by copying keys rather than renaming' do
|
43
|
+
is_expected.to match_array(results)
|
44
|
+
end
|
45
|
+
end
|
31
46
|
end
|
32
47
|
|
33
48
|
describe '.embedded' do
|
@@ -39,7 +54,7 @@ describe ROM::Mapper do
|
|
39
54
|
embedded :items, type: :hash do
|
40
55
|
attribute(:key) { |key| [prefix, key].join('_') }
|
41
56
|
end
|
42
|
-
|
57
|
+
|
43
58
|
def prefix
|
44
59
|
'foo'
|
45
60
|
end
|
@@ -61,7 +76,7 @@ describe ROM::Mapper do
|
|
61
76
|
wrap :items do
|
62
77
|
attribute(:key) { |key| [prefix, key].join('_') }
|
63
78
|
end
|
64
|
-
|
79
|
+
|
65
80
|
def prefix
|
66
81
|
'foo'
|
67
82
|
end
|
@@ -83,7 +98,7 @@ describe ROM::Mapper do
|
|
83
98
|
unwrap :items do
|
84
99
|
attribute(:key) { |key| [prefix, key].join('_') }
|
85
100
|
end
|
86
|
-
|
101
|
+
|
87
102
|
def prefix
|
88
103
|
'foo'
|
89
104
|
end
|
@@ -71,11 +71,23 @@ describe ROM::Mapper do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
describe 'copy_keys' do
|
75
|
+
let(:attributes) { [[:name, type: :string]] }
|
76
|
+
let(:options) { { copy_keys: true } }
|
77
|
+
|
78
|
+
it 'sets copy_keys for the header' do
|
79
|
+
mapper.copy_keys true
|
80
|
+
mapper.attribute :name, type: :string
|
81
|
+
|
82
|
+
expect(header).to eql(expected_header)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
74
86
|
describe 'reject_keys' do
|
75
87
|
let(:attributes) { [[:name, type: :string]] }
|
76
88
|
let(:options) { { reject_keys: true } }
|
77
89
|
|
78
|
-
it 'sets
|
90
|
+
it 'sets reject_keys for the header' do
|
79
91
|
mapper.reject_keys true
|
80
92
|
mapper.attribute :name, type: :string
|
81
93
|
|
@@ -68,21 +68,39 @@ describe ROM::Processor::Transproc do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
context 'copying keys' do
|
72
|
+
let(:options) do
|
73
|
+
{ copy_keys: true }
|
74
|
+
end
|
75
|
+
|
76
|
+
let(:attributes) do
|
77
|
+
[['b', from: 'a'], ['c', from: 'b']]
|
78
|
+
end
|
79
|
+
|
80
|
+
let(:relation) do
|
81
|
+
[{ 'a' => 'copy' }]
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'copies without removing the original' do
|
85
|
+
expect(transproc[relation]).to eql([{ 'a' => 'copy', 'b' => 'copy', 'c' => 'copy' }])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
71
89
|
describe 'key from exsisting keys' do
|
72
90
|
let(:attributes) do
|
73
|
-
coercer = ->(a,b) { b + a }
|
74
|
-
[[:c, {from: [:a, :b], coercer: coercer}
|
91
|
+
coercer = ->(a, b) { b + a }
|
92
|
+
[[:c, { from: [:a, :b], coercer: coercer }]]
|
75
93
|
end
|
76
94
|
|
77
95
|
let(:relation) do
|
78
96
|
[
|
79
|
-
{a: 'works', b: 'this'}
|
97
|
+
{ a: 'works', b: 'this' }
|
80
98
|
]
|
81
99
|
end
|
82
100
|
|
83
|
-
let
|
101
|
+
let(:expected_result) do
|
84
102
|
[
|
85
|
-
{c: 'thisworks'}
|
103
|
+
{ c: 'thisworks' }
|
86
104
|
]
|
87
105
|
end
|
88
106
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0.
|
4
|
+
version: 0.3.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-equalizer
|