flat_map 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ module FlatMap
4
+ module PersistenceSpec
5
+ class TargetClass < Struct.new(:attr_a, :attr_b)
6
+ end
7
+
8
+ class OtherTargetClass < Struct.new(:attr_c, :attr_d)
9
+ end
10
+
11
+ module ArbitraryModule
12
+ end
13
+
14
+ class TargetClassMapper < Mapper
15
+ include ArbitraryModule
16
+
17
+ map :attr_a
18
+ map :dob => :attr_b, :multiparam => Date
19
+ end
20
+
21
+ class InheritedClassMapper < TargetClassMapper
22
+ end
23
+
24
+ class ExplicitNameMapper < Mapper
25
+ self.target_class_name = 'FlatMap::PersistenceSpec::OtherTargetClass'
26
+ end
27
+ end
28
+
29
+ describe Mapper::Persistence do
30
+ describe '#target_class' do
31
+ it 'should detect target_class from mapper class name' do
32
+ PersistenceSpec::TargetClassMapper.target_class.should == PersistenceSpec::TargetClass
33
+ end
34
+
35
+ it 'should detect target_class from nearest ancestor when inherited' do
36
+ PersistenceSpec::InheritedClassMapper.target_class.should == PersistenceSpec::TargetClass
37
+ end
38
+
39
+ it 'should use explicit class name if specified' do
40
+ PersistenceSpec::ExplicitNameMapper.target_class.should == PersistenceSpec::OtherTargetClass
41
+ end
42
+ end
43
+
44
+ describe '.build' do
45
+ it 'should use target class to build a new object for mapper' do
46
+ PersistenceSpec::TargetClassMapper.should_receive(:new).with(kind_of(PersistenceSpec::TargetClass), :used_trait)
47
+ PersistenceSpec::TargetClassMapper.build(:used_trait)
48
+ end
49
+ end
50
+
51
+ describe '.find' do
52
+ let(:target){ PersistenceSpec::TargetClass.new('a', 'b') }
53
+
54
+ it 'should delegate to target class to find object for mapper' do
55
+ PersistenceSpec::TargetClass.should_receive(:find).with(1).and_return(target)
56
+ PersistenceSpec::TargetClassMapper.should_receive(:new).with(target, :used_trait)
57
+ PersistenceSpec::TargetClassMapper.find(1, :used_trait)
58
+ end
59
+ end
60
+
61
+ describe 'behavior' do
62
+ let(:target){ PersistenceSpec::TargetClass.new('a', 'b') }
63
+ let(:mapper){ PersistenceSpec::TargetClassMapper.new(target){} }
64
+
65
+ specify '#model_name' do
66
+ mapper.model_name.should == 'mapper'
67
+ end
68
+
69
+ specify '#to_key should delegate to target' do
70
+ target.should_receive(:to_key).and_return(1)
71
+ mapper.to_key.should == 1
72
+ end
73
+
74
+ specify '#persisted? when target does not respond to :persised?' do
75
+ mapper.should_not be_persisted
76
+ end
77
+
78
+ specify '#persisted? when target responds to :persisted?' do
79
+ target.stub(:persisted?).and_return(true)
80
+ mapper.should be_persisted
81
+ end
82
+
83
+ specify '#id when target does not respond to :id' do
84
+ mapper.id.should be_nil
85
+ end
86
+
87
+ specify '#id when target responds to :id' do
88
+ target.stub(:id).and_return(1)
89
+ mapper.id.should == 1
90
+ end
91
+
92
+ describe '#write with multiparams' do
93
+ let(:params) {{
94
+ 'attr_a' => 'A',
95
+ 'dob(0i)' => '1999',
96
+ 'dob(1i)' => '01',
97
+ 'dob(2i)' => '02'
98
+ }}
99
+
100
+ it 'should assign values properly' do
101
+ mapper.write(params)
102
+ target.attr_a.should == 'A'
103
+ target.attr_b.should == Date.new(1999, 1, 2)
104
+ end
105
+ end
106
+
107
+ describe '#save_target' do
108
+ it 'should return true for owned mappers' do
109
+ mapper.extension.save_target.should be_true
110
+ end
111
+
112
+ it 'should return true if target does not respond to #save' do
113
+ mapper.save_target.should be_true
114
+ end
115
+
116
+ it 'should save with no validation if target responds to #save' do
117
+ target.should_receive(:save).with(:validate => false).and_return(true)
118
+ mapper.save_target.should be_true
119
+ end
120
+ end
121
+
122
+ describe '#apply' do
123
+ let(:params){{ :attr_a => 'A' }}
124
+
125
+ it 'should write params first' do
126
+ mapper.should_receive(:write).with(params)
127
+ ActiveRecord::Base.should_receive(:transaction).and_yield
128
+ mapper.apply(params)
129
+ end
130
+
131
+ it 'should not save if not valid' do
132
+ mapper.stub(:valid?).and_return(false)
133
+ mapper.should_not_receive(:save)
134
+ mapper.apply(params)
135
+ end
136
+
137
+ it 'should save if valid' do
138
+ mapper.stub(:valid?).and_return(true)
139
+ ActiveRecord::Base.should_receive(:transaction).and_yield
140
+ mapper.should_receive(:save)
141
+ mapper.apply(params)
142
+ end
143
+ end
144
+
145
+ specify '#shallow_save saves target in a save callbacks' do
146
+ mapper.should_receive(:run_callbacks).with(:save).and_yield
147
+ mapper.should_receive(:save_target)
148
+ mapper.shallow_save
149
+ end
150
+ end
151
+ end
152
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flat_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TMX Credit
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2014-01-10 00:00:00.000000000 Z
14
+ date: 2014-01-17 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -106,19 +106,7 @@ files:
106
106
  - Rakefile
