HornsAndHooves-flat_map 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HornsAndHooves-flat_map.gemspec +3 -3
- data/lib/flat_map/version.rb +1 -1
- data/spec/flat_map/errors_spec.rb +3 -3
- data/spec/flat_map/mapper/attribute_methods_spec.rb +11 -11
- data/spec/flat_map/mapper/callbacks_spec.rb +7 -7
- data/spec/flat_map/mapper/factory_spec.rb +72 -70
- data/spec/flat_map/mapper/mapping_spec.rb +17 -17
- data/spec/flat_map/mapper/mounting_spec.rb +24 -24
- data/spec/flat_map/mapper/persistence_spec.rb +34 -31
- data/spec/flat_map/mapper/skipping_spec.rb +17 -16
- data/spec/flat_map/mapper/targeting_spec.rb +32 -33
- data/spec/flat_map/mapper/traits_spec.rb +22 -22
- data/spec/flat_map/mapper/validations_spec.rb +7 -7
- data/spec/flat_map/mapping/factory_spec.rb +1 -1
- data/spec/flat_map/mapping/reader/basic_spec.rb +7 -7
- data/spec/flat_map/mapping/reader/formatted_spec.rb +9 -9
- data/spec/flat_map/mapping/reader/method_spec.rb +5 -5
- data/spec/flat_map/mapping/reader/proc_spec.rb +5 -5
- data/spec/flat_map/mapping/writer/basic_spec.rb +6 -6
- data/spec/flat_map/mapping/writer/method_spec.rb +4 -4
- data/spec/flat_map/mapping/writer/proc_spec.rb +4 -4
- data/spec/flat_map/mapping_spec.rb +24 -24
- metadata +26 -14
@@ -31,13 +31,13 @@ module FlatMap
|
|
31
31
|
describe 'Mapping' do
|
32
32
|
context 'defining mappings' do
|
33
33
|
it "should use Factory for defining mappings" do
|
34
|
-
MappingSpec::EmptyMapper.
|
34
|
+
expect(MappingSpec::EmptyMapper).to receive(:define_mappings).once.
|
35
35
|
with({:attr_a => :attr_a, :mapped_attr_b => :attr_b}, {:writer => false}).
|
36
36
|
and_call_original
|
37
|
-
Mapping::Factory.
|
37
|
+
expect(Mapping::Factory).to receive(:new).
|
38
38
|
with(:attr_a, :attr_a, :writer => false).
|
39
39
|
and_call_original
|
40
|
-
Mapping::Factory.
|
40
|
+
expect(Mapping::Factory).to receive(:new).
|
41
41
|
with(:mapped_attr_b, :attr_b, :writer => false).
|
42
42
|
and_call_original
|
43
43
|
|
@@ -48,36 +48,36 @@ module FlatMap
|
|
48
48
|
end
|
49
49
|
|
50
50
|
specify 'mapper class should have defined mappings' do
|
51
|
-
MappingSpec::SpecMapper.mappings.size.
|
52
|
-
MappingSpec::SpecMapper.mappings.all?{ |m| m.is_a?(Mapping::Factory) }.
|
51
|
+
expect(MappingSpec::SpecMapper.mappings.size).to eq 4
|
52
|
+
expect(MappingSpec::SpecMapper.mappings.all?{ |m| m.is_a?(Mapping::Factory) }).to be true
|
53
53
|
end
|
54
54
|
|
55
55
|
context "for initialized mapper" do
|
56
|
-
let(:target){ MappingSpec::SpecTarget.new('a', 'b', 'c', 'd') }
|
57
|
-
let(:mapper){ MappingSpec::SpecMapper.new(target) }
|
56
|
+
let(:target) { MappingSpec::SpecTarget.new('a', 'b', 'c', 'd') }
|
57
|
+
let(:mapper) { MappingSpec::SpecMapper.new(target) }
|
58
58
|
|
59
59
|
it "should be able to access mapping by its name" do
|
60
|
-
mapper.mapping(:mapped_attr_a).
|
61
|
-
mapper.mapping(:not_defined).
|
60
|
+
expect(mapper.mapping(:mapped_attr_a)).to be_a(FlatMap::Mapping)
|
61
|
+
expect(mapper.mapping(:not_defined )).to be_nil
|
62
62
|
end
|
63
63
|
|
64
64
|
describe 'reading and writing' do
|
65
65
|
it "should be able to read from target via brackets" do
|
66
|
-
mapper[:mapped_attr_a].
|
66
|
+
expect(mapper[:mapped_attr_a]).to eq 'a'
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'should be able to write to target via brackets' do
|
70
70
|
mapper[:attr_b] = 'B'
|
71
|
-
target.attr_b.
|
71
|
+
expect(target.attr_b).to eq 'B'
|
72
72
|
end
|
73
73
|
|
74
74
|
it '#read should read all mappings to a hash' do
|
75
|
-
mapper.read.
|
75
|
+
expect(mapper.read).to eq({
|
76
76
|
:mapped_attr_a => 'a',
|
77
77
|
:attr_b => 'b',
|
78
78
|
:attr_c => 'attr_c-c',
|
79
79
|
:mapped_attr_d => 'mapped_attr_d-d'
|
80
|
-
}
|
80
|
+
})
|
81
81
|
end
|
82
82
|
|
83
83
|
it '#write should assign values using mappings, ignoring invalid ones' do
|
@@ -87,10 +87,10 @@ module FlatMap
|
|
87
87
|
:attr_c => 'new-c',
|
88
88
|
:mapped_attr_d => 'new-d'
|
89
89
|
|
90
|
-
target.attr_a.
|
91
|
-
target.attr_b.
|
92
|
-
target.attr_c.
|
93
|
-
target.attr_d.
|
90
|
+
expect(target.attr_a).to eq 'A'
|
91
|
+
expect(target.attr_b).to eq 'B'
|
92
|
+
expect(target.attr_c).to eq 'NEW-C'
|
93
|
+
expect(target.attr_d).to eq 'NEW-D'
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
@@ -75,7 +75,7 @@ module FlatMap
|
|
75
75
|
|
76
76
|
context 'defining mountings' do
|
77
77
|
it "should use Factory for defining mappings" do
|
78
|
-
Mapper::Factory.
|
78
|
+
expect(Mapper::Factory).to receive(:new).
|
79
79
|
with(:foo, :mapper_class_name => 'FooMapper').
|
80
80
|
and_call_original
|
81
81
|
|
@@ -85,75 +85,75 @@ module FlatMap
|
|
85
85
|
end
|
86
86
|
|
87
87
|
describe 'properties' do
|
88
|
-
it{ mapper.hosted
|
89
|
-
it{ mounting.hosted
|
90
|
-
it{ mounting.host.
|
88
|
+
it{ expect(mapper.hosted? ).to be false }
|
89
|
+
it{ expect(mounting.hosted?).to be true }
|
90
|
+
it{ expect(mounting.host ).to eq mapper }
|
91
91
|
end
|
92
92
|
|
93
93
|
it 'should be able to access mapping by name' do
|
94
|
-
mapper.mounting(:spec_mount).
|
95
|
-
mapper.mounting(:undefined_mount).
|
94
|
+
expect(mapper.mounting(:spec_mount )).to be_a(FlatMap::Mapper)
|
95
|
+
expect(mapper.mounting(:undefined_mount)).to be_nil
|
96
96
|
end
|
97
97
|
|
98
98
|
describe "#read" do
|
99
99
|
it 'should add mounted mappers to a result' do
|
100
|
-
mapper.read.
|
100
|
+
expect(mapper.read).to eq({
|
101
101
|
:host_attr => 'attr',
|
102
102
|
:attr_a => 'a',
|
103
103
|
:mapped_attr_b => 'b'
|
104
|
-
}
|
104
|
+
})
|
105
105
|
end
|
106
106
|
|
107
107
|
it 'should define dynamic writer methods' do
|
108
|
-
mapper.respond_to?(:attr_a).
|
109
|
-
mapper.method(:attr_a).
|
108
|
+
expect(mapper.respond_to?(:attr_a)).to be true
|
109
|
+
expect(mapper.method(:attr_a )).not_to be nil
|
110
110
|
|
111
|
-
mapper.respond_to?(:mapped_attr_b).
|
112
|
-
mapper.method(:mapped_attr_b).
|
111
|
+
expect(mapper.respond_to?(:mapped_attr_b)).to be true
|
112
|
+
expect(mapper.method(:mapped_attr_b )).not_to be nil
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
116
|
describe "#write" do
|
117
117
|
it 'should define dynamic writer methods' do
|
118
|
-
mapper.respond_to?(:attr_a=).
|
119
|
-
mapper.method(:attr_a=).
|
118
|
+
expect(mapper.respond_to?(:attr_a=)).to be true
|
119
|
+
expect(mapper.method(:attr_a= )).not_to be nil
|
120
120
|
|
121
|
-
mapper.respond_to?(:mapped_attr_b=).
|
122
|
-
mapper.method(:mapped_attr_b=).
|
121
|
+
expect(mapper.respond_to?(:mapped_attr_b=)).to be true
|
122
|
+
expect(mapper.method(:mapped_attr_b= )).not_to be nil
|
123
123
|
end
|
124
124
|
|
125
125
|
it 'should pass values to mounted mappers' do
|
126
126
|
target = MountingSpec.mount_target
|
127
127
|
mapper.write :attr_a => 'A', :mapped_attr_b => 'B'
|
128
|
-
target.attr_a.
|
129
|
-
target.attr_b.
|
128
|
+
expect(target.attr_a).to eq 'A'
|
129
|
+
expect(target.attr_b).to eq 'B'
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
it 'should delegate missing methods to mounted mappers' do
|
134
|
-
expect{ mapper.a_method.
|
134
|
+
expect{ expect(mapper.a_method).to eq 'a value' }.not_to raise_error
|
135
135
|
end
|
136
136
|
|
137
137
|
specify '#before_save_mountings' do
|
138
|
-
mapper.before_save_mountings.
|
138
|
+
expect(mapper.before_save_mountings).to eq [mapper.mounting(:spec_mount_before)]
|
139
139
|
end
|
140
140
|
|
141
141
|
specify '#after_save_mountings' do
|
142
|
-
mapper.after_save_mountings.
|
142
|
+
expect(mapper.after_save_mountings).to eq [mapper.mounting(:spec_mount)]
|
143
143
|
end
|
144
144
|
|
145
145
|
context 'with suffix' do
|
146
146
|
let(:mapper){ MountingSuffixSpec::SpecMapper.new(OpenStruct.new) }
|
147
147
|
let(:mounting){ mapper.mounting(:mount_foo) }
|
148
148
|
|
149
|
-
it{ mapper.
|
150
|
-
it{ mounting.
|
149
|
+
it{ expect(mapper ).not_to be_suffixed }
|
150
|
+
it{ expect(mounting).to be_suffixed }
|
151
151
|
|
152
152
|
it 'should cascade to nested mappings' do
|
153
153
|
mapper.attr_mount_foo = 'foo'
|
154
154
|
mapper.attr_nested_foo = 'bar'
|
155
155
|
|
156
|
-
mapper.read.
|
156
|
+
expect(mapper.read).to include({
|
157
157
|
:attr_mount_foo => 'foo',
|
158
158
|
:attr_nested_foo => 'bar' })
|
159
159
|
end
|
@@ -29,21 +29,24 @@ module FlatMap
|
|
29
29
|
describe Mapper::Persistence do
|
30
30
|
describe '#target_class' do
|
31
31
|
it 'should detect target_class from mapper class name' do
|
32
|
-
PersistenceSpec::TargetClassMapper.target_class.
|
32
|
+
expect(PersistenceSpec::TargetClassMapper.target_class.ancestors).
|
33
|
+
to include PersistenceSpec::TargetClass
|
33
34
|
end
|
34
35
|
|
35
36
|
it 'should detect target_class from nearest ancestor when inherited' do
|
36
|
-
PersistenceSpec::InheritedClassMapper.target_class.
|
37
|
+
expect(PersistenceSpec::InheritedClassMapper.target_class.ancestors).
|
38
|
+
to include PersistenceSpec::TargetClass
|
37
39
|
end
|
38
40
|
|
39
41
|
it 'should use explicit class name if specified' do
|
40
|
-
PersistenceSpec::ExplicitNameMapper.target_class.
|
42
|
+
expect(PersistenceSpec::ExplicitNameMapper.target_class.ancestors).
|
43
|
+
to include PersistenceSpec::OtherTargetClass
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
44
47
|
describe '.build' do
|
45
48
|
it 'should use target class to build a new object for mapper' do
|
46
|
-
PersistenceSpec::TargetClassMapper.
|
49
|
+
expect(PersistenceSpec::TargetClassMapper).to receive(:new).with(kind_of(PersistenceSpec::TargetClass), :used_trait)
|
47
50
|
PersistenceSpec::TargetClassMapper.build(:used_trait)
|
48
51
|
end
|
49
52
|
end
|
@@ -52,8 +55,8 @@ module FlatMap
|
|
52
55
|
let(:target){ PersistenceSpec::TargetClass.new('a', 'b') }
|
53
56
|
|
54
57
|
it 'should delegate to target class to find object for mapper' do
|
55
|
-
PersistenceSpec::TargetClass.
|
56
|
-
PersistenceSpec::TargetClassMapper.
|
58
|
+
expect(PersistenceSpec::TargetClass).to receive(:find).with(1).and_return(target)
|
59
|
+
expect(PersistenceSpec::TargetClassMapper).to receive(:new).with(target, :used_trait)
|
57
60
|
PersistenceSpec::TargetClassMapper.find(1, :used_trait)
|
58
61
|
end
|
59
62
|
end
|
@@ -63,35 +66,35 @@ module FlatMap
|
|
63
66
|
let(:mapper){ PersistenceSpec::TargetClassMapper.new(target){} }
|
64
67
|
|
65
68
|
specify '#model_name' do
|
66
|
-
mapper.model_name.
|
69
|
+
expect(mapper.model_name).to eq 'mapper'
|
67
70
|
end
|
68
71
|
|
69
72
|
specify '#to_key should delegate to target' do
|
70
|
-
target.
|
71
|
-
mapper.to_key.
|
73
|
+
expect(target).to receive(:to_key).and_return(1)
|
74
|
+
expect(mapper.to_key).to eq 1
|
72
75
|
end
|
73
76
|
|
74
77
|
specify '#persisted? when target does not respond to :persised?' do
|
75
|
-
mapper.
|
78
|
+
expect(mapper).not_to be_persisted
|
76
79
|
end
|
77
80
|
|
78
81
|
specify '#persisted? when target responds to :persisted?' do
|
79
|
-
target.
|
80
|
-
mapper.
|
82
|
+
expect(target).to receive(:persisted?).and_return(true)
|
83
|
+
expect(mapper).to be_persisted
|
81
84
|
end
|
82
85
|
|
83
86
|
specify '#id when target does not respond to :id' do
|
84
|
-
mapper.id.
|
87
|
+
expect(mapper.id).to be_nil
|
85
88
|
end
|
86
89
|
|
87
90
|
specify '#id when target responds to :id' do
|
88
|
-
target.
|
89
|
-
mapper.id.
|
91
|
+
expect(target).to receive(:id).and_return(1)
|
92
|
+
expect(mapper.id).to eq 1
|
90
93
|
end
|
91
94
|
|
92
95
|
describe '#write with multiparams' do
|
93
96
|
let(:params) {{
|
94
|
-
'attr_a'
|
97
|
+
'attr_a' => 'A',
|
95
98
|
'dob(0i)' => '1999',
|
96
99
|
'dob(1i)' => '01',
|
97
100
|
'dob(2i)' => '02'
|
@@ -99,23 +102,23 @@ module FlatMap
|
|
99
102
|
|
100
103
|
it 'should assign values properly' do
|
101
104
|
mapper.write(params)
|
102
|
-
target.attr_a.
|
103
|
-
target.attr_b.
|
105
|
+
expect(target.attr_a).to eq 'A'
|
106
|
+
expect(target.attr_b).to eq Date.new(1999, 1, 2)
|
104
107
|
end
|
105
108
|
end
|
106
109
|
|
107
110
|
describe '#save_target' do
|
108
111
|
it 'should return true for owned mappers' do
|
109
|
-
mapper.extension.save_target.
|
112
|
+
expect(mapper.extension.save_target).to eq true
|
110
113
|
end
|
111
114
|
|
112
115
|
it 'should return true if target does not respond to #save' do
|
113
|
-
mapper.save_target.
|
116
|
+
expect(mapper.save_target).to eq true
|
114
117
|
end
|
115
118
|
|
116
119
|
it 'should save with no validation if target responds to #save' do
|
117
|
-
target.
|
118
|
-
mapper.save_target.
|
120
|
+
expect(target).to receive(:save).with(:validate => false).and_return(true)
|
121
|
+
expect(mapper.save_target).to eq true
|
119
122
|
end
|
120
123
|
end
|
121
124
|
|
@@ -123,28 +126,28 @@ module FlatMap
|
|
123
126
|
let(:params){{ :attr_a => 'A' }}
|
124
127
|
|
125
128
|
it 'should write params first' do
|
126
|
-
mapper.
|
127
|
-
ActiveRecord::Base.
|
129
|
+
expect(mapper).to receive(:write).with(params)
|
130
|
+
expect(ActiveRecord::Base).to receive(:transaction).and_yield
|
128
131
|
mapper.apply(params)
|
129
132
|
end
|
130
133
|
|
131
134
|
it 'should not save if not valid' do
|
132
|
-
mapper.
|
133
|
-
mapper.
|
135
|
+
expect(mapper).to receive(:valid?).and_return(false)
|
136
|
+
expect(mapper).not_to receive(:save)
|
134
137
|
mapper.apply(params)
|
135
138
|
end
|
136
139
|
|
137
140
|
it 'should save if valid' do
|
138
|
-
mapper.
|
139
|
-
ActiveRecord::Base.
|
140
|
-
mapper.
|
141
|
+
expect(mapper).to receive(:valid?).and_return(true)
|
142
|
+
expect(ActiveRecord::Base).to receive(:transaction).and_yield
|
143
|
+
expect(mapper).to receive(:save)
|
141
144
|
mapper.apply(params)
|
142
145
|
end
|
143
146
|
end
|
144
147
|
|
145
148
|
specify '#shallow_save saves target in a save callbacks' do
|
146
|
-
mapper.
|
147
|
-
mapper.
|
149
|
+
expect(mapper).to receive(:run_callbacks).with(:save).and_yield
|
150
|
+
expect(mapper).to receive(:save_target)
|
148
151
|
mapper.shallow_save
|
149
152
|
end
|
150
153
|
end
|
@@ -28,22 +28,22 @@ module FlatMap
|
|
28
28
|
before{ mapper.trait(:with_trait).skip! }
|
29
29
|
|
30
30
|
it 'should completely ignore skipped mounting' do
|
31
|
-
mapper.
|
32
|
-
mapper.save.
|
33
|
-
mapper.attr_a.
|
34
|
-
mapper.attr_b.
|
31
|
+
expect(mapper ).to be_valid
|
32
|
+
expect(mapper.save ).to be true
|
33
|
+
expect(mapper.attr_a).to be_nil
|
34
|
+
expect(mapper.attr_b).to be_nil
|
35
35
|
end
|
36
36
|
|
37
37
|
it '#use! should enable skipped mounting' do
|
38
38
|
mapper.trait(:with_trait).use!
|
39
39
|
|
40
|
-
mapper.
|
41
|
-
mapper.attr_a.
|
42
|
-
mapper.errors[:attr_a].
|
40
|
+
expect(mapper).not_to be_valid
|
41
|
+
expect(mapper.attr_a).to eq 'a'
|
42
|
+
expect(mapper.errors[:attr_a]).to be_present
|
43
43
|
|
44
44
|
mapper.attr_a = 5
|
45
45
|
mapper.save
|
46
|
-
mapper.attr_b.
|
46
|
+
expect(mapper.attr_b).to eq 'b'
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -51,39 +51,40 @@ module FlatMap
|
|
51
51
|
let(:target){ OpenStruct.new }
|
52
52
|
let(:mapper){ SkippingSpec::SpecMapper.new(target, :with_trait) }
|
53
53
|
|
54
|
-
before{ target
|
54
|
+
before{ expect(target).
|
55
|
+
to receive(:is_a?).at_least(1).times.with(ActiveRecord::Base).and_return(true) }
|
55
56
|
|
56
57
|
context 'for new record' do
|
57
58
|
before do
|
58
|
-
target.
|
59
|
+
expect(target).to receive(:new_record?).at_least(1).times.and_return(true)
|
59
60
|
mapper.trait(:with_trait).skip!
|
60
61
|
end
|
61
62
|
|
62
63
|
specify '#skip! should set ivar @destroyed to true' do
|
63
|
-
target.instance_variable_get('@destroyed').
|
64
|
+
expect(target.instance_variable_get('@destroyed')).to be true
|
64
65
|
end
|
65
66
|
|
66
67
|
specify '#use! should set ivar @destroyed to true' do
|
67
68
|
mapper.trait(:with_trait).use!
|
68
|
-
target.instance_variable_get('@destroyed').
|
69
|
+
expect(target.instance_variable_get('@destroyed')).to be false
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
72
73
|
context 'for persisted record' do
|
73
74
|
before do
|
74
|
-
target.
|
75
|
+
expect(target).to receive(:new_record?).at_least(1).times.and_return(false)
|
75
76
|
end
|
76
77
|
|
77
78
|
specify '#skip! should reload persisted record' do
|
78
|
-
target.
|
79
|
+
expect(target).to receive(:reload)
|
79
80
|
mapper.trait(:with_trait).skip!
|
80
81
|
end
|
81
82
|
|
82
83
|
specify '#use! should use all nested mountings' do
|
83
84
|
mapper.trait(:with_trait).skip!
|
84
85
|
mock = double('mounting')
|
85
|
-
mock.
|
86
|
-
mapper.trait(:with_trait).
|
86
|
+
expect(mock).to receive(:use!)
|
87
|
+
expect(mapper.trait(:with_trait)).to receive(:all_nested_mountings).and_return([mock])
|
87
88
|
mapper.trait(:with_trait).use!
|
88
89
|
end
|
89
90
|
end
|
@@ -29,24 +29,23 @@ module FlatMap
|
|
29
29
|
describe 'Working with Target' do
|
30
30
|
describe '#target_class' do
|
31
31
|
it 'should detect target_class from mapper class name' do
|
32
|
-
ModelMethodsSpec::TargetClassMapper.target_class.
|
32
|
+
expect(ModelMethodsSpec::TargetClassMapper.target_class).to eq ModelMethodsSpec::TargetClass
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should detect target_class from nearest ancestor when inherited' do
|
36
|
-
ModelMethodsSpec::InheritedClassMapper.target_class.
|
37
|
-
|
36
|
+
expect(ModelMethodsSpec::InheritedClassMapper.target_class.ancestors).
|
37
|
+
to include ModelMethodsSpec::TargetClass
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should use explicit class name if specified' do
|
41
|
-
ModelMethodsSpec::ExplicitNameMapper.target_class.
|
42
|
-
|
41
|
+
expect(ModelMethodsSpec::ExplicitNameMapper.target_class.ancestors).
|
42
|
+
to include ModelMethodsSpec::OtherTargetClass
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
describe '.build' do
|
47
47
|
it 'should use target class to build a new object for mapper' do
|
48
|
-
ModelMethodsSpec::TargetClassMapper.
|
49
|
-
should_receive(:new).
|
48
|
+
expect(ModelMethodsSpec::TargetClassMapper).to receive(:new).
|
50
49
|
with(kind_of(ModelMethodsSpec::TargetClass), :used_trait)
|
51
50
|
ModelMethodsSpec::TargetClassMapper.build(:used_trait)
|
52
51
|
end
|
@@ -56,8 +55,8 @@ module FlatMap
|
|
56
55
|
let(:target){ ModelMethodsSpec::TargetClass.new('a', 'b') }
|
57
56
|
|
58
57
|
it 'should delegate to target class to find object for mapper' do
|
59
|
-
ModelMethodsSpec::TargetClass.
|
60
|
-
ModelMethodsSpec::TargetClassMapper.
|
58
|
+
expect(ModelMethodsSpec::TargetClass).to receive(:find).with(1).and_return(target)
|
59
|
+
expect(ModelMethodsSpec::TargetClassMapper).to receive(:new).with(target, :used_trait)
|
61
60
|
ModelMethodsSpec::TargetClassMapper.find(1, :used_trait)
|
62
61
|
end
|
63
62
|
end
|
@@ -67,30 +66,30 @@ module FlatMap
|
|
67
66
|
let(:mapper){ ModelMethodsSpec::TargetClassMapper.new(target){} }
|
68
67
|
|
69
68
|
specify '#model_name' do
|
70
|
-
mapper.model_name.
|
69
|
+
expect(mapper.model_name).to eq 'mapper'
|
71
70
|
end
|
72
71
|
|
73
72
|
specify '#to_key should delegate to target' do
|
74
|
-
target.
|
75
|
-
mapper.to_key.
|
73
|
+
expect(target).to receive(:to_key).and_return(1)
|
74
|
+
expect(mapper.to_key).to eq 1
|
76
75
|
end
|
77
76
|
|
78
77
|
specify '#persisted? when target does not respond to :persised?' do
|
79
|
-
mapper.
|
78
|
+
expect(mapper).not_to be_persisted
|
80
79
|
end
|
81
80
|
|
82
81
|
specify '#persisted? when target responds to :persisted?' do
|
83
|
-
target.
|
84
|
-
mapper.
|
82
|
+
expect(target).to receive(:persisted?).and_return(true)
|
83
|
+
expect(mapper).to be_persisted
|
85
84
|
end
|
86
85
|
|
87
86
|
specify '#id when target does not respond to :id' do
|
88
|
-
mapper.id.
|
87
|
+
expect(mapper.id).to be_nil
|
89
88
|
end
|
90
89
|
|
91
90
|
specify '#id when target responds to :id' do
|
92
|
-
target.
|
93
|
-
mapper.id.
|
91
|
+
expect(target).to receive(:id).and_return(1)
|
92
|
+
expect(mapper.id).to eq 1
|
94
93
|
end
|
95
94
|
|
96
95
|
describe '#write with multiparams' do
|
@@ -103,23 +102,23 @@ module FlatMap
|
|
103
102
|
|
104
103
|
it 'should assign values properly' do
|
105
104
|
mapper.write(params)
|
106
|
-
target.attr_a.
|
107
|
-
target.attr_b.
|
105
|
+
expect(target.attr_a).to eq 'A'
|
106
|
+
expect(target.attr_b).to eq Date.new(1999, 1, 2)
|
108
107
|
end
|
109
108
|
end
|
110
109
|
|
111
110
|
describe '#save_target' do
|
112
111
|
it 'should return true for owned mappers' do
|
113
|
-
mapper.extension.save_target.
|
112
|
+
expect(mapper.extension.save_target).to be true
|
114
113
|
end
|
115
114
|
|
116
115
|
it 'should return true if target does not respond to #save' do
|
117
|
-
mapper.save_target.
|
116
|
+
expect(mapper.save_target).to be true
|
118
117
|
end
|
119
118
|
|
120
119
|
it 'should save with no validation if target responds to #save' do
|
121
|
-
target.
|
122
|
-
mapper.save_target.
|
120
|
+
expect(target).to receive(:save).with(:validate => false).and_return(true)
|
121
|
+
expect(mapper.save_target).to be true
|
123
122
|
end
|
124
123
|
end
|
125
124
|
|
@@ -127,28 +126,28 @@ module FlatMap
|
|
127
126
|
let(:params){{ :attr_a => 'A' }}
|
128
127
|
|
129
128
|
it 'should write params first' do
|
130
|
-
mapper.
|
131
|
-
ActiveRecord::Base.
|
129
|
+
expect(mapper).to receive(:write).with(params)
|
130
|
+
expect(ActiveRecord::Base).to receive(:transaction).and_yield
|
132
131
|
mapper.apply(params)
|
133
132
|
end
|
134
133
|
|
135
134
|
it 'should not save if not valid' do
|
136
|
-
mapper.
|
137
|
-
mapper.
|
135
|
+
expect(mapper).to receive(:valid?).and_return(false)
|
136
|
+
expect(mapper).not_to receive(:save)
|
138
137
|
mapper.apply(params)
|
139
138
|
end
|
140
139
|
|
141
140
|
it 'should save if valid' do
|
142
|
-
mapper.
|
143
|
-
ActiveRecord::Base.
|
144
|
-
mapper.
|
141
|
+
expect(mapper).to receive(:valid?).and_return(true)
|
142
|
+
expect(ActiveRecord::Base).to receive(:transaction).and_yield
|
143
|
+
expect(mapper).to receive(:save)
|
145
144
|
mapper.apply(params)
|
146
145
|
end
|
147
146
|
end
|
148
147
|
|
149
148
|
specify '#shallow_save saves target in a save callbacks' do
|
150
|
-
mapper.
|
151
|
-
mapper.
|
149
|
+
expect(mapper).to receive(:run_callbacks).with(:save).and_yield
|
150
|
+
expect(mapper).to receive(:save_target)
|
152
151
|
mapper.shallow_save
|
153
152
|
end
|
154
153
|
end
|