objectified_sessions 1.0.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.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +21 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +391 -0
  7. data/Rakefile +6 -0
  8. data/lib/objectified_session_generator.rb +139 -0
  9. data/lib/objectified_sessions/base.rb +299 -0
  10. data/lib/objectified_sessions/errors.rb +55 -0
  11. data/lib/objectified_sessions/field_definition.rb +105 -0
  12. data/lib/objectified_sessions/version.rb +3 -0
  13. data/lib/objectified_sessions.rb +157 -0
  14. data/objectified_sessions.gemspec +43 -0
  15. data/spec/objectified_sessions/helpers/controller_helper.rb +55 -0
  16. data/spec/objectified_sessions/helpers/exception_helpers.rb +20 -0
  17. data/spec/objectified_sessions/system/basic_system_spec.rb +135 -0
  18. data/spec/objectified_sessions/system/error_handling_system_spec.rb +217 -0
  19. data/spec/objectified_sessions/system/prefix_system_spec.rb +62 -0
  20. data/spec/objectified_sessions/system/retired_inactive_system_spec.rb +188 -0
  21. data/spec/objectified_sessions/system/setup_system_spec.rb +65 -0
  22. data/spec/objectified_sessions/system/strings_symbols_system_spec.rb +73 -0
  23. data/spec/objectified_sessions/system/unknown_data_system_spec.rb +65 -0
  24. data/spec/objectified_sessions/system/visibility_system_spec.rb +61 -0
  25. data/spec/objectified_sessions/unit/objectified_session_generator_spec.rb +121 -0
  26. data/spec/objectified_sessions/unit/objectified_sessions/base_spec.rb +484 -0
  27. data/spec/objectified_sessions/unit/objectified_sessions/errors_spec.rb +75 -0
  28. data/spec/objectified_sessions/unit/objectified_sessions/field_definition_spec.rb +138 -0
  29. data/spec/objectified_sessions/unit/objectified_sessions_spec.rb +149 -0
  30. metadata +120 -0