107
107
  - flat_map.gemspec
108
108
  - lib/flat_map.rb
109
- - lib/flat_map/base_mapper.rb
110
- - lib/flat_map/base_mapper/attribute_methods.rb
111
- - lib/flat_map/base_mapper/factory.rb
112
- - lib/flat_map/base_mapper/mapping.rb
113
- - lib/flat_map/base_mapper/mounting.rb
114
- - lib/flat_map/base_mapper/persistence.rb
115
- - lib/flat_map/base_mapper/skipping.rb
116
- - lib/flat_map/base_mapper/traits.rb
117
- - lib/flat_map/empty_mapper.rb
118
109
  - lib/flat_map/errors.rb
119
- - lib/flat_map/mapper.rb
120
- - lib/flat_map/mapper/skipping.rb
121
- - lib/flat_map/mapper/targeting.rb
122
110
  - lib/flat_map/mapping.rb
123
111
  - lib/flat_map/mapping/factory.rb
124
112
  - lib/flat_map/mapping/reader.rb
@@ -131,14 +119,25 @@ files:
131
119
  - lib/flat_map/mapping/writer/basic.rb
132
120
  - lib/flat_map/mapping/writer/method.rb
133
121
  - lib/flat_map/mapping/writer/proc.rb
122
+ - lib/flat_map/model_mapper.rb
123
+ - lib/flat_map/model_mapper/persistence.rb
124
+ - lib/flat_map/model_mapper/skipping.rb
125
+ - lib/flat_map/open_mapper.rb
126
+ - lib/flat_map/open_mapper/attribute_methods.rb
127
+ - lib/flat_map/open_mapper/factory.rb
128
+ - lib/flat_map/open_mapper/mapping.rb
129
+ - lib/flat_map/open_mapper/mounting.rb
130
+ - lib/flat_map/open_mapper/persistence.rb
131
+ - lib/flat_map/open_mapper/skipping.rb
132
+ - lib/flat_map/open_mapper/traits.rb
134
133
  - lib/flat_map/version.rb
135
- - spec/flat_map/empty_mapper_spec.rb
136
134
  - spec/flat_map/errors_spec.rb
137
135
  - spec/flat_map/mapper/attribute_methods_spec.rb
138
136
  - spec/flat_map/mapper/callbacks_spec.rb
139
137
  - spec/flat_map/mapper/factory_spec.rb
140
138
  - spec/flat_map/mapper/mapping_spec.rb
141
139
  - spec/flat_map/mapper/mounting_spec.rb
140
+ - spec/flat_map/mapper/persistence_spec.rb
142
141
  - spec/flat_map/mapper/skipping_spec.rb
