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
@@ -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
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Reader::Basic do
|
4
|
-
let(:target){ double('target') }
|
5
|
-
let(:mapping){ double('mapping') }
|
6
|
-
let(:reader){ described_class.new(mapping) }
|
4
|
+
let(:target) { double('target') }
|
5
|
+
let(:mapping) { double('mapping') }
|
6
|
+
let(:reader) { described_class.new(mapping) }
|
7
7
|
|
8
8
|
specify("#read should fetch value from mapping's target_attribute") do
|
9
|
-
mapping.
|
10
|
-
mapping.
|
11
|
-
target.
|
9
|
+
expect(mapping).to receive(:target ).and_return(target)
|
10
|
+
expect(mapping).to receive(:target_attribute).and_return(:foo)
|
11
|
+
expect(target ).to receive(:foo ).and_return(:bar)
|
12
12
|
|
13
|
-
reader.read.
|
13
|
+
expect(reader.read).to eq :bar
|
14
14
|
end
|
15
15
|
end
|
@@ -20,9 +20,9 @@ describe FlatMap::Mapping::Reader::Formatted do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
specify "#read should use formatting method for fetching a value" do
|
23
|
-
reader.read.
|
24
|
-
reader.read(:downcase).
|
25
|
-
reader.read(:unknown).
|
23
|
+
expect(reader.read ).to eq 'FOOBAR'
|
24
|
+
expect(reader.read(:downcase)).to eq 'foobar'
|
25
|
+
expect(reader.read(:unknown )).to eq 'fOoBaR'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -32,9 +32,9 @@ describe FlatMap::Mapping::Reader::Formatted do
|
|
32
32
|
|
33
33
|
it "should use I18n_l to format value" do
|
34
34
|
formatted_value = "le FooBar"
|
35
|
-
I18n.
|
35
|
+
expect(I18n).to receive(:l).with(value).and_return(formatted_value)
|
36
36
|
|
37
|
-
reader.read.
|
37
|
+
expect(reader.read).to eq formatted_value
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -49,13 +49,13 @@ describe FlatMap::Mapping::Reader::Formatted do
|
|
49
49
|
let(:reader){ described_class.new(mapping, :enum) }
|
50
50
|
|
51
51
|
it "should use the name property of the target object for value" do
|
52
|
-
enum.
|
53
|
-
reader.read.
|
52
|
+
expect(enum).to receive(:name).and_return(value)
|
53
|
+
expect(reader.read).to eq value
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should be able to use the desired method to get enum's property" do
|
57
|
-
enum.
|
58
|
-
reader.read(:description).
|
57
|
+
expect(enum).to receive(:description).and_return(value)
|
58
|
+
expect(reader.read(:description)).to eq value
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Reader::Method do
|
4
|
-
let(:mapper){ double('mapper') }
|
5
|
-
let(:mapping){ double('mapping', :mapper => mapper) }
|
6
|
-
let(:reader){ described_class.new(mapping, :read_with_method) }
|
4
|
+
let(:mapper) { double('mapper') }
|
5
|
+
let(:mapping) { double('mapping', :mapper => mapper) }
|
6
|
+
let(:reader) { described_class.new(mapping, :read_with_method) }
|
7
7
|
|
8
8
|
specify("#read delegates to mapper-defined method and passes mapping to it") do
|
9
|
-
mapper.
|
9
|
+
expect(mapper).to receive(:read_with_method).with(mapping).and_return(:bar)
|
10
10
|
|
11
|
-
reader.read.
|
11
|
+
expect(reader.read).to eq :bar
|
12
12
|
end
|
13
13
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Reader::Proc do
|
4
|
-
let(:target){ double('target') }
|
5
|
-
let(:mapping){ double('mapping', :target => target) }
|
6
|
-
let(:reader){ described_class.new(mapping, lambda{ |t| t.foo }) }
|
4
|
+
let(:target) { double('target') }
|
5
|
+
let(:mapping) { double('mapping', :target => target) }
|
6
|
+
let(:reader) { described_class.new(mapping, lambda{ |t| t.foo }) }
|
7
7
|
|
8
8
|
specify("#read should pass target to Proc object to fetch value") do
|
9
|
-
target.
|
9
|
+
expect(target).to receive(:foo).and_return(:bar)
|
10
10
|
|
11
|
-
reader.read.
|
11
|
+
expect(reader.read).to eq :bar
|
12
12
|
end
|
13
13
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Writer::Basic do
|
4
|
-
let(:target){ double('target') }
|
5
|
-
let(:mapping){ double('mapping') }
|
6
|
-
let(:writer){ described_class.new(mapping) }
|
4
|
+
let(:target) { double('target') }
|
5
|
+
let(:mapping) { double('mapping') }
|
6
|
+
let(:writer) { described_class.new(mapping) }
|
7
7
|
|
8
8
|
specify("#write use target_attribute as writer to assign value to target") do
|
9
|
-
mapping.
|
10
|
-
mapping.
|
11
|
-
target.
|
9
|
+
expect(mapping).to receive(:target ).and_return(target)
|
10
|
+
expect(mapping).to receive(:target_attribute).and_return(:foo)
|
11
|
+
expect(target ).to receive(:foo=).with(:bar)
|
12
12
|
|
13
13
|
writer.write(:bar)
|
14
14
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Writer::Method do
|
4
|
-
let(:mapper){ double('mapper') }
|
5
|
-
let(:mapping){ double('mapping', :mapper => mapper) }
|
6
|
-
let(:writer){ described_class.new(mapping, :write_with_method) }
|
4
|
+
let(:mapper) { double('mapper') }
|
5
|
+
let(:mapping) { double('mapping', :mapper => mapper) }
|
6
|
+
let(:writer) { described_class.new(mapping, :write_with_method) }
|
7
7
|
|
8
8
|
specify("#write delegates to mapper-defined method and passes mapping and value to it") do
|
9
|
-
mapper.
|
9
|
+
expect(mapper).to receive(:write_with_method).with(mapping, :foo)
|
10
10
|
|
11
11
|
writer.write(:foo)
|
12
12
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FlatMap::Mapping::Writer::Proc do
|
4
|
-
let(:target){ double('target') }
|
5
|
-
let(:mapping){ double('mapping', :target => target) }
|
6
|
-
let(:writer){ described_class.new(mapping, lambda{ |t, v| t.foo(v) }) }
|
4
|
+
let(:target) { double('target') }
|
5
|
+
let(:mapping) { double('mapping', :target => target) }
|
6
|
+
let(:writer) { described_class.new(mapping, lambda{ |t, v| t.foo(v) }) }
|
7
7
|
|
8
8
|
specify("#write should pass target and value to Proc object for assignment") do
|
9
|
-
target.
|
9
|
+
expect(target).to receive(:foo).with(:bar)
|
10
10
|
|
11
11
|
writer.write(:bar)
|
12
12
|
end
|
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module FlatMap
|
4
4
|
describe Mapping do
|
5
|
-
let(:mapper){ double('mapper', :suffixed? => false) }
|
6
|
-
let(:mapping){ Mapping.new(mapper, :name, :attr) }
|
5
|
+
let(:mapper) { double('mapper', :suffixed? => false) }
|
6
|
+
let(:mapping) { Mapping.new(mapper, :name, :attr) }
|
7
7
|
|
8
8
|
describe "initialization" do
|
9
9
|
it "should be initializable" do
|
@@ -11,12 +11,12 @@ module FlatMap
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'should delegate #read to reader' do
|
14
|
-
mapping.reader.
|
14
|
+
expect(mapping.reader).to receive(:read)
|
15
15
|
mapping.read
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'should delegate #write to writer' do
|
19
|
-
mapping.writer.
|
19
|
+
expect(mapping.writer).to receive(:write).with(:foo)
|
20
20
|
mapping.write(:foo)
|
21
21
|
end
|
22
22
|
|
@@ -25,43 +25,43 @@ module FlatMap
|
|
25
25
|
|
26
26
|
subject{ Mapping.new(mapper, :name, :attr, :multiparam => Date) }
|
27
27
|
|
28
|
-
its(:mapper){
|
29
|
-
its(:name){
|
30
|
-
its(:target_attribute){
|
31
|
-
its(:full_name){
|
32
|
-
its(:multiparam){
|
33
|
-
its(:multiparam?){
|
28
|
+
its(:mapper) { is_expected.to eq mapper }
|
29
|
+
its(:name) { is_expected.to eq :name }
|
30
|
+
its(:target_attribute) { is_expected.to eq :attr }
|
31
|
+
its(:full_name) { is_expected.to eq :name_foo }
|
32
|
+
its(:multiparam) { is_expected.to eq Date }
|
33
|
+
its(:multiparam?) { is_expected.to be true }
|
34
34
|
end
|
35
35
|
|
36
36
|
describe "#fetch_reader" do
|
37
37
|
context 'default' do
|
38
38
|
subject{ Mapping.new(mapper, :name, :attr) }
|
39
39
|
|
40
|
-
its(:reader){
|
40
|
+
its(:reader) { is_expected.to be_a(Mapping::Reader::Basic) }
|
41
41
|
end
|
42
42
|
|
43
43
|
context 'method' do
|
44
44
|
subject{ Mapping.new(mapper, :name, :attr, :reader => :method_name) }
|
45
45
|
|
46
|
-
its(:reader){
|
46
|
+
its(:reader) { is_expected.to be_a(Mapping::Reader::Method) }
|
47
47
|
end
|
48
48
|
|
49
49
|
context 'proc' do
|
50
50
|
subject{ Mapping.new(mapper, :name, :attr, :reader => lambda{ |t| t.foo }) }
|
51
51
|
|
52
|
-
its(:reader){
|
52
|
+
its(:reader) { is_expected.to be_a(Mapping::Reader::Proc) }
|
53
53
|
end
|
54
54
|
|
55
55
|
context 'formatted' do
|
56
56
|
subject{ Mapping.new(mapper, :name, :attr, :format => :i18n_l) }
|
57
57
|
|
58
|
-
its(:reader){
|
58
|
+
its(:reader) { is_expected.to be_a(Mapping::Reader::Formatted) }
|
59
59
|
end
|
60
60
|
|
61
61
|
context 'blank' do
|
62
62
|
subject{ Mapping.new(mapper, :name, :attr, :reader => false) }
|
63
63
|
|
64
|
-
its(:reader){
|
64
|
+
its(:reader) { is_expected.to be_nil }
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
@@ -70,32 +70,32 @@ module FlatMap
|
|
70
70
|
context 'default' do
|
71
71
|
subject{ Mapping.new(mapper, :name, :attr) }
|
72
72
|
|
73
|
-
its(:writer){
|
73
|
+
its(:writer) { is_expected.to be_a(Mapping::Writer::Basic) }
|
74
74
|
end
|
75
75
|
|
76
76
|
context 'method' do
|
77
77
|
subject{ Mapping.new(mapper, :name, :attr, :writer => :method_name) }
|
78
78
|
|
79
|
-
its(:writer){
|
79
|
+
its(:writer) { is_expected.to be_a(Mapping::Writer::Method) }
|
80
80
|
end
|
81
81
|
|
82
82
|
context 'proc' do
|
83
83
|
subject{ Mapping.new(mapper, :name, :attr, :writer => lambda{ |t| t.foo }) }
|
84
84
|
|
85
|
-
its(:writer){
|
85
|
+
its(:writer) { is_expected.to be_a(Mapping::Writer::Proc) }
|
86
86
|
end
|
87
87
|
|
88
88
|
context 'blank' do
|
89
89
|
subject{ Mapping.new(mapper, :name, :attr, :writer => false) }
|
90
90
|
|
91
|
-
its(:writer){
|
91
|
+
its(:writer) { is_expected.to be_nil }
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
describe "read_as_params" do
|
96
96
|
it "should return mapping as key value pair" do
|
97
97
|
stub_target
|
98
|
-
mapping.read_as_params.
|
98
|
+
expect(mapping.read_as_params).to eq({:name => "target_foo"})
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
@@ -107,16 +107,16 @@ module FlatMap
|
|
107
107
|
|
108
108
|
it "should set no value on the target from params" do
|
109
109
|
target = stub_target
|
110
|
-
target.
|
110
|
+
expect(target).not_to receive(:attr=)
|
111
111
|
mapping.write_from_params(:not_mapping_name => "value")
|
112
|
-
target.attr.
|
112
|
+
expect(target.attr).to eq 'target_foo'
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
116
|
def stub_target(assignment = nil)
|
117
117
|
target = double('target', :attr => 'target_foo')
|
118
|
-
mapper.
|
119
|
-
target.
|
118
|
+
allow(mapper).to receive(:target).and_return(target)
|
119
|
+
expect(target).to receive(:attr=).with(assignment) if assignment.present?
|
120
120
|
target
|
121
121
|
end
|
122
122
|
end
|