andrzejsliwa-session_management 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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ SessionManagment
2
+ ================
3
+
4
+ This is the simple plugin for session managing in objective way.
5
+
6
+ Copyright (c) 2008 I-TOOL SOFTWARE - Andrzej Sliwa, released under the MIT license
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the session_management plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the session_management plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'SessionManagement'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ This generator is used for enabling session_managment in
3
+ your application.
4
+
5
+ Example:
6
+ ./script/generate session_managment
7
+
8
+ This will create:
9
+ configuration file user_session.rb in config, and add
10
+ using of this api in application controller
@@ -0,0 +1,22 @@
1
+ class SessionManagementGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+
5
+ m.class_collisions 'UserSession'
6
+ m.directory 'config'
7
+ m.template 'user_session.rb', 'config/user_session.rb'
8
+
9
+ gsub_file 'app/controllers/application.rb', /(#{Regexp.escape("class ApplicationController < ActionController::Base")})/mi do |match|
10
+ "#{match}\n # See SessionManagment::ManagementsMethods for details\n # This option enable object oriented session managment.\n enable_session_management\n"
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ def gsub_file(relative_destination, regexp, *args, &block)
17
+ path = destination_path(relative_destination)
18
+ content = File.read(path).gsub(regexp, *args, &block)
19
+ File.open(path, 'wb') { |file| file.write(content) }
20
+ end
21
+
22
+ end
@@ -0,0 +1,31 @@
1
+ require 'session_management/session.rb'
2
+
3
+ # This class is configuration for session managment.
4
+ # Here you can define members or collection managed
5
+ # by session.
6
+ class UserSession < SessionManagement::Session
7
+
8
+ # Example of member definition:
9
+ # <code>
10
+ # member :last_action
11
+ # </code>
12
+ #
13
+ # Generated methods:
14
+ # <code>last_action= # set value on session </code>
15
+ # <code>last_action # return value from session </code>
16
+ # <code>reset_last_action # this reset value on session (and return value) </code>
17
+ member :last_action
18
+
19
+ # Example of collection definition:
20
+ # <code>
21
+ # collection :products
22
+ # </code>
23
+ #
24
+ # Generated methods:
25
+ # <code>add_product= # set value on session collection </code>
26
+ # <code>remove_product(prod) # remove value from session collection </code>
27
+ # <code>products # return session collection </code>
28
+ # <code>reset_products # this reset value on session (and return value) </code>
29
+ # collection :products
30
+
31
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,29 @@
1
+ module SessionManagement
2
+
3
+ def self.included(base)
4
+ base.extend ManagementsMethods
5
+ end
6
+
7
+ # This module is for extending ActionController::Base
8
+ module ManagementsMethods
9
+
10
+ # This method is for enabling session management
11
+ # functionality for application
12
+ def enable_session_management
13
+ unless included_modules.include? InstanceMethods
14
+ include InstanceMethods
15
+ end
16
+ end
17
+ end
18
+
19
+ # This module is used for include instance methods
20
+ # for ActionController::Base
21
+ module InstanceMethods
22
+
23
+ private
24
+ def user_session
25
+ @user_session ||= UserSession.new(session)
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,16 @@
1
+ module SessionManagement
2
+
3
+ # This Exception class is used for
4
+ # raising exception when session variable
5
+ # is defined allready.
6
+ class DefinedException < Exception
7
+ end
8
+
9
+ # This Exception class is user for
10
+ # raising exception when param is
11
+ # not Symbol for instance method sess
12
+ # of object created from Session class is
13
+ class ParamTypeException < Exception
14
+ end
15
+
16
+ end
@@ -0,0 +1,119 @@
1
+ require 'session_management/exceptions.rb'
2
+
3
+ module SessionManagement
4
+
5
+ # This class is used for managing objects on session
6
+ # with better controll of using session hash
7
+ #
8
+ # For using this you must extend this class.
9
+ # Example using:
10
+ # <code>
11
+ # class UserSession < SessionManagment::Session
12
+ # </code>
13
+ #
14
+ class Session
15
+
16
+ # This is constructor for initializing
17
+ # Session object
18
+ def initialize(session)
19
+ @session = session
20
+ end
21
+
22
+ # This helper method is for definie new
23
+ # session values ready to use in aplication
24
+ #
25
+ # Example using:
26
+ # <code>
27
+ # member :last_action
28
+ # </code>
29
+ #
30
+ # This declaration add new methods helpers for
31
+ # managing on session:
32
+ # <code>last_action= # set value on session </code>
33
+ # <code>last_action # return value from session </code>
34
+ # <code>reset_last_action # this reset value on session (and return value) </code>
35
+ #
36
+ def self.member(name)
37
+ raise ParamTypeException, "#{name} must be Symbol" unless name.class == Symbol
38
+
39
+ setter_name = "#{name}=".to_sym
40
+ getter_name = name
41
+ reset_name = "reset_#{name}".to_sym
42
+
43
+ defined = instance_method( setter_name ) rescue false
44
+ raise DefinedException, "#{setter_name} is already defined in #{self}" if defined
45
+
46
+ # define setter method in runtime
47
+ define_method( setter_name ) do |param|
48
+ @session[name] = param
49
+ end
50
+ # define getter method in runtime
51
+ define_method( getter_name ) do
52
+ @session[name]
53
+ end
54
+ # define reset method in runtime
55
+ define_method( reset_name ) do
56
+ orginal_value = @session[name]
57
+ @session[name] = nil
58
+ orginal_value
59
+ end
60
+ end
61
+
62
+ # This helper method is for definie new
63
+ # session values ready to use in aplication
64
+ #
65
+ # Example using:
66
+ # <code>
67
+ # collection :products
68
+ # </code>
69
+ #
70
+ # This declaration add new methods helpers for
71
+ # managing on session:
72
+ # <code>add_product(prod) # set value on session collection </code>
73
+ # <code>remove_product(prod) # remove value from session collection </code>
74
+ # <code>products # return session collection </code>
75
+ # <code>reset_products # this reset value on session (and return value) </code>
76
+ #
77
+ def self.collection(name)
78
+ raise ParamTypeException, "#{name} must be Symbol" unless name.class == Symbol
79
+
80
+ singular_name = name.to_s.singularize
81
+
82
+ add_name = "add_#{singular_name}".to_sym
83
+ remove_name = "remove_#{singular_name}".to_sym
84
+ coll_name = "#{name}".to_sym
85
+ reset_name = "reset_#{name}".to_sym
86
+
87
+ defined = instance_method( setter_name ) rescue false
88
+ raise DefinedException, "#{add_name} is already defined in #{self}" if defined
89
+
90
+ # define add method in runtime
91
+ define_method( add_name ) do |param|
92
+ resolve_collection( name ) << param
93
+ end
94
+ # define remove method in runtime
95
+ define_method( remove_name ) do |param|
96
+ resolve_collection( name ).delete param
97
+ end
98
+ # define get collection in runtime
99
+ define_method( coll_name ) do
100
+ resolve_collection( name )
101
+ end
102
+
103
+ # define reset collection method
104
+ define_method( reset_name ) do
105
+ orginal_value = resolve_collection( name )
106
+ @session[name] = nil
107
+ orginal_value
108
+ end
109
+ end
110
+
111
+ private
112
+ # helper method - stay DRY ;)
113
+ def resolve_collection(name)
114
+ @session[name] ||= Array.new
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,5 @@
1
+ require 'session_management'
2
+
3
+ ActionController::Base.send( :include, SessionManagement )
4
+
5
+ RAILS_DEFAULT_LOGGER.info "** session_management: initialized properly."
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "session_management"
3
+ s.version = "1.0.0"
4
+ s.date = "2008-11-12"
5
+ s.summary = "Session Management Plugin in object oriented way."
6
+ s.email = "andrzej.sliwa@i-tool.eu"
7
+ s.homepage = "http://github.com/andrzejsliwa/session_management/tree/master"
8
+ s.description = "Session Management Plugin in object oriented way."
9
+ s.has_rdoc = false
10
+ s.authors = ["Andrzej Sliwa"]
11
+ s.files = [
12
+ "MIT-LICENSE",
13
+ "README",
14
+ "generators/session_management",
15
+ "generators/session_management/session_management_generator.rb",
16
+ "generators/session_management/templates",
17
+ "generators/session_management/templates/user_session.rb",
18
+ "generators/session_management/USAGE",
19
+ "init.rb",
20
+ "install.rb",
21
+ "lib/session_management",
22
+ "lib/session_management/exceptions.rb",
23
+ "lib/session_management/session.rb",
24
+ "lib/session_management.rb",
25
+ "rails/init.rb",
26
+ "Rakefile",
27
+ "session_management.gemspec",
28
+ "test/functional",
29
+ "test/functional/session_management_test.rb",
30
+ "test/unit/user_session_test.rb",
31
+ "uninstall.rb" ]
32
+ s.add_dependency("rails", [">= 2.1.0"])
33
+ end
@@ -0,0 +1,74 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'action_controller'
4
+ require 'action_controller/test_process'
5
+ require 'session_management'
6
+ require 'session_management/session.rb'
7
+
8
+ ActionController::Base.send( :include, SessionManagement )
9
+ ActionController::Routing::Routes.reload rescue nil
10
+ ActionController::Routing::Routes.draw do |map|
11
+ map.connect ':controller/:action/:id'
12
+ end
13
+
14
+ class UserSession < SessionManagement::Session
15
+ member :last_action
16
+ collection :products
17
+ end
18
+
19
+ class SessionManagementController < ActionController::Base
20
+ enable_session_management
21
+
22
+ def set_last_action
23
+ user_session.last_action = "index"
24
+ render :xml => "<message>index</message>"
25
+ end
26
+
27
+ def get_last_action
28
+ render :xml => "<last_action>#{user_session.last_action}</last_action>"
29
+ end
30
+
31
+ def add_product
32
+ user_session.add_product(params[:id].to_s)
33
+ render :xml => "<message>index</message>"
34
+ end
35
+
36
+ def get_products
37
+ render :xml => "<products>#{user_session.products.collect{|a| a + ", "}.to_s.chop.chop}</products>"
38
+ end
39
+
40
+ end
41
+
42
+ class SessionManagementTest < Test::Unit::TestCase
43
+
44
+ def setup
45
+ @controller = SessionManagementController.new
46
+ @request = ActionController::TestRequest.new
47
+ @response = ActionController::TestResponse.new
48
+ end
49
+
50
+
51
+ def test_should_manage_members_in_session
52
+ get :set_last_action
53
+ assert_response :success
54
+ assert_select 'message', 'index'
55
+
56
+ get :get_last_action
57
+ assert_response :success
58
+ assert_select 'last_action', 'index'
59
+ end
60
+
61
+ def test_should_manage_collection_in_session
62
+ get :add_product, :id => 1
63
+ assert_response :success
64
+ assert_select 'message', 'index'
65
+
66
+ get :add_product, :id => 2
67
+ assert_response :success
68
+ assert_select 'message', 'index'
69
+
70
+ get :get_products
71
+ assert_response :success
72
+ assert_select 'products', '1, 2'
73
+ end
74
+ end
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'action_controller'
4
+ require 'action_controller/test_process'
5
+ require 'session_management'
6
+ require 'session_management/session.rb'
7
+
8
+ class UserSessionTest < Test::Unit::TestCase
9
+
10
+ class UserSession < SessionManagement::Session
11
+ member :last_action
12
+ collection :products
13
+ end
14
+
15
+ def setup
16
+ @user_session = UserSession.new(Hash.new)
17
+ end
18
+
19
+ def test_should_set_and_get_member
20
+ @user_session.last_action = "index"
21
+ assert_equal "index", @user_session.last_action
22
+ end
23
+
24
+ def test_should_reset_member
25
+ @user_session.last_action = "index"
26
+ value = @user_session.reset_last_action
27
+ assert_equal "index", value
28
+ assert_nil @user_session.last_action
29
+ end
30
+
31
+ def test_should_add_to_and_get_collection
32
+ @user_session.add_product(1)
33
+ assert_equal 1, @user_session.products[0]
34
+ @user_session.add_product(2)
35
+ assert_equal [1,2], @user_session.products
36
+ end
37
+
38
+ def test_should_reset_collection
39
+ @user_session.add_product(1)
40
+ @user_session.reset_products
41
+ assert_equal [], @user_session.products
42
+ end
43
+
44
+ end
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: andrzejsliwa-session_management
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrzej Sliwa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-12 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.0
23
+ version:
24
+ description: Session Management Plugin in object oriented way.
25
+ email: andrzej.sliwa@i-tool.eu
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - MIT-LICENSE
34
+ - README
35
+ - generators/session_management
36
+ - generators/session_management/session_management_generator.rb
37
+ - generators/session_management/templates
38
+ - generators/session_management/templates/user_session.rb
39
+ - generators/session_management/USAGE
40
+ - init.rb
41
+ - install.rb
42
+ - lib/session_management
43
+ - lib/session_management/exceptions.rb
44
+ - lib/session_management/session.rb
45
+ - lib/session_management.rb
46
+ - rails/init.rb
47
+ - Rakefile
48
+ - session_management.gemspec
49
+ - test/functional
50
+ - test/functional/session_management_test.rb
51
+ - test/unit/user_session_test.rb
52
+ - uninstall.rb
53
+ has_rdoc: false
54
+ homepage: http://github.com/andrzejsliwa/session_management/tree/master
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.2.0
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Session Management Plugin in object oriented way.
79
+ test_files: []
80
+