@@ -0,0 +1,55 @@
1
+ module ObjectifiedSessions
2
+ module Helpers
3
+ module ControllerHelper
4
+ def new_spec_controller_instance
5
+ klass = Class.new(::ActionController::Base)
6
+ klass_name = "SpecController#{rand(1_000_000_000)}"
7
+ ::Object.const_set(klass_name, klass)
8
+
9
+ underlying_session = double("underlying_session")
10
+
11
+ instance = klass.new
12
+ allow(instance).to receive(:session).with().and_return(underlying_session)
13
+ instance
14
+ end
15
+
16
+ def define_objsession_class(name = nil, &block)
17
+ @objsession_class = Class.new(::ObjectifiedSessions::Base)
18
+ @objsession_class.class_eval(&block)
19
+
20
+ if name
21
+ ::Object.send(:remove_const, name) if ::Object.const_defined?(name)
22
+ ::Object.send(:const_set, name, @objsession_class)
23
+ end
24
+
25
+ ::ObjectifiedSessions.session_class = @objsession_class
26
+ end
27
+
28
+ def set_new_controller_instance
29
+ @controller_instance = new_spec_controller_instance
30
+ @controller_class = @controller_instance.class
31
+ @controller_class_name = @controller_class.name
32
+ @underlying_session = @controller_instance.session
33
+ end
34
+
35
+ def should_be_using_prefix(prefix, should_require_set = false)
36
+ prefix_set = (! should_require_set)
37
+
38
+ if should_require_set
39
+ expect(@underlying_session).to receive(:[]=).once.with(prefix, { }) do
40
+ prefix_set = true
41
+ end
42
+ end
43
+
44
+ @prefixed_underlying_session = double("prefixed_underlying_session")
45
+ allow(@underlying_session).to receive(:[]).with(prefix) do
46
+ if prefix_set
47
+ @prefixed_underlying_session
48
+ else
49
+ nil
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,20 @@
1
+ module ObjectifiedSessions
2
+ module Helpers
3
+ module ExceptionHelpers
4
+ def capture_exception(required_class = Exception, &block)
5
+ e = nil
6
+ begin
7
+ block.call
8
+ rescue required_class => x
9
+ e = x
10
+ end
11
+
12
+ unless e
13
+ raise "Expected an exception of class #{required_class.inspect}, but none was raised"
14
+ end
15
+
16
+ e
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,135 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions basic operations" do
6
+ include ObjectifiedSessions::Helpers::ControllerHelper
7
+ include ObjectifiedSessions::Helpers::ExceptionHelpers
8
+
9
+ before :each do
10
+ set_new_controller_instance
11
+ end
12
+
13
+ it "should have an object at #objsession, even with an empty class" do
14
+ define_objsession_class { }
15
+ @controller_instance.objsession.should be
16
+ end
17
+
18
+ it "should allow setting and getting a defined field, and read/write that on the underlying session" do
19
+ define_objsession_class { field :foo }
20
+
21
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
22
+ @controller_instance.objsession.foo = 123
23
+
24
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
25
+ @controller_instance.objsession.foo.should == 234
26
+ end
27
+
28
+ it "should allow specifying a defined field as a String" do
29
+ define_objsession_class { field 'FoO' }
30
+
31
+ expect(@underlying_session).to receive(:[]=).once.with('FoO', 123)
32
+ @controller_instance.objsession.foo = 123
33
+
34
+ expect(@underlying_session).to receive(:[]).once.with('FoO').and_return(234)
35
+ @controller_instance.objsession.foo.should == 234
36
+ end
37
+
38
+ it "should allow hash access to the underlying session" do
39
+ define_objsession_class do
40
+ field :foo, :visibility => :private
41
+
42
+ def set_foo(x)
43
+ self[:foo] = x
44
+ end
45
+
46
+ def get_foo
47
+ self[:foo]
48
+ end
49
+ end
50
+
51
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
52
+ @controller_instance.objsession.set_foo(123)
53
+
54
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
55
+ @controller_instance.objsession.get_foo.should == 234
56
+ end
57
+
58
+ it "should tell you what fields are defined, and which have data" do
59
+ define_objsession_class do
60
+ field :foo
61
+ field :bar
62
+ end
63
+
64
+ @controller_instance.objsession.field_names.sort_by(&:to_s).should == [ :foo, :bar ].sort_by(&:to_s)
65
+
66
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(123)
67
+ expect(@underlying_session).to receive(:[]).once.with('bar').and_return(nil)
68
+ @controller_instance.objsession.keys.should == [ :foo ]
69
+
70
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(nil)
71
+ expect(@underlying_session).to receive(:[]).once.with('bar').and_return(false)
72
+ @controller_instance.objsession.keys.should == [ :bar ]
73
+ end
74
+
75
+ it "should turn itself into a string reasonably well" do
76
+ define_objsession_class do
77
+ field :foo
78
+ field :bar
79
+ end
80
+
81
+ ::Object.const_set(:ObjectifiedSessionsSpecBasicString, @objsession_class)
82
+
83
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(123)
84
+ allow(@underlying_session).to receive(:[]).with('bar').and_return(nil)
85
+ @controller_instance.objsession.to_s.should == "<ObjectifiedSessionsSpecBasicString: foo: 123>"
86
+ @controller_instance.objsession.inspect.should == @controller_instance.objsession.to_s
87
+
88
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(123)
89
+ allow(@underlying_session).to receive(:[]).with('bar').and_return("a" * 200)
90
+ @controller_instance.objsession.to_s.should == "<ObjectifiedSessionsSpecBasicString: bar: \"#{"a" * 36}..., foo: 123>"
91
+ @controller_instance.objsession.inspect.should == @controller_instance.objsession.to_s
92
+
93
+ @controller_instance.objsession.to_s(false).should == "<ObjectifiedSessionsSpecBasicString: bar: \"#{"a" * 200}\", foo: 123>"
94
+ @controller_instance.objsession.inspect(false).should == @controller_instance.objsession.to_s(false)
95
+ end
96
+
97
+ it "should allow setting a storage name for a field, and should use that when talking to the underlying session" do
98
+ define_objsession_class do
99
+ unknown_fields :delete
100
+
101
+ field :foo, :storage => :f
102
+ field :bar, :storage => :b
103
+ end
104
+
105
+ allow(@underlying_session).to receive(:keys).and_return([ :foo, :b ])
106
+ expect(@underlying_session).to receive(:delete).once.with([ :foo ])
107
+
108
+
109
+ expect(@underlying_session).to receive(:[]=).once.with('f', 123)
110
+ @controller_instance.objsession.foo = 123
111
+
112
+ expect(@underlying_session).to receive(:[]).once.with('f').and_return(234)
113
+ @controller_instance.objsession.foo.should == 234
114
+
115
+ allow(@underlying_session).to receive(:[]).once.with('b').and_return(456)
116
+ @controller_instance.objsession.bar.should == 456
117
+
118
+ lambda { @controller_instance.objsession.send(:f) }.should raise_error(NoMethodError)
119
+ lambda { @controller_instance.objsession.send(:f=) }.should raise_error(NoMethodError)
120
+ lambda { @controller_instance.objsession.send(:b) }.should raise_error(NoMethodError)
121
+ lambda { @controller_instance.objsession.send(:b=) }.should raise_error(NoMethodError)
122
+
123
+ lambda { @controller_instance.objsession.send(:[], :f) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
124
+ lambda { @controller_instance.objsession.send(:[]=, :f, 123) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
125
+ lambda { @controller_instance.objsession.send(:[], :b) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
126
+ lambda { @controller_instance.objsession.send(:[]=, :b, 123) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
127
+ end
128
+
129
+ it "should call the included module something sane" do
130
+ define_objsession_class(:IncludedModuleSpecObjSession) { field :foo }
131
+
132
+ included = @controller_instance.objsession.class.included_modules.detect { |m| m.name =~ /objectifiedsessions/i }
133
+ included.should be
134
+ end
135
+ end
@@ -0,0 +1,217 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions error handling" do
6
+ include ObjectifiedSessions::Helpers::ControllerHelper
7
+ include ObjectifiedSessions::Helpers::ExceptionHelpers
8
+
9
+ before :each do
10
+ set_new_controller_instance
11
+ ::ObjectifiedSessions.instance_variable_set("@session_class", nil)
12
+ ::ObjectifiedSessions.instance_variable_set("@_session_class_object", nil)
13
+ end
14
+
15
+ describe "session-class specification" do
16
+ it "should raise a nice exception if you haven't specified a session class, nor defined one" do
17
+ e = capture_exception(NameError) { @controller_instance.objsession }
18
+ e.message.should match(/Objsession/)
19
+ end
20
+
21
+ it "should raise a relevant exception if the specified session class doesn't exist" do
22
+ ::ObjectifiedSessions.session_class = :NonexistentSessionClass1
23
+
24
+ e = capture_exception(NameError) { @controller_instance.objsession }
25
+ e.message.should match(/NonexistentSessionClass1/i)
26
+ end
27
+
28
+ it "should raise a relevant exception if the specified class can't be instantiated" do
29
+ class FooError < StandardError; end
30
+
31
+ define_objsession_class(:CannotInstantiateObjsessionClass) do
32
+ def initialize(*args)
33
+ raise FooError, "kaboomba"
34
+ end
35
+ end
36
+
37
+ e = capture_exception(ObjectifiedSessions::Errors::CannotCreateSessionError) { @controller_instance.objsession }
38
+ e.message.should match(/kaboomba/i)
39
+ e.message.should match(/fooerror/i)
40
+ e.message.should match(/CannotInstantiateObjsessionClass/i)
41
+ end
42
+
43
+ it "should raise a relevant exception if the specified class isn't a subclass of ObjectifiedSessions::Base" do
44
+ class SomeOtherClass
45
+ def initialize(*args)
46
+ end
47
+
48
+ def to_s
49
+ "woomba"
50
+ end
51
+
52
+ def inspect
53
+ "woomba"
54
+ end
55
+ end
56
+
57
+ ::ObjectifiedSessions.session_class = :SomeOtherClass
58
+
59
+ e = capture_exception(ObjectifiedSessions::Errors::CannotCreateSessionError) { @controller_instance.objsession }
60
+ e.message.should match(/SomeOtherClass/i)
61
+ e.message.should match(/ObjectifiedSessions::Base/i)
62
+ e.message.should match(/woomba/i)
63
+ end
64
+ end
65
+
66
+ describe "visibility and prefix specification" do
67
+ it "should raise a nice error if you try to set the default visibility to something bogus" do
68
+ e = capture_exception(ArgumentError) do
69
+ define_objsession_class do
70
+ default_visibility true
71
+ end
72
+ end
73
+
74
+ e.message.should match(/true/)
75
+ e.message.should match(/public/)
76
+ e.message.should match(/private/)
77
+ end
78
+
79
+ it "should raise a nice error if you try to set the prefix to something bogus" do
80
+ e = capture_exception(ArgumentError) do
81
+ define_objsession_class do
82
+ prefix true
83
+ end
84
+ end
85
+
86
+ e.message.should match(/true/)
87
+ end
88
+
89
+ it "should raise a nice error if you try to set the unknown-fields setting to something bogus" do
90
+ e = capture_exception(ArgumentError) do
91
+ define_objsession_class do
92
+ unknown_fields true
93
+ end
94
+ end
95
+
96
+ e.message.should match(/delete/)
97
+ e.message.should match(/preserve/)
98
+ end
99
+ end
100
+
101
+ describe "field specification" do
102
+ it "should raise a nice error if you don't pass a String or Symbol as your field name" do
103
+ e = capture_exception(ArgumentError) do
104
+ define_objsession_class do
105
+ field 123
106
+ end
107
+ end
108
+
109
+ e.message.should match(/123/)
110
+ end
111
+
112
+ it "should raise a nice error if you pass an invalid option" do
113
+ e = capture_exception(ArgumentError) do
114
+ define_objsession_class do
115
+ field :foo, :a => :b
116
+ end
117
+ end
118
+
119
+ e.message.should match(/a/)
120
+ end
121
+
122
+ it "should raise a nice error if you pass an invalid :storage option" do
123
+ e = capture_exception(ArgumentError) do
124
+ define_objsession_class do
125
+ field :foo, :storage => false
126
+ end
127
+ end
128
+
129
+ e.message.should match(/false/)
130
+ end
131
+
132
+ it "should raise a nice error if you pass an invalid :visibility option" do
133
+ e = capture_exception(ArgumentError) do
134
+ define_objsession_class do
135
+ field :foo, :visibility => 12345
136
+ end
137
+ end
138
+
139
+ e.message.should match(/12345/)
140
+ end
141
+ end
142
+
143
+ it "should not allow hash access to the underlying session for undefined fields" do
144
+ define_objsession_class do
145
+ field :foo, :visibility => :private
146
+ field :baz
147
+
148
+ def set_bar(x)
149
+ self[:bar] = x
150
+ end
151
+
152
+ def get_bar
153
+ self[:bar]
154
+ end
155
+ end
156
+
157
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @controller_instance.objsession.set_bar(123) }
158
+ e.session_class.should be(@objsession_class)
159
+ e.field_name.should == :bar
160
+ e.accessible_field_names.sort_by(&:to_s).should == [ :foo, :baz ].sort_by(&:to_s)
161
+ e.message.should match(/bar/i)
162
+ e.message.should match(/foo/i)
163
+ e.message.should match(/baz/i)
164
+
165
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @controller_instance.objsession.get_bar }
166
+ e.session_class.should be(@objsession_class)
167
+ e.field_name.should == :bar
168
+ e.accessible_field_names.sort_by(&:to_s).should == [ :foo, :baz ].sort_by(&:to_s)
169
+ e.message.should match(/bar/i)
170
+ e.message.should match(/foo/i)
171
+ e.message.should match(/baz/i)
172
+ end
173
+
174
+ it "should not let you define more than one field with the same name" do
175
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
176
+ define_objsession_class do
177
+ field :foo
178
+ field 'foo'
179
+ end
180
+ end
181
+
182
+ e.session_class.should be(@objsession_class)
183
+ e.field_name.should == :foo
184
+ e.message.should match(/foo/i)
185
+ end
186
+
187
+ it "should not let you define more than one field with the same storage name" do
188
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
189
+ define_objsession_class do
190
+ field :foo, :storage => :bar
191
+ field :baz, :storage => :bar
192
+ end
193
+ end
194
+
195
+ e.session_class.should be(@objsession_class)
196
+ e.original_field_name.should == :foo
197
+ e.new_field_name.should == :baz
198
+ e.storage_name.should == 'bar'
199
+ e.message.should match(/foo/i)
200
+ e.message.should match(/baz/i)
201
+ e.message.should match(/bar/i)
202
+
203
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
204
+ define_objsession_class do
205
+ field :foo, :storage => :bar
206
+ field :bar
207
+ end
208
+ end
209
+
210
+ e.session_class.should be(@objsession_class)
211
+ e.original_field_name.should == :foo
212
+ e.new_field_name.should == :bar
213
+ e.storage_name.should == 'bar'
214
+ e.message.should match(/foo/i)
215
+ e.message.should match(/bar/i)
216
+ end
217
+ end
@@ -0,0 +1,62 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions prefix" do
6
+ include ObjectifiedSessions::Helpers::ControllerHelper
7
+ include ObjectifiedSessions::Helpers::ExceptionHelpers
8
+
9
+ before :each do
10
+ set_new_controller_instance
11
+ end
12
+
13
+ it "should allow setting a prefix, and then the underlying session should always be accessed via that Hash" do
14
+ define_objsession_class do
15
+ prefix :prf
16
+
17
+ field :foo
18
+ field :bar
19
+ end
20
+
21
+ @objsession_class.prefix.should == 'prf'
22
+
23
+ should_be_using_prefix('prf', true)
24
+
25
+ expect(@prefixed_underlying_session).to receive(:[]=).once.with('foo', 123)
26
+ @controller_instance.objsession.foo = 123
27
+
28
+ expect(@prefixed_underlying_session).to receive(:[]).once.with('foo').and_return(234)
29
+ @controller_instance.objsession.foo.should == 234
30
+ end
31
+
32
+ it "should allow setting a prefix, and return nil for everything if nothing's been set there" do
33
+ define_objsession_class do
34
+ prefix :prf
35
+
36
+ field :foo
37
+ field :bar
38
+ end
39
+
40
+ allow(@underlying_session).to receive(:[]).with('prf').and_return(nil)
41
+
42
+ @controller_instance.objsession.foo.should == nil
43
+ @controller_instance.objsession.bar.should == nil
44
+ end
45
+
46
+ it "should allow setting the prefix to nil, which shouldn't change anything" do
47
+ define_objsession_class do
48
+ prefix nil
49
+
50
+ field :foo
51
+ field :bar
52
+ end
53
+
54
+ @objsession_class.prefix.should == nil
55
+
56
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
57
+ @controller_instance.objsession.foo = 123
58
+
59
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
60
+ @controller_instance.objsession.foo.should == 234
61
+ end
62
+ end
@@ -0,0 +1,188 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions retired and inactive field handling" do
6
+ include ObjectifiedSessions::Helpers::ControllerHelper
7
+ include ObjectifiedSessions::Helpers::ExceptionHelpers
8
+
9
+ before :each do
10
+ set_new_controller_instance
11
+ end
12
+
13
+
14
+ describe "retired fields" do
15
+ it "should prevent you from defining a field with the same storage name" do
16
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
17
+ define_objsession_class do
18
+ field :foo
19
+
20
+ retired :foo
21
+ end
22
+ end
23
+
24
+ e.session_class.should be(@objsession_class)
25
+ e.field_name.should == :foo
26
+ e.message.should match(/foo/i)
27
+
28
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
29
+ define_objsession_class do
30
+ retired :foo
31
+ field :foo
32
+ end
33
+ end
34
+
35
+ e.session_class.should be(@objsession_class)
36
+ e.field_name.should == :foo
37
+ e.message.should match(/foo/i)
38
+
39
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
40
+ define_objsession_class do
41
+ retired :foo
42
+ field :bar, :storage => :foo
43
+ end
44
+ end
45
+
46
+ e.session_class.should be(@objsession_class)
47
+ e.original_field_name.should == :foo
48
+ e.new_field_name.should == :bar
49
+ e.storage_name.should == 'foo'
50
+ e.message.should match(/foo/i)
51
+ e.message.should match(/bar/i)
52
+ end
53
+
54
+ it "should not allow access to a retired field's data" do
55
+ define_objsession_class do
56
+ field :foo
57
+ retired :bar
58
+ end
59
+
60
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
61
+
62
+ @controller_instance.objsession.foo.should == 234
63
+
64
+ @controller_instance.objsession.respond_to?(:bar).should_not be
65
+ @controller_instance.objsession.respond_to?(:bar=).should_not be
66
+
67
+ lambda { @controller_instance.objsession.send(:bar) }.should raise_error(NoMethodError)
68
+ lambda { @controller_instance.objsession.send(:bar=, 123) }.should raise_error(NoMethodError)
69
+
70
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @controller_instance.objsession.send(:[], :bar) }
71
+ e.accessible_field_names.should == [ :foo ]
72
+ lambda { @controller_instance.objsession.send(:[]=, :bar, 123) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
73
+ end
74
+
75
+ it "should still delete such a field's data if the field is retired" do
76
+ define_objsession_class do
77
+ unknown_fields :delete
78
+
79
+ field :foo
80
+ retired :bar
81
+ end
82
+
83
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :bar ])
84
+ expect(@underlying_session).to receive(:delete).once.with([ :bar ])
85
+
86
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
87
+
88
+ @controller_instance.objsession.foo.should == 234
89
+ end
90
+ end
91
+
92
+ describe "inactive fields" do
93
+ it "should prevent you from defining a field with the same storage name" do
94
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
95
+ define_objsession_class do
96
+ field :foo
97
+
98
+ inactive :foo
99
+ end
100
+ end
101
+
102
+ e.session_class.should be(@objsession_class)
103
+ e.field_name.should == :foo
104
+ e.message.should match(/foo/i)
105
+
106
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
107
+ define_objsession_class do
108
+ inactive :foo
109
+ field :foo
110
+ end
111
+ end
112
+
113
+ e.session_class.should be(@objsession_class)
114
+ e.field_name.should == :foo
115
+ e.message.should match(/foo/i)
116
+
117
+ e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError) do
118
+ define_objsession_class do
119
+ inactive :foo
120
+ field :bar, :storage => :foo
121
+ end
122
+ end
123
+
124
+ e.session_class.should be(@objsession_class)
125
+ e.original_field_name.should == :foo
126
+ e.new_field_name.should == :bar
127
+ e.storage_name.should == 'foo'
128
+ e.message.should match(/foo/i)
129
+ e.message.should match(/bar/i)
130
+ end
131
+
132
+ it "should not allow access to an inactive field's data" do
133
+ define_objsession_class do
134
+ field :foo
135
+ inactive :bar
136
+ end
137
+
138
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
139
+
140
+ @controller_instance.objsession.foo.should == 234
141
+
142
+ @controller_instance.objsession.respond_to?(:bar).should_not be
143
+ @controller_instance.objsession.respond_to?(:bar=).should_not be
144
+
145
+ lambda { @controller_instance.objsession.send(:bar) }.should raise_error(NoMethodError)
146
+ lambda { @controller_instance.objsession.send(:bar=, 123) }.should raise_error(NoMethodError)
147
+
148
+ e = capture_exception(ObjectifiedSessions::Errors::NoSuchFieldError) { @controller_instance.objsession.send(:[], :bar) }
149
+ e.accessible_field_names.should == [ :foo ]
150
+ lambda { @controller_instance.objsession.send(:[]=, :bar, 123) }.should raise_error(ObjectifiedSessions::Errors::NoSuchFieldError)
151
+ end
152
+
153
+ it "should NOT delete such a field's data if the field is inactive" do
154
+ define_objsession_class do
155
+ unknown_fields :delete
156
+
157
+ field :foo
158
+ inactive :bar
159
+ end
160
+
161
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :bar ])
162
+
163
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
164
+
165
+ @controller_instance.objsession.foo.should == 234
166
+ end
167
+ end
168
+
169
+ it "should let you override a method, and #super should still work" do
170
+ define_objsession_class do
171
+ field :foo
172
+
173
+ def foo
174
+ "X" + super + "Y"
175
+ end
176
+
177
+ def foo=(x)
178
+ super("A" + x + "B")
179
+ end
180
+ end
181
+
182
+ expect(@underlying_session).to receive(:[]=).once.with('foo', "AzB")
183
+ @controller_instance.objsession.foo = 'z'
184
+
185
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return("q")
186
+ @controller_instance.objsession.foo.should == 'XqY'
187
+ end
188
+ end