objectified_sessions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,65 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions setup" 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
+ end
13
+
14
+ it "should allow setting the session class as a String" do
15
+ class SetupStringSpecifiedClass < ::ObjectifiedSessions::Base; end
16
+ ::ObjectifiedSessions.session_class = 'setup_string_specified_class'
17
+ @controller_instance.objsession.class.should == SetupStringSpecifiedClass
18
+ end
19
+
20
+ it "should allow setting the session class as a Symbol" do
21
+ class SetupSymbolSpecifiedClass < ::ObjectifiedSessions::Base; end
22
+ ::ObjectifiedSessions.session_class = :setup_symbol_specified_class
23
+ @controller_instance.objsession.class.should == SetupSymbolSpecifiedClass
24
+ end
25
+
26
+ it "should allow setting the session class as a Class" do
27
+ class SetupClassSpecifiedClass < ::ObjectifiedSessions::Base; end
28
+ ::ObjectifiedSessions.session_class = SetupClassSpecifiedClass
29
+ @controller_instance.objsession.class.should == SetupClassSpecifiedClass
30
+ end
31
+
32
+ it "should be able to load the session class from a file on the load path" do
33
+ require 'tmpdir'
34
+ require 'fileutils'
35
+
36
+ dir = Dir.mktmpdir('objectified_sessions')
37
+ dir = File.expand_path(dir)
38
+
39
+ subdir = File.join(dir, 'foo')
40
+ FileUtils.mkdir_p(subdir)
41
+
42
+ target_file = File.join(subdir, 'bar.rb')
43
+ File.open(target_file, 'w') do |f|
44
+ f.puts <<-EOF
45
+ module Foo
46
+ class Bar < ObjectifiedSessions::Base
47
+ field :foo
48
+ end
49
+ end
50
+ EOF
51
+ end
52
+
53
+ $: << dir
54
+ ::ObjectifiedSessions.session_class = "Foo::Bar"
55
+ @controller_instance.objsession.class.name.should == "Foo::Bar"
56
+
57
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
58
+ @controller_instance.objsession.foo = 123
59
+
60
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
61
+ @controller_instance.objsession.foo.should == 234
62
+
63
+ FileUtils.rm_rf(dir)
64
+ end
65
+ end
@@ -0,0 +1,73 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions strings vs. symbols" 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 always set and get by String, never Symbol" do
14
+ define_objsession_class { field :foo }
15
+
16
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
17
+ @controller_instance.objsession.foo = 123
18
+
19
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
20
+ @controller_instance.objsession.send(:[]=, :foo, 123)
21
+
22
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
23
+ @controller_instance.objsession.foo.should == 234
24
+
25
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
26
+ @controller_instance.objsession.send(:[], :foo).should == 234
27
+ end
28
+
29
+ it "should treat symbols and strings in received data identically" do
30
+ define_objsession_class do
31
+ prefix :prf
32
+ unknown_fields :delete
33
+
34
+ field :foo
35
+ field :bar
36
+ end
37
+
38
+ should_be_using_prefix('prf', false)
39
+ allow(@underlying_session).to receive(:keys).and_return([ 'foo', 'prf', :baz, :quux ])
40
+ allow(@prefixed_underlying_session).to receive(:keys).and_return([ 'foo', 'aaa', :bbb, :bar ])
41
+
42
+ expect(@prefixed_underlying_session).to receive(:delete).once do |arr|
43
+ unless arr.sort_by(&:to_s) == [ 'aaa', :bbb ].sort_by(&:to_s)
44
+ raise "Received :delete with incorrect arguments: #{arr.inspect}"
45
+ end
46
+ end
47
+
48
+ allow(@prefixed_underlying_session).to receive(:[]).with('foo').and_return(123)
49
+ allow(@prefixed_underlying_session).to receive(:[]).with('bar').and_return(345)
50
+
51
+ @controller_instance.objsession.foo.should == 123
52
+ @controller_instance.objsession.bar.should == 345
53
+ end
54
+
55
+ it "should preserve and delete data correctly, whether it's specified as a String or a Symbol" do
56
+ define_objsession_class do
57
+ unknown_fields :delete
58
+
59
+ field :preserve1
60
+ field :preserve2
61
+ end
62
+
63
+ expect(@underlying_session).to receive(:keys).once.with().and_return([ :preserve1, 'preserve2', :delete1, 'delete2' ])
64
+ expect(@underlying_session).to receive(:delete).once do |arr|
65
+ unless arr.sort_by(&:to_s) == [ :delete1, 'delete2' ].sort_by(&:to_s)
66
+ raise "Received :delete with incorrect arguments: #{arr.inspect}"
67
+ end
68
+ end
69
+
70
+ allow(@underlying_session).to receive(:[]).with('preserve1').and_return(234)
71
+ @controller_instance.objsession.preserve1.should == 234
72
+ end
73
+ end
@@ -0,0 +1,65 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions unknown-data 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
+ it "should not remove unknown data by default" do
14
+ define_objsession_class { field :foo; field :bar }
15
+
16
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :baz, :quux ])
17
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
18
+
19
+ @controller_instance.objsession.foo.should == 234
20
+ end
21
+
22
+ it "should remove unknown data if asked to" do
23
+ define_objsession_class do
24
+ unknown_fields :delete
25
+
26
+ field :foo
27
+ field :bar
28
+ end
29
+
30
+ allow(@underlying_session).to receive(:keys).with().and_return([ :foo, :baz, :quux ])
31
+ expect(@underlying_session).to receive(:delete).once do |arr|
32
+ unless arr.sort_by(&:to_s) == [ :baz, :quux ].sort_by(&:to_s)
33
+ raise "Received :delete with incorrect arguments: #{arr.inspect}"
34
+ end
35
+ end
36
+
37
+ allow(@underlying_session).to receive(:[]).with('foo').and_return(234)
38
+
39
+ @controller_instance.objsession.foo.should == 234
40
+ end
41
+
42
+ it "should not remove unknown data from outside the prefix" do
43
+ define_objsession_class do
44
+ prefix :prf
45
+ unknown_fields :delete
46
+
47
+ field :foo
48
+ field :bar
49
+ end
50
+
51
+ should_be_using_prefix('prf', false)
52
+ allow(@underlying_session).to receive(:keys).and_return([ :foo, :prf, :baz, :quux ])
53
+ allow(@prefixed_underlying_session).to receive(:keys).and_return([ :foo, :aaa, :bbb ])
54
+
55
+ expect(@prefixed_underlying_session).to receive(:delete).once do |arr|
56
+ unless arr.sort_by(&:to_s) == [ :aaa, :bbb ].sort_by(&:to_s)
57
+ raise "Received :delete with incorrect arguments: #{arr.inspect}"
58
+ end
59
+ end
60
+
61
+ allow(@prefixed_underlying_session).to receive(:[]).with('foo').and_return(123)
62
+
63
+ @controller_instance.objsession.foo.should == 123
64
+ end
65
+ end
@@ -0,0 +1,61 @@
1
+ require 'objectified_sessions'
2
+ require "objectified_sessions/helpers/controller_helper"
3
+ require "objectified_sessions/helpers/exception_helpers"
4
+
5
+ describe "ObjectifiedSessions visibility" 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 making a field private, and generate only private methods for it" do
14
+ define_objsession_class do
15
+ field :foo, :visibility => :private
16
+
17
+ def set_foo(x)
18
+ self.foo = x
19
+ end
20
+
21
+ def get_foo
22
+ foo
23
+ end
24
+ end
25
+
26
+ @controller_instance.objsession.respond_to?(:foo).should_not be
27
+ @controller_instance.objsession.respond_to?(:foo=).should_not be
28
+ lambda { @controller_instance.objsession.foo }.should raise_error(NoMethodError)
29
+ lambda { @controller_instance.objsession.foo = 123 }.should raise_error(NoMethodError)
30
+
31
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
32
+ @controller_instance.objsession.set_foo(123)
33
+
34
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
35
+ @controller_instance.objsession.get_foo.should == 234
36
+ end
37
+
38
+ it "should allow setting the default visibility to private, but overriding it on a field-by-field basis" do
39
+ define_objsession_class do
40
+ default_visibility :private
41
+
42
+ field :foo
43
+ field :bar, :visibility => :public
44
+ end
45
+
46
+ @controller_instance.objsession.respond_to?(:foo).should_not be
47
+ @controller_instance.objsession.respond_to?(:foo=).should_not be
48
+ lambda { @controller_instance.objsession.foo }.should raise_error(NoMethodError)
49
+ lambda { @controller_instance.objsession.foo = 123 }.should raise_error(NoMethodError)
50
+
51
+ expect(@underlying_session).to receive(:[]=).once.with('bar', 123)
52
+ @controller_instance.objsession.bar = 123
53
+ expect(@underlying_session).to receive(:[]).once.with('bar').and_return(234)
54
+ @controller_instance.objsession.bar.should == 234
55
+
56
+ expect(@underlying_session).to receive(:[]=).once.with('foo', 123)
57
+ @controller_instance.objsession.send(:foo=, 123)
58
+ expect(@underlying_session).to receive(:[]).once.with('foo').and_return(234)
59
+ @controller_instance.objsession.send(:foo).should == 234
60
+ end
61
+ end
@@ -0,0 +1,121 @@
1
+ require 'objectified_sessions'
2
+ require 'fileutils'
3
+
4
+ describe ObjectifiedSessionGenerator do
5
+ before :each do
6
+ @dir = Dir.mktmpdir("objectified_sessions_spec_generator")
7
+
8
+ allow(::Rails).to receive(:root).with().and_return(@dir)
9
+ FileUtils.mkdir_p(File.join(@dir, "lib"))
10
+
11
+ @generator = ObjectifiedSessionGenerator.new
12
+
13
+ @puts_msgs = [ ]
14
+ pm = @puts_msgs
15
+ allow($stdout).to receive(:puts) { |*args| pm << args }
16
+ end
17
+
18
+ def output
19
+ @puts_msgs.flatten.join("\n")
20
+ end
21
+
22
+ after :each do
23
+ FileUtils.rm_rf(@dir) if @dir && File.exist?(@dir)
24
+ end
25
+
26
+ context "actual class creation" do
27
+ def expect_class_creation(class_name, subpath)
28
+ @generator.create_session_file
29
+
30
+ output.should match(/#{class_name}/)
31
+ output.should match(/#{@dir}/)
32
+
33
+ target_file = File.join(@dir, subpath)
34
+ File.exist?(target_file).should be
35
+
36
+ const_target = ::Object
37
+ while const_target && class_name =~ /^(.*?)::(.*)$/i
38
+ if const_target.const_defined?($1)
39
+ const_target = const_target.const_get($1)
40
+ else
41
+ const_target = nil
42
+ end
43
+ end
44
+
45
+ const_target.send(:remove_const, class_name.to_sym) if const_target && const_target.const_defined?(class_name.to_sym)
46
+
47
+ load(target_file)
48
+
49
+ klass = class_name.constantize
50
+ klass.superclass.should == ::ObjectifiedSessions::Base
51
+ klass.accessible_field_names.should == [ ]
52
+ klass.default_visibility.should == :public
53
+ klass.prefix.should == nil
54
+ klass.unknown_fields.should == :preserve
55
+ end
56
+
57
+ it "should create a normal 'objsession' class" do
58
+ ::ObjectifiedSessions.session_class = ::ObjectifiedSessions::DEFAULT_OBJSESSION_CLASS_NAME
59
+ expect_class_creation("Objsession", "lib/objsession.rb")
60
+ end
61
+
62
+ it "should create an alternate class if so configured" do
63
+ ::ObjectifiedSessions.session_class = "FooBar"
64
+ expect_class_creation("FooBar", "lib/foo_bar.rb")
65
+ end
66
+
67
+ it "should create a nested class if asked to" do
68
+ ::ObjectifiedSessions.session_class = "BazTest::BarTest::FooBarTest"
69
+ expect_class_creation("BazTest::BarTest::FooBarTest", "lib/baz_test/bar_test/foo_bar_test.rb")
70
+ end
71
+ end
72
+
73
+ context "class creation failures" do
74
+ it "should not create an Objsession class if one exists already in the namespace" do
75
+ class Objsession; end
76
+ ::ObjectifiedSessions.session_class = ::ObjectifiedSessions::DEFAULT_OBJSESSION_CLASS_NAME
77
+
78
+ @generator.create_session_file
79
+ target_file = File.join(@dir, "lib/objsession.rb")
80
+
81
+ File.exist?(target_file).should_not be
82
+ output.should match(/doing nothing/i)
83
+ output.should match(/Objsession/i)
84
+ end
85
+
86
+ it "should not create a custom-named class if one exists already in the namespace" do
87
+ class FooBar1; end
88
+ ::ObjectifiedSessions.session_class = :FooBar1
89
+
90
+ @generator.create_session_file
91
+ target_file = File.join(@dir, "lib/foo_bar1.rb")
92
+
93
+ File.exist?(target_file).should_not be
94
+ output.should match(/doing nothing/i)
95
+ output.should match(/FooBar1/i)
96
+ end
97
+
98
+ it "should not create an Objsession class if a file exists on disk" do
99
+ ::ObjectifiedSessions.session_class = ::ObjectifiedSessions::DEFAULT_OBJSESSION_CLASS_NAME
100
+ ::Object.send(:remove_const, :Objsession) if ::Object.const_defined?(:Objsession)
101
+
102
+ FileUtils.mkdir_p(File.join(@dir, "lib"))
103
+ File.open(File.join(@dir, "lib/objsession.rb"), "w") { |f| f.puts "hi!" }
104
+ @generator.create_session_file
105
+
106
+ output.should match(/already a file/i)
107
+ output.should match(%r{#{@dir}/lib/objsession\.rb}i)
108
+ end
109
+
110
+ it "should not create a custom class if a file exists on disk" do
111
+ ::ObjectifiedSessions.session_class = "Foo::Bar2"
112
+
113
+ FileUtils.mkdir_p(File.join(@dir, "lib/foo"))
114
+ File.open(File.join(@dir, "lib/foo/bar2.rb"), "w") { |f| f.puts "hi!" }
115
+ @generator.create_session_file
116
+
117
+ output.should match(/already a file/i)
118
+ output.should match(%r{#{@dir}/lib/foo/bar2\.rb}i)
119
+ end
120
+ end
121
+ end