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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7902561902733c34e2f37b66ae25ed17fded33ff38f6f0f013630137a87eacb6
|
4
|
+
data.tar.gz: ba3167200d89dad368a42d23dbe4bebcfc147f15879ef8a78835a609127aa20c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44f53062529bafb8ae96504dbaa6576e6e44c453749a01012c37a723726aa459a31f1f097e3e364a47ad1453d1609e1da7e87099d4cd27d0f2a2d8f365463bff
|
7
|
+
data.tar.gz: 5d8861f359ea9c460e0cbac177726143cf87b4d18299f43052a845fb0c74faf2aef483e828bbd7be06160a5d9c7719c6cd55c64c505f0a9d99fede9bf142feaa
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.6.6
|
data/Gemfile
CHANGED
@@ -3,19 +3,16 @@ source "https://rubygems.org"
|
|
3
3
|
# Specify your gem's dependencies in flat_map.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
gem 'yard'
|
9
|
-
end
|
6
|
+
gem "activerecord"
|
7
|
+
gem "activesupport"
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
gem "metric_fu"
|
14
|
-
gem "pry"
|
15
|
-
end
|
9
|
+
gem 'redcarpet'
|
10
|
+
gem 'yard'
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
# code metrics:
|
13
|
+
gem "metric_fu"
|
14
|
+
gem "pry"
|
15
|
+
|
16
|
+
gem 'colorize' , :require => false
|
17
|
+
gem 'simplecov' , :require => false
|
18
|
+
gem 'simplecov-rcov-text', :require => false
|
@@ -5,8 +5,8 @@ require "flat_map/version"
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "HornsAndHooves-flat_map"
|
7
7
|
s.version = FlatMap::VERSION
|
8
|
-
s.authors = ["HornsAndHooves", "Artem Kuzko", "Zachary Belzer", "Sergey Potapov"]
|
9
|
-
s.email = ["a.kuzko@gmail.com", "zbelzer@gmail.com", "blake131313@gmail.com"]
|
8
|
+
s.authors = ["HornsAndHooves", "Artem Kuzko", "Zachary Belzer", "Sergey Potapov", "Bruce Burdick"]
|
9
|
+
s.email = ["a.kuzko@gmail.com", "zbelzer@gmail.com", "blake131313@gmail.com", "rubygems.org@bruceburdick.com"]
|
10
10
|
s.homepage = "https://github.com/HornsAndHooves/flat_map"
|
11
11
|
s.licenses = ["LICENSE"]
|
12
12
|
s.summary = %q{Deep object graph to a plain properties mapper}
|
@@ -19,11 +19,11 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
|
-
s.add_dependency
|
23
|
-
s.add_dependency
|
24
|
-
s.add_dependency
|
22
|
+
s.add_dependency "activesupport", ">= 5", "< 7.0.0"
|
23
|
+
s.add_dependency "activerecord", ">= 5", "< 7.0.0"
|
24
|
+
s.add_dependency "yard"
|
25
25
|
|
26
|
-
s.add_development_dependency "rspec",
|
26
|
+
s.add_development_dependency "rspec", '~> 3.5'
|
27
27
|
s.add_development_dependency "rspec-its"
|
28
28
|
s.add_development_dependency "rake"
|
29
29
|
end
|
@@ -55,13 +55,6 @@ module FlatMap
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
# Return a 'mapper' string as a model_name. Used by Rails FormBuilder.
|
59
|
-
#
|
60
|
-
# @return [String]
|
61
|
-
def model_name
|
62
|
-
'mapper'
|
63
|
-
end
|
64
|
-
|
65
58
|
# Delegate to the target's #to_key method.
|
66
59
|
# @return [String]
|
67
60
|
def to_key
|
@@ -133,7 +133,7 @@ module FlatMap
|
|
133
133
|
# @return [Array<ActiveModel::Errors>]
|
134
134
|
def consolidate_errors!
|
135
135
|
mountings.map(&:errors).each do |errs|
|
136
|
-
errors.
|
136
|
+
errors.merge!(errs)
|
137
137
|
end
|
138
138
|
end
|
139
139
|
private :consolidate_errors!
|
data/lib/flat_map/version.rb
CHANGED
@@ -11,13 +11,13 @@ describe FlatMap::Errors do
|
|
11
11
|
|
12
12
|
it "preserved errors should appear on #empty? call exactly once" do
|
13
13
|
errors.preserve :base, 'an error'
|
14
|
-
errors.
|
15
|
-
errors[:base].
|
14
|
+
expect(errors).not_to be_empty
|
15
|
+
expect(errors[:base]).to eq ['an error']
|
16
16
|
expect{ errors.empty? }.not_to change{ errors[:base].length }
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should add error to mapper with suffix" do
|
20
20
|
errors.add(:attr, 'an error')
|
21
|
-
errors[:attr_foo].
|
21
|
+
expect(errors[:attr_foo]).to eq ['an error']
|
22
22
|
end
|
23
23
|
end
|
@@ -8,8 +8,8 @@ module FlatMap
|
|
8
8
|
end
|
9
9
|
|
10
10
|
describe 'Attribute Methods' do
|
11
|
-
let(:target){ OpenStruct.new }
|
12
|
-
let(:mapper){ AttributeMethodsSpec::SpecMapper.new(target) }
|
11
|
+
let(:target) { OpenStruct.new }
|
12
|
+
let(:mapper) { AttributeMethodsSpec::SpecMapper.new(target) }
|
13
13
|
|
14
14
|
before do
|
15
15
|
target.attr_a = 'a'
|
@@ -17,27 +17,27 @@ module FlatMap
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'correctly responds to dynamic methods' do
|
20
|
-
mapper.respond_to
|
21
|
-
mapper.method(:attr_a=).
|
20
|
+
expect(mapper).to respond_to(:attr_a=)
|
21
|
+
expect(mapper.method(:attr_a=)).not_to be_nil
|
22
22
|
|
23
|
-
mapper.respond_to
|
24
|
-
mapper.method(:attr_b=).
|
23
|
+
expect(mapper).to respond_to(:attr_b=)
|
24
|
+
expect(mapper.method(:attr_b=)).not_to be_nil
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'should be able to read values via method calls' do
|
28
|
-
mapper.attr_a.
|
29
|
-
mapper.attr_b.
|
28
|
+
expect(mapper.attr_a).to eq 'a'
|
29
|
+
expect(mapper.attr_b).to eq 'b'
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should be able to write values via method calls' do
|
33
33
|
mapper.attr_a = 'A'
|
34
34
|
mapper.attr_b = 'B'
|
35
|
-
target.attr_a.
|
36
|
-
target.attr_b.
|
35
|
+
expect(target.attr_a).to eq 'A'
|
36
|
+
expect(target.attr_b).to eq 'B'
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should still raise for unknown or private method calls' do
|
40
|
-
expect{ mapper.undefined_method
|
40
|
+
expect{ mapper.undefined_method }.to raise_error(NoMethodError)
|
41
41
|
expect{ mapper.attribute_methods }.to raise_error(NoMethodError)
|
42
42
|
end
|
43
43
|
end
|
@@ -49,27 +49,27 @@ module FlatMap
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'should call callbacks once' do
|
52
|
-
mapper.
|
52
|
+
expect(mapper).to receive(:set_a).once
|
53
53
|
mapper.save
|
54
54
|
end
|
55
55
|
|
56
56
|
specify 'validation callbacks' do
|
57
57
|
mapper.valid?
|
58
|
-
mapper.attr_a.
|
59
|
-
mapper.attr_b.
|
60
|
-
mapper.attr_c.
|
58
|
+
expect(mapper.attr_a).to be_nil
|
59
|
+
expect(mapper.attr_b).to eq 'before validate'
|
60
|
+
expect(mapper.attr_c).to eq 'mounted before validate'
|
61
61
|
end
|
62
62
|
|
63
63
|
specify 'save callbacks' do
|
64
64
|
mapper.save
|
65
|
-
mapper.attr_a.
|
65
|
+
expect(mapper.attr_a).to eq 'before save'
|
66
66
|
end
|
67
67
|
|
68
68
|
context 'extension trait and named traits' do
|
69
69
|
it 'should process extension first' do
|
70
|
-
mapper.extension.
|
70
|
+
expect(mapper.extension).to receive(:extension_set_b).once.and_call_original
|
71
71
|
mapper.valid?
|
72
|
-
mapper.attr_b.
|
72
|
+
expect(mapper.attr_b).to eq 'before validate'
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -2,40 +2,40 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module FlatMap
|
4
4
|
describe OpenMapper::Factory do
|
5
|
-
let(:trait_class){ Class.new(FlatMap::OpenMapper) }
|
6
|
-
let(:mapper){ OpenMapper.build }
|
5
|
+
let(:trait_class) { Class.new(FlatMap::OpenMapper) }
|
6
|
+
let(:mapper) { OpenMapper.build }
|
7
7
|
|
8
|
-
let(:mount_factory){ OpenMapper::Factory.new(:spec_mount, :traits => :used_traits) }
|
9
|
-
let(:trait_factory){ OpenMapper::Factory.new(trait_class, :trait_name => :a_trait) }
|
10
|
-
let(:open_factory){ OpenMapper::Factory.new(:some_mount, :open => true) }
|
8
|
+
let(:mount_factory) { OpenMapper::Factory.new(:spec_mount, :traits => :used_traits) }
|
9
|
+
let(:trait_factory) { OpenMapper::Factory.new(trait_class, :trait_name => :a_trait) }
|
10
|
+
let(:open_factory) { OpenMapper::Factory.new(:some_mount, :open => true) }
|
11
11
|
|
12
12
|
context 'when used for a trait' do
|
13
13
|
subject{ trait_factory }
|
14
14
|
|
15
|
-
it{
|
16
|
-
its(:name){
|
17
|
-
its(:trait_name){
|
18
|
-
its(:mapper_class){
|
15
|
+
it{ is_expected.to be_traited }
|
16
|
+
its(:name) { is_expected.to be_nil }
|
17
|
+
its(:trait_name) { is_expected.to eq :a_trait }
|
18
|
+
its(:mapper_class) { is_expected.to eq trait_class }
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'when used for a mounted mapper' do
|
22
22
|
subject{ mount_factory }
|
23
23
|
|
24
|
-
it{
|
25
|
-
its(:name){
|
26
|
-
its(:trait_name){
|
27
|
-
its(:traits){
|
24
|
+
it{ is_expected.not_to be_traited }
|
25
|
+
its(:name) { is_expected.to eq :spec_mount }
|
26
|
+
its(:trait_name) { is_expected.to be_nil }
|
27
|
+
its(:traits) { is_expected.to eq [:used_traits] }
|
28
28
|
end
|
29
29
|
|
30
30
|
context 'when used for an open mapper' do
|
31
31
|
it "should have descendant of OpenMapper as mapper_class" do
|
32
|
-
open_factory.mapper_class.
|
33
|
-
open_factory.mapper_class.name.
|
32
|
+
expect(open_factory.mapper_class.ancestors).to include(OpenMapper)
|
33
|
+
expect(open_factory.mapper_class.name).to eq 'SomeMountMapper'
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should create and instance of OpenMapper with open struct as a target" do
|
37
37
|
mounted = open_factory.create(mapper)
|
38
|
-
mounted.target.
|
38
|
+
expect(mounted.target).to be_a(OpenStruct)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -48,7 +48,7 @@ module FlatMap
|
|
48
48
|
class OpenMapper::Factory::SpecMountMapper < FlatMap::OpenMapper; end
|
49
49
|
|
50
50
|
it "should be able to fetch class name from name" do
|
51
|
-
mount_factory.mapper_class.
|
51
|
+
expect(mount_factory.mapper_class.ancestors).to include(::SpecMountMapper)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should be able to fetch class from mapper_class" do
|
@@ -56,7 +56,8 @@ module FlatMap
|
|
56
56
|
:spec_mount,
|
57
57
|
:mapper_class => FlatMap::OpenMapper::Factory::SpecMountMapper
|
58
58
|
)
|
59
|
-
factory.mapper_class.
|
59
|
+
expect(factory.mapper_class.ancestors).
|
60
|
+
to include(::FlatMap::OpenMapper::Factory::SpecMountMapper)
|
60
61
|
end
|
61
62
|
|
62
63
|
it "should use options if specified" do
|
@@ -64,38 +65,39 @@ module FlatMap
|
|
64
65
|
:spec_mount,
|
65
66
|
:mapper_class_name => 'FlatMap::OpenMapper::Factory::SpecMountMapper'
|
66
67
|
)
|
67
|
-
factory.mapper_class.
|
68
|
+
expect(factory.mapper_class.ancestors).
|
69
|
+
to include(::FlatMap::OpenMapper::Factory::SpecMountMapper)
|
68
70
|
end
|
69
71
|
end
|
70
72
|
|
71
73
|
describe '#fetch_target_from' do
|
72
74
|
it "should return owner's target for traited factory" do
|
73
|
-
trait_factory.fetch_target_from(mapper).
|
75
|
+
expect(trait_factory.fetch_target_from(mapper)).to eq target
|
74
76
|
end
|
75
77
|
|
76
78
|
context 'explicit target' do
|
77
79
|
it "should use explicitly specified if applicable" do
|
78
80
|
factory = OpenMapper::Factory.new(:spec_mount, :target => other_target)
|
79
|
-
factory.fetch_target_from(mapper).
|
81
|
+
expect(factory.fetch_target_from(mapper)).to eq other_target
|
80
82
|
end
|
81
83
|
|
82
84
|
it "should call Proc and pass owner target to it if Proc is specified as :target" do
|
83
85
|
factory = OpenMapper::Factory.new(:spec_mount, :target => lambda{ |obj| obj.foo })
|
84
|
-
target.
|
85
|
-
factory.fetch_target_from(mapper).
|
86
|
+
expect(target).to receive(:foo).and_return(other_target)
|
87
|
+
expect(factory.fetch_target_from(mapper)).to eq other_target
|
86
88
|
end
|
87
89
|
|
88
90
|
it "should call a method if Symbol is used" do
|
89
91
|
factory = OpenMapper::Factory.new(:spec_mount, :target => :foo)
|
90
|
-
mapper.
|
91
|
-
factory.fetch_target_from(mapper).
|
92
|
+
expect(mapper).to receive(:foo).and_return(other_target)
|
93
|
+
expect(factory.fetch_target_from(mapper)).to eq other_target
|
92
94
|
end
|
93
95
|
end
|
94
96
|
|
95
97
|
context 'target from association' do
|
96
98
|
before do
|
97
|
-
target.
|
98
|
-
target.
|
99
|
+
allow(target).to receive(:is_a?).and_call_original
|
100
|
+
expect(target).to receive(:is_a?).with(ActiveRecord::Base).and_return(true)
|
99
101
|
end
|
100
102
|
|
101
103
|
let(:has_one_current_reflection) {
|
@@ -113,68 +115,68 @@ module FlatMap
|
|
113
115
|
|
114
116
|
it "should refer to effective name for has_one_current association" do
|
115
117
|
# Note: has_one_current is not part of Rails
|
116
|
-
mount_factory.
|
118
|
+
expect(mount_factory).to receive(:reflection_from_target).
|
117
119
|
with(target).
|
118
120
|
and_return(has_one_current_reflection)
|
119
|
-
target.
|
120
|
-
mount_factory.fetch_target_from(mapper).
|
121
|
+
expect(target).to receive(:effective_spec_mount).and_return(other_target)
|
122
|
+
expect(mount_factory.fetch_target_from(mapper)).to eq other_target
|
121
123
|
end
|
122
124
|
|
123
125
|
it "should refer to existing association object if possible, " \
|
124
126
|
"and build it if it is absent for :has_one" do
|
125
|
-
mount_factory.
|
127
|
+
expect(mount_factory).to receive(:reflection_from_target).
|
126
128
|
with(target).
|
127
129
|
and_return(has_one_reflection)
|
128
|
-
target.
|
129
|
-
target.
|
130
|
-
mount_factory.fetch_target_from(mapper).
|
130
|
+
expect(target).to receive(:spec_mount).and_return(nil)
|
131
|
+
expect(target).to receive(:build_spec_mount).and_return(other_target)
|
132
|
+
expect(mount_factory.fetch_target_from(mapper)).to eq other_target
|
131
133
|
end
|
132
134
|
|
133
135
|
it "should refer to existing association object if possible, " \
|
134
136
|
"and build it if it is absent for :belongs_to" do
|
135
|
-
mount_factory.
|
137
|
+
expect(mount_factory).to receive(:reflection_from_target).
|
136
138
|
with(target).
|
137
139
|
and_return(belongs_to_reflection)
|
138
|
-
target.
|
139
|
-
target.
|
140
|
-
mount_factory.fetch_target_from(mapper).
|
140
|
+
expect(target).to receive(:spec_mount).and_return(nil)
|
141
|
+
expect(target).to receive(:build_spec_mount).and_return(other_target)
|
142
|
+
expect(mount_factory.fetch_target_from(mapper)).to eq other_target
|
141
143
|
end
|
142
144
|
|
143
145
|
it "should always build a new record for :has_many association" do
|
144
|
-
mount_factory.
|
146
|
+
expect(mount_factory).to receive(:reflection_from_target).
|
145
147
|
with(target).
|
146
148
|
and_return(has_many_reflection)
|
147
|
-
target.
|
148
|
-
target.
|
149
|
-
mount_factory.fetch_target_from(mapper).
|
149
|
+
expect(target).to receive(:association).with(:spec_mounts)
|
150
|
+
allow(target).to receive_message_chain(:association, :build).and_return(other_target)
|
151
|
+
expect(mount_factory.fetch_target_from(mapper)).to eq other_target
|
150
152
|
end
|
151
153
|
|
152
154
|
describe 'reflection_from_target' do
|
153
|
-
before{ target.
|
155
|
+
before{ allow(target).to receive(:is_a?).with(ActiveRecord::Base).and_return(true) }
|
154
156
|
|
155
157
|
it 'should first refer to singular association' do
|
156
|
-
target.
|
158
|
+
allow(target).to receive_message_chain(:class, :reflect_on_association).
|
157
159
|
with(:spec_mount).
|
158
160
|
and_return(has_one_reflection)
|
159
|
-
mount_factory.reflection_from_target(target).
|
161
|
+
expect(mount_factory.reflection_from_target(target)).to eq has_one_reflection
|
160
162
|
end
|
161
163
|
|
162
164
|
it 'should use collection association if singular does not exist' do
|
163
|
-
target.
|
165
|
+
allow(target).to receive_message_chain(:class, :reflect_on_association).
|
164
166
|
with(:spec_mount).
|
165
167
|
and_return(nil)
|
166
|
-
target.
|
168
|
+
allow(target).to receive_message_chain(:class, :reflect_on_association).
|
167
169
|
with(:spec_mounts).
|
168
170
|
and_return(has_many_reflection)
|
169
|
-
mount_factory.reflection_from_target(target).
|
171
|
+
expect(mount_factory.reflection_from_target(target)).to eq has_many_reflection
|
170
172
|
end
|
171
173
|
end
|
172
174
|
end
|
173
175
|
|
174
176
|
context 'target from name' do
|
175
177
|
it 'should simply send method to owner target' do
|
176
|
-
target.
|
177
|
-
mount_factory.fetch_target_from(mapper).
|
178
|
+
expect(target).to receive(:spec_mount).and_return(other_target)
|
179
|
+
expect(mount_factory.fetch_target_from(mapper)).to eq other_target
|
178
180
|
end
|
179
181
|
end
|
180
182
|
end
|
@@ -182,7 +184,7 @@ module FlatMap
|
|
182
184
|
describe '#create' do
|
183
185
|
specify 'traited factory should create an owned mapper' do
|
184
186
|
new_one = trait_factory.create(mapper)
|
185
|
-
new_one.owner.
|
187
|
+
expect(new_one.owner).to eq mapper
|
186
188
|
end
|
187
189
|
|
188
190
|
context 'mounted mapper' do
|
@@ -190,12 +192,12 @@ module FlatMap
|
|
190
192
|
let(:factory){ mount_factory }
|
191
193
|
|
192
194
|
before do
|
193
|
-
factory.
|
194
|
-
factory.
|
195
|
+
allow(factory).to receive(:mapper_class ).and_return(mount_class)
|
196
|
+
allow(factory).to receive(:fetch_target_from).and_return(other_target)
|
195
197
|
end
|
196
198
|
|
197
199
|
it 'should combine traits' do
|
198
|
-
mount_class.
|
200
|
+
expect(mount_class).to receive(:new).
|
199
201
|
with(other_target, :used_traits, :another_trait).
|
200
202
|
and_call_original
|
201
203
|
factory.create(mapper, :another_trait)
|
@@ -203,10 +205,10 @@ module FlatMap
|
|
203
205
|
|
204
206
|
it 'should properly set properties' do
|
205
207
|
new_one = factory.create(mapper)
|
206
|
-
new_one.host .
|
207
|
-
new_one.name .
|
208
|
-
new_one.save_order.
|
209
|
-
new_one.suffix.
|
208
|
+
expect(new_one.host ).to eq mapper
|
209
|
+
expect(new_one.name ).to eq :spec_mount
|
210
|
+
expect(new_one.save_order).to eq :after
|
211
|
+
expect(new_one.suffix ).to be_nil
|
210
212
|
end
|
211
213
|
|
212
214
|
context 'when suffix is defined' do
|
@@ -214,8 +216,8 @@ module FlatMap
|
|
214
216
|
|
215
217
|
it "should adjust properties with suffix" do
|
216
218
|
new_one = factory.create(mapper)
|
217
|
-
new_one.name .
|
218
|
-
new_one.suffix.
|
219
|
+
expect(new_one.name ).to eq :spec_mount_foo
|
220
|
+
expect(new_one.suffix).to eq :foo
|
219
221
|
end
|
220
222
|
end
|
221
223
|
|
@@ -224,7 +226,7 @@ module FlatMap
|
|
224
226
|
let(:factory){ OpenMapper::Factory.new(:spec_mount, &extension) }
|
225
227
|
|
226
228
|
it "should pass it to mapper initialization" do
|
227
|
-
mount_class.
|
229
|
+
expect(mount_class).to receive(:new).
|
228
230
|
with(other_target, &extension).
|
229
231
|
and_call_original
|
230
232
|
new_one = factory.create(mapper)
|
@@ -233,20 +235,20 @@ module FlatMap
|
|
233
235
|
|
234
236
|
describe 'save order' do
|
235
237
|
before do
|
236
|
-
mapper.
|
237
|
-
mapper.
|
238
|
+
allow(mapper).to receive(:is_a?).and_call_original
|
239
|
+
allow(mapper).to receive(:is_a?).with(ModelMapper).and_return(true)
|
238
240
|
end
|
239
241
|
|
240
242
|
it 'should be :before for belongs_to association' do
|
241
|
-
factory.
|
243
|
+
expect(factory).to receive(:reflection_from_target).
|
242
244
|
and_return(double('reflection', :macro => :belongs_to))
|
243
|
-
factory.fetch_save_order(mapper).
|
245
|
+
expect(factory.fetch_save_order(mapper)).to eq :before
|
244
246
|
end
|
245
247
|
|
246
248
|
it 'should be :after for other cases' do
|
247
|
-
factory.
|
249
|
+
expect(factory).to receive(:reflection_from_target).
|
248
250
|
and_return(double('reflection', :macro => :has_one))
|
249
|
-
factory.fetch_save_order(mapper).
|
251
|
+
expect(factory.fetch_save_order(mapper)).to eq :after
|
250
252
|
end
|
251
253
|
|
252
254
|
context 'when explicitly set' do
|
@@ -254,7 +256,7 @@ module FlatMap
|
|
254
256
|
|
255
257
|
it 'should fetch from options, if possible' do
|
256
258
|
new_one = factory.create(mapper)
|
257
|
-
new_one.save_order.
|
259
|
+
expect(new_one.save_order).to eq :before
|
258
260
|
end
|
259
261
|
end
|
260
262
|
end
|
@@ -273,11 +275,11 @@ module FlatMap
|
|
273
275
|
let(:factory_for_b){ mapper_class.mountings.first.mapper_class.mountings.first }
|
274
276
|
|
275
277
|
it "should be required for nested trait" do
|
276
|
-
factory_for_b.required_for_any_trait?([:trait_c]).
|
278
|
+
expect(factory_for_b.required_for_any_trait?([:trait_c])).to be true
|
277
279
|
end
|
278
280
|
|
279
281
|
it "should not be required for top trait" do
|
280
|
-
factory_for_b.required_for_any_trait?([:trait_a]).
|
282
|
+
expect(factory_for_b.required_for_any_trait?([:trait_a])).to be false
|
281
283
|
end
|
282
284
|
end
|
283
285
|
end
|
@@ -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
|