virtus-mapper 0.2.1 → 0.3.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/lib/virtus/mapper.rb +1 -4
- data/lib/virtus/mapper/version.rb +1 -1
- data/spec/virtus/mapper_spec.rb +34 -22
- 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: 610232c9390a9e5738942dc54b1e50464e7661bc
|
4
|
+
data.tar.gz: 646327cae53a1c6d56de6cf93f3a9b4d2f260868
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1e216c9effb6ae800aeaaf3d159b9fd544f4ad541099a364c6ce7b83a717d60e93fed0da94ba474fdff1a3c298ef2988195e163a2d7cddc9d355fc6241de6d4
|
7
|
+
data.tar.gz: c5c37ed1bf79d24afa50bc7e21193f99f50eb1025dd5df42da6b2cb48649eae7b1835d9b472c391b17d024cb97cc9ba7ffeb38913264e3ca1b886809fec09b8c
|
data/lib/virtus/mapper.rb
CHANGED
@@ -41,10 +41,7 @@ module Virtus
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def attributes_to_map_by_symbol(attrs)
|
44
|
-
attributes_to_map
|
45
|
-
!from(att).respond_to?(:call) &&
|
46
|
-
!attrs.has_key?(att.name)
|
47
|
-
end
|
44
|
+
attributes_to_map - attributes_to_map_by_call
|
48
45
|
end
|
49
46
|
|
50
47
|
def attributes_to_map_by_call
|
data/spec/virtus/mapper_spec.rb
CHANGED
@@ -6,7 +6,7 @@ module Virtus
|
|
6
6
|
|
7
7
|
before do
|
8
8
|
module Examples
|
9
|
-
class
|
9
|
+
class PersonMapper
|
10
10
|
include Virtus.model
|
11
11
|
include Virtus::Mapper
|
12
12
|
|
@@ -19,7 +19,7 @@ module Virtus
|
|
19
19
|
from: lambda { |atts| atts[:address][:street] rescue '' }
|
20
20
|
end
|
21
21
|
|
22
|
-
module
|
22
|
+
module EmploymentMapper
|
23
23
|
include Virtus.module
|
24
24
|
include Virtus::Mapper
|
25
25
|
|
@@ -28,14 +28,14 @@ module Virtus
|
|
28
28
|
attribute :salary, Integer
|
29
29
|
end
|
30
30
|
|
31
|
-
module
|
31
|
+
module TraitsMapper
|
32
32
|
include Virtus.module
|
33
33
|
include Virtus::Mapper
|
34
34
|
|
35
35
|
attribute :eye_color, String, from: :eyecolor
|
36
36
|
end
|
37
37
|
|
38
|
-
class
|
38
|
+
class DogMapper
|
39
39
|
include Virtus.model
|
40
40
|
include Virtus::Mapper
|
41
41
|
|
@@ -57,7 +57,7 @@ module Virtus
|
|
57
57
|
let(:employment_attrs) {
|
58
58
|
{ salary: 100, business: 'RentPath', position: 'Programmer' }
|
59
59
|
}
|
60
|
-
let(:person) { Examples::
|
60
|
+
let(:person) { Examples::PersonMapper.new(person_attrs) }
|
61
61
|
|
62
62
|
describe 'attribute with from option as symbol' do
|
63
63
|
it 'translates key' do
|
@@ -68,22 +68,34 @@ module Virtus
|
|
68
68
|
expect { person.surname }.to raise_error(NoMethodError)
|
69
69
|
end
|
70
70
|
|
71
|
-
describe '
|
72
|
-
it '
|
73
|
-
expect { Examples::
|
71
|
+
describe 'required attribute with name key, missing from key' do
|
72
|
+
it 'raises error' do
|
73
|
+
expect { Examples::PersonMapper.new({id: 1}) }.to raise_error
|
74
74
|
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'attribute with name key, missing from key' do
|
78
|
+
it 'returns nil from reader method' do
|
79
|
+
data = { person_id: 1, last_name: 'Smith' }
|
80
|
+
person = Examples::PersonMapper.new(data)
|
81
|
+
expect(person.last_name).to be_nil
|
82
|
+
end
|
83
|
+
end
|
75
84
|
|
76
|
-
|
77
|
-
|
85
|
+
describe 'when name and from keys exist in initialized attributes' do
|
86
|
+
it 'prefers from data to name data' do
|
87
|
+
data = person_attrs.merge({ last_name: 'Smith' })
|
88
|
+
person = Examples::PersonMapper.new(data)
|
89
|
+
expect(person.last_name).to eq('Doe')
|
78
90
|
end
|
79
91
|
end
|
80
92
|
end
|
81
93
|
|
82
94
|
describe 'attribute with from option as callable object' do
|
83
95
|
it 'calls the object and passes the attributes hash' do
|
84
|
-
callable = Examples::
|
96
|
+
callable = Examples::PersonMapper.attribute_set[:address].options[:from]
|
85
97
|
expect(callable).to receive(:call) { person_attrs }
|
86
|
-
Examples::
|
98
|
+
Examples::PersonMapper.new(person_attrs)
|
87
99
|
end
|
88
100
|
|
89
101
|
it 'sets attribute to result of call' do
|
@@ -98,7 +110,7 @@ module Virtus
|
|
98
110
|
end
|
99
111
|
|
100
112
|
it 'maps attributes with indifferent access' do
|
101
|
-
person = Examples::
|
113
|
+
person = Examples::PersonMapper.new({ person_id: 1,
|
102
114
|
first_name: first_name,
|
103
115
|
'surname' => last_name })
|
104
116
|
expect(person.last_name).to eq('Doe')
|
@@ -106,22 +118,22 @@ module Virtus
|
|
106
118
|
|
107
119
|
describe 'given no arguments to constructor' do
|
108
120
|
it 'does not raise error' do
|
109
|
-
expect { Examples::
|
121
|
+
expect { Examples::DogMapper.new }.not_to raise_error
|
110
122
|
end
|
111
123
|
|
112
124
|
it 'respects defaults' do
|
113
|
-
expect(Examples::
|
125
|
+
expect(Examples::DogMapper.new.name).to eq('Spot')
|
114
126
|
end
|
115
127
|
end
|
116
128
|
|
117
129
|
describe 'given nil values' do
|
118
130
|
it 'respects nil values' do
|
119
|
-
expect(Examples::
|
131
|
+
expect(Examples::DogMapper.new(name: nil).name).to be_nil
|
120
132
|
end
|
121
133
|
end
|
122
134
|
|
123
135
|
describe '#mapped_attributes' do
|
124
|
-
let(:person) { Examples::
|
136
|
+
let(:person) { Examples::PersonMapper.new(person_attrs.merge({ unused: true })) }
|
125
137
|
|
126
138
|
it 'preserves unused attributes' do
|
127
139
|
expect(person.mapped_attributes[:unused]).to be true
|
@@ -135,11 +147,11 @@ module Virtus
|
|
135
147
|
describe '#extend_with' do
|
136
148
|
describe 'for single extended module' do
|
137
149
|
let(:person) {
|
138
|
-
Examples::
|
150
|
+
Examples::PersonMapper.new(person_attrs.merge(employment_attrs))
|
139
151
|
}
|
140
152
|
|
141
153
|
before do
|
142
|
-
person.extend_with(Examples::
|
154
|
+
person.extend_with(Examples::EmploymentMapper)
|
143
155
|
end
|
144
156
|
|
145
157
|
it 'updates unmapped attribute values for extended modules' do
|
@@ -166,14 +178,14 @@ module Virtus
|
|
166
178
|
|
167
179
|
describe 'for multiple extended modules' do
|
168
180
|
let(:person) {
|
169
|
-
Examples::
|
181
|
+
Examples::PersonMapper.new(
|
170
182
|
person_attrs.merge(employment_attrs.merge({ eyecolor: 'green' }))
|
171
183
|
)
|
172
184
|
}
|
173
185
|
|
174
186
|
before do
|
175
|
-
person.extend_with(Examples::
|
176
|
-
person.extend_with(Examples::
|
187
|
+
person.extend_with(Examples::EmploymentMapper)
|
188
|
+
person.extend_with(Examples::TraitsMapper)
|
177
189
|
end
|
178
190
|
|
179
191
|
it 'updates mapped attributes for last module extended' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: virtus-mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RentPath
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|