HornsAndHooves-flat_map 0.2.1 → 0.5.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 +5 -5
- data/.ruby-version +1 -1
- data/Gemfile +11 -14
- data/HornsAndHooves-flat_map.gemspec +6 -6
- data/lib/flat_map/model_mapper/persistence.rb +0 -7
- data/lib/flat_map/open_mapper/persistence.rb +1 -1
- 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
- data/spec/spec_helper.rb +6 -0
- metadata +30 -18
@@ -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 respond_to :human
|
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 respond_to :human
|
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
|
@@ -51,14 +51,14 @@ module FlatMap
|
|
51
51
|
describe 'Traits' do
|
52
52
|
describe 'trait definition' do
|
53
53
|
it "should add a traited mapper factory to a class" do
|
54
|
-
TraitsSpec::EmptyMapper.
|
54
|
+
expect(TraitsSpec::EmptyMapper).to receive(:mount).
|
55
55
|
with(kind_of(Class), :trait_name => :a_trait).
|
56
56
|
and_call_original
|
57
57
|
expect{ TraitsSpec::EmptyMapper.trait(:a_trait) }.
|
58
58
|
to change{ TraitsSpec::EmptyMapper.mountings.length }.by(1)
|
59
59
|
trait_mapper_class =
|
60
60
|
TraitsSpec::EmptyMapper.mountings.first.instance_variable_get('@identifier')
|
61
|
-
trait_mapper_class.name.
|
61
|
+
expect(trait_mapper_class.name).to eq 'FlatMap::TraitsSpec::EmptyMapperATraitTrait'
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -79,25 +79,25 @@ module FlatMap
|
|
79
79
|
end
|
80
80
|
|
81
81
|
it 'should be able to access trait by name' do
|
82
|
-
mapper.trait(:trait_one).
|
83
|
-
mapper.trait(:undefined).
|
82
|
+
expect(mapper.trait(:trait_one)).to be_a(Mapper)
|
83
|
+
expect(mapper.trait(:undefined)).to be_nil
|
84
84
|
end
|
85
85
|
|
86
86
|
it 'should not contain unused trait' do
|
87
|
-
mapper.trait(:trait_two).
|
87
|
+
expect(mapper.trait(:trait_two)).to be_nil
|
88
88
|
end
|
89
89
|
|
90
90
|
it 'should have mountings of a trait' do
|
91
|
-
mapper.mounting(:spec_mount).
|
91
|
+
expect(mapper.mounting(:spec_mount)).to be_present
|
92
92
|
end
|
93
93
|
|
94
94
|
it '#read should read values with respect to trait' do
|
95
|
-
mapper.read.
|
95
|
+
expect(mapper.read).to eq({
|
96
96
|
:attr_a => 'a',
|
97
97
|
:attr_b => 'b',
|
98
98
|
:attr_c => 'c',
|
99
99
|
:attr_d => 'd'
|
100
|
-
}
|
100
|
+
})
|
101
101
|
end
|
102
102
|
|
103
103
|
it '#write should properly distribute values' do
|
@@ -106,33 +106,33 @@ module FlatMap
|
|
106
106
|
:attr_b => 'B',
|
107
107
|
:attr_c => 'C',
|
108
108
|
:attr_d => 'D'
|
109
|
-
target.attr_a.
|
110
|
-
target.attr_b.
|
111
|
-
mount_target.attr_c.
|
112
|
-
mount_target.attr_d.
|
109
|
+
expect(target.attr_a ).to eq 'A'
|
110
|
+
expect(target.attr_b ).to eq 'B'
|
111
|
+
expect(mount_target.attr_c).to eq 'C'
|
112
|
+
expect(mount_target.attr_d).to eq 'D'
|
113
113
|
end
|
114
114
|
|
115
115
|
specify 'mapper should be avle to call methods of enabled traits' do
|
116
116
|
mapper = TraitsSpec::HostMapper.new(target, :trait_one)
|
117
|
-
mapper.method_one.
|
117
|
+
expect(mapper.method_one).to eq 'one'
|
118
118
|
expect{ mapper.method_two }.to raise_error(NoMethodError)
|
119
119
|
end
|
120
120
|
|
121
121
|
specify 'traits should be able to call methods of each other' do
|
122
122
|
mapper = TraitsSpec::HostMapper.new(target, :trait_one, :trait_two)
|
123
|
-
mapper.trait(:trait_two).method_two.
|
123
|
+
expect(mapper.trait(:trait_two).method_two).to eq 'one'
|
124
124
|
end
|
125
125
|
|
126
126
|
describe 'trait nesting' do
|
127
127
|
let(:mapper){ TraitsSpec::HostMapper.new(target, :trait_one_nested) }
|
128
128
|
|
129
129
|
it 'should still be able to have top-level trait definitions' do
|
130
|
-
mapper.mounting(:spec_mount).
|
131
|
-
mapper.method_one.
|
130
|
+
expect(mapper.mounting(:spec_mount)).to be_present
|
131
|
+
expect(mapper.method_one).to eq 'one'
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'should have new definitions' do
|
135
|
-
mapper.method_one_nested.
|
135
|
+
expect(mapper.method_one_nested).to eq 'nested_one'
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
@@ -150,21 +150,21 @@ module FlatMap
|
|
150
150
|
end
|
151
151
|
|
152
152
|
it 'should behave like a normal trait' do
|
153
|
-
mapper.trait(:extension).
|
154
|
-
mapper.read.
|
153
|
+
expect(mapper.trait(:extension)).to be_present
|
154
|
+
expect(mapper.read).to include :attr_b => 'b'
|
155
155
|
end
|
156
156
|
|
157
157
|
it 'should be accessible' do
|
158
|
-
mapper.extension.
|
158
|
+
expect(mapper.extension).to be_present
|
159
159
|
end
|
160
160
|
|
161
161
|
it 'should be_extension' do
|
162
|
-
mapper.extension.
|
162
|
+
expect(mapper.extension).to be_extension
|
163
163
|
end
|
164
164
|
|
165
165
|
it "should be able to handle save exception of traits" do
|
166
166
|
expect{ mapper.apply(:writing_error => 'foo') }.not_to raise_error
|
167
|
-
mapper.errors[:writing_error].
|
167
|
+
expect(mapper.errors[:writing_error]).to include 'cannot be foo'
|
168
168
|
end
|
169
169
|
end
|
170
170
|
end
|
@@ -52,21 +52,21 @@ module FlatMap
|
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should not be valid' do
|
55
|
-
mapper.
|
55
|
+
expect(mapper).not_to be_valid
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'should call callbacks' do
|
59
|
-
mapper.trait(:with_trait).
|
59
|
+
expect(mapper.trait(:with_trait)).to receive(:set_default_attr_b).and_call_original
|
60
60
|
mapper.valid?
|
61
|
-
mapper.attr_b.
|
61
|
+
expect(mapper.attr_b).to eq 'foo'
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'should have all the errors' do
|
65
65
|
mapper.valid?
|
66
|
-
mapper.errors[:attr_a].
|
67
|
-
mapper.errors[:attr_b].
|
68
|
-
mapper.errors[:attr_c].
|
69
|
-
mapper.errors[:attr_d].
|
66
|
+
expect(mapper.errors[:attr_a]).to eq ["can't be blank"]
|
67
|
+
expect(mapper.errors[:attr_b]).to eq ["can't be foo"]
|
68
|
+
expect(mapper.errors[:attr_c]).to eq ["can't be blank"]
|
69
|
+
expect(mapper.errors[:attr_d]).to eq ["can't be blank"]
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
@@ -5,7 +5,7 @@ describe FlatMap::Mapping::Factory do
|
|
5
5
|
|
6
6
|
specify('#create should delegate all initialization params to new mapping') do
|
7
7
|
mapper_stub = double('mapper')
|
8
|
-
FlatMap::Mapping.
|
8
|
+
expect(FlatMap::Mapping).to receive(:new).with(mapper_stub, :foo, :bar, :baz)
|
9
9
|
|
10
10
|
factory.create(mapper_stub)
|
11
11
|
end
|