143
142
  - spec/flat_map/mapper/targeting_spec.rb
144
143
  - spec/flat_map/mapper/traits_spec.rb
@@ -176,9 +175,31 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
175
  version: '0'
177
176
  requirements: []
178
177
  rubyforge_project: flat_map
179
- rubygems_version: 2.2.1
178
+ rubygems_version: 2.0.7
180
179
  signing_key:
181
180
  specification_version: 4
182
181
  summary: Deep object graph to a plain properties mapper
183
- test_files: []
182
+ test_files:
183
+ - spec/flat_map/errors_spec.rb
184
+ - spec/flat_map/mapper/attribute_methods_spec.rb
185
+ - spec/flat_map/mapper/callbacks_spec.rb
186
+ - spec/flat_map/mapper/factory_spec.rb
187
+ - spec/flat_map/mapper/mapping_spec.rb
188
+ - spec/flat_map/mapper/mounting_spec.rb
189
+ - spec/flat_map/mapper/persistence_spec.rb
190
+ - spec/flat_map/mapper/skipping_spec.rb
191
+ - spec/flat_map/mapper/targeting_spec.rb
192
+ - spec/flat_map/mapper/traits_spec.rb
193
+ - spec/flat_map/mapper/validations_spec.rb
194
+ - spec/flat_map/mapper_spec.rb
195
+ - spec/flat_map/mapping/factory_spec.rb
196
+ - spec/flat_map/mapping/reader/basic_spec.rb
197
+ - spec/flat_map/mapping/reader/formatted_spec.rb
198
+ - spec/flat_map/mapping/reader/method_spec.rb
199
+ - spec/flat_map/mapping/reader/proc_spec.rb
200
+ - spec/flat_map/mapping/writer/basic_spec.rb
201
+ - spec/flat_map/mapping/writer/method_spec.rb
202
+ - spec/flat_map/mapping/writer/proc_spec.rb
203
+ - spec/flat_map/mapping_spec.rb
204
+ - spec/spec_helper.rb
184
205
  has_rdoc:
@@ -1,29 +0,0 @@
1
- module FlatMap
2
- # +EmptyMapper+ behaves in a very same way as targeted {Mapper Mappers}
3
- # with only distinction of absence of required target. That makes them
4
- # a platform for mounting other mappers and placing control structures
5
- # of business logic.
6
- #
7
- # Form more detailed information on what mappers are and their API
8
- # refer to {Mapper}.
9
- class EmptyMapper < BaseMapper
10
- # Initializes +mapper+ with +traits+, which are
11
- # used to fetch proper list of mounted mappers.
12
- #
13
- # @param [*Symbol] traits List of traits
14
- def initialize(*traits)
15
- @traits = traits
16
-
17
- if block_given?
18
- singleton_class.trait :extension, &Proc.new
19
- end
20
- end
21
-
22
- # Return +true+ since there's no target.
23
- #
24
- # @return [true]
25
- def save_target
26
- true
27
- end
28
- end
29
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module EmptyMapperSpec
4
- class MountedMapper < ::FlatMap::Mapper
5
- end
6
-
7
- class Mapper < ::FlatMap::EmptyMapper
8
- mount :mounted,
9
- :mapper_class_name => 'EmptyMapperSpec::MountedMapper',
10
- :target => Object.new
11
-
12
- trait :some_trait do
13
- end
14
- end
15
- end
16
-
17
- module FlatMap
18
- describe EmptyMapper do
19
- let(:mapper){ EmptyMapperSpec::Mapper.new(:some_trait){} }
20
-
21
- it 'should be normally initialized' do
22
- mapper.mounting(:mounted).should be_present
23
- mapper.trait(:some_trait).should be_present
24
- mapper.extension.should be_present
25
- end
26
-
27
- it 'should raise error for malounted mapper when target is not specified' do
28
- mapper_class = Class.new(::FlatMap::EmptyMapper) do
29
- mount :mounted, :mapper_class_name => 'EmptyMapperSpec::MountedMapper'
30
- end
31
-
32
- expect{ mapper_class.new.mounting(:mounted) }.
33
- to raise_error(::FlatMap::Mapper::Targeting::NoTargetError)
34
- end
35
- end
36
- end