active_session 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_session.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
7
+ gem "minitest", :platform => :ruby_18
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jakub Kuźma, Wojciech Wnętrzak
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.md ADDED
@@ -0,0 +1,5 @@
1
+ # Active Session
2
+
3
+ ## Copyright
4
+
5
+ Copyright (c) 2011 Jakub Kuźma, Wojciech Wnętrzak. See [LICENSE](http://github.com/qoobaa/active_session/raw/master/LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << "lib" << "test"
6
+ test.pattern = "test/**/*_test.rb"
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_session/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active_session"
7
+ s.version = ActiveSession::VERSION
8
+ s.authors = ["Jakub Kuźma", "Wojciech Wnętrzak"]
9
+ s.email = ["qoobaa@gmail.com", "w.wnetrzak@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{ActiveModel session}
12
+ s.description = %q{ActiveModel session}
13
+
14
+ #s.rubyforge_project = "active_session"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "activemodel", "~> 3.1.1"
22
+ s.add_runtime_dependency "activesupport", "~> 3.1.1"
23
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveSession
2
+ module ActiveSessionError
3
+
4
+ end
5
+
6
+ class UnknownAttributeError < NoMethodError
7
+ include ActiveSessionError
8
+ end
9
+
10
+ class AssociationTypeMismatch < StandardError
11
+ include ActiveSessionError
12
+ end
13
+
14
+ class StoreValueNotDefined < StandardError
15
+ include ActiveSessionError
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveSession
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,113 @@
1
+ require "active_model"
2
+ require "active_support/concern"
3
+ require "active_support/core_ext"
4
+ require "active_session/errors"
5
+
6
+ module ActiveSession
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ extend ActiveModel::Naming
11
+ include ActiveModel::AttributeMethods
12
+ include ActiveModel::Conversion
13
+ include ActiveModel::MassAssignmentSecurity
14
+ include ActiveModel::Validations
15
+
16
+ attribute_method_suffix "", "="
17
+ attr_reader :attributes
18
+
19
+ cattr_accessor :store_key
20
+ self.store_key = :id
21
+ end
22
+
23
+ module ClassMethods
24
+ def clear_store(store)
25
+ store.delete(store_key)
26
+ end
27
+
28
+ def belongs_to(name, options = {})
29
+ klass = options.fetch(:class_name, name.to_s.classify.constantize)
30
+ foreign_key = options.fetch(:foreign_key, "#{name}_id").to_sym
31
+
32
+ define_method(name) do
33
+ return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
34
+ instance_variable_set("@#{name}", klass.find(send(foreign_key)))
35
+ end
36
+
37
+ define_method("#{name}=") do |model|
38
+ raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got #{model.class}" unless model.nil? or model.is_a?(klass)
39
+ send("#{foreign_key}=", model.try(:id))
40
+ end
41
+
42
+ define_method(foreign_key) do
43
+ @attributes[foreign_key]
44
+ end
45
+
46
+ define_method("#{foreign_key}=") do |model_id|
47
+ remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
48
+ @attributes[foreign_key] = model_id
49
+ end
50
+ end
51
+ end
52
+
53
+ module InstanceMethods
54
+ def initialize(store, attributes = nil, options = {})
55
+ @store = store
56
+ @attributes = {}
57
+ assign_attributes(attributes, options) if attributes
58
+ yield self if block_given?
59
+ end
60
+
61
+ def attributes=(attributes)
62
+ assign_attributes(attributes)
63
+ end
64
+
65
+ def assign_attributes(new_attributes, options = {})
66
+ return unless new_attributes
67
+
68
+ attributes = new_attributes.stringify_keys
69
+
70
+ unless options[:without_protection]
71
+ attributes = sanitize_for_mass_assignment(attributes, options.fetch(:as, :default))
72
+ end
73
+
74
+ attributes.each do |key, value|
75
+ if respond_to?("#{key}=")
76
+ send("#{key}=", value)
77
+ else
78
+ raise ActiveSession::UnknownAttributeError, "unknown attribute: #{key}"
79
+ end
80
+ end
81
+ end
82
+
83
+ def persisted?
84
+ @store[self.class.store_key].present? and @store[self.class.store_key] == store_value
85
+ end
86
+
87
+ def destroy
88
+ persisted? && self.class.clear_store(@store) && true
89
+ end
90
+
91
+ def store_value
92
+ raise ActiveSession::StoreValueNotDefined, "you have to override store_value method to return something useful (usually user_id)"
93
+ end
94
+
95
+ def save
96
+ valid? && serialize && true
97
+ end
98
+
99
+ private
100
+
101
+ def attribute(attribute)
102
+ @attributes[attribute.to_sym]
103
+ end
104
+
105
+ def attribute=(attribute, value)
106
+ @attributes[attribute.to_sym] = value
107
+ end
108
+
109
+ def serialize
110
+ @store[self.class.store_key] = store_value
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,12 @@
1
+ require "helper"
2
+
3
+ require "models/basic_session"
4
+
5
+ describe BasicSession do
6
+ include ActiveModel::Lint::Tests
7
+
8
+ before do
9
+ @store = {}
10
+ @model = BasicSession.new(@store)
11
+ end
12
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "minitest/autorun"
2
+
3
+ require "active_session"
@@ -0,0 +1,3 @@
1
+ class BasicSession
2
+ include ActiveSession
3
+ end
@@ -0,0 +1,11 @@
1
+ class User
2
+ attr_reader :id
3
+
4
+ def self.find(id)
5
+ new(id) if id.to_i > 0
6
+ end
7
+
8
+ def initialize(id)
9
+ @id = id
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class UserSession
2
+ include ActiveSession
3
+
4
+ belongs_to :user
5
+
6
+ validates :user, :presence => true
7
+
8
+ self.store_key = :user_id
9
+ alias_method :store_value, :user_id
10
+ end
@@ -0,0 +1,121 @@
1
+ require "helper"
2
+
3
+ require "models/user"
4
+ require "models/user_session"
5
+
6
+ describe UserSession do
7
+ include ActiveModel::Lint::Tests
8
+
9
+ before do
10
+ @store = {}
11
+ @model = UserSession.new(@store)
12
+ end
13
+
14
+ describe "valid?" do
15
+
16
+ it "returns true with existing user_id" do
17
+ session = UserSession.new(@store, :user_id => 1)
18
+ assert_equal true, session.valid?
19
+ end
20
+
21
+ it "returns false with non-existing user_id" do
22
+ session = UserSession.new(@store, :user_id => "non-existing")
23
+ assert_equal false, session.valid?
24
+ end
25
+
26
+ it "returns true with existing user" do
27
+ session = UserSession.new(@store, :user => User.new(1))
28
+ assert_equal true, session.valid?
29
+ end
30
+
31
+ it "returns false when user is nil" do
32
+ session = UserSession.new(@store, :user => nil)
33
+ assert_equal false, session.valid?
34
+ end
35
+
36
+ end
37
+
38
+ describe "save" do
39
+
40
+ it "returns true with existing user_id" do
41
+ session = UserSession.new(@store, :user_id => 5)
42
+ assert_equal true, session.save
43
+ assert_equal 5, @store[:user_id]
44
+ end
45
+
46
+ it "returns false with non-existing user" do
47
+ @store[:user_id] = "666"
48
+ session = UserSession.new(@store, :user => nil)
49
+ assert_equal false, session.save
50
+ assert_equal "666", @store[:user_id]
51
+ end
52
+
53
+ end
54
+
55
+ describe "persisted?" do
56
+
57
+ it "returns true with existing user and user_id in store" do
58
+ @store[:user_id] = 1
59
+ session = UserSession.new(@store, :user_id => 1)
60
+ assert_equal true, session.persisted?
61
+ end
62
+
63
+ it "returns false without user_id" do
64
+ @store[:user_id] = "666"
65
+ session = UserSession.new(@store)
66
+ assert_equal false, session.persisted?
67
+ end
68
+
69
+ it "returns false with existing user and different user_id in store" do
70
+ @store[:user_id] = "666"
71
+ session = UserSession.new(@store, :user_id => 1)
72
+ assert_equal false, session.persisted?
73
+ end
74
+
75
+ end
76
+
77
+ describe "destroy" do
78
+
79
+ it "returns true when persisted with existing user" do
80
+ @store[:user_id] = 1
81
+ session = UserSession.new(@store, :user_id => 1)
82
+ assert_equal true, session.destroy
83
+ assert_nil @store[:user_id]
84
+ end
85
+
86
+ it "returns false when not persisted" do
87
+ @store[:user_id] = "666"
88
+ session = UserSession.new(@store, :user_id => 1)
89
+ assert_equal false, session.destroy
90
+ assert_equal "666", @store[:user_id]
91
+ end
92
+
93
+ end
94
+
95
+ describe "user" do
96
+
97
+ it "returns user with given user_id" do
98
+ session = UserSession.new(@store)
99
+ assert_nil session.user
100
+ session.user_id = 1
101
+ assert_equal 1, session.user.id
102
+ end
103
+
104
+ end
105
+
106
+ describe "user=" do
107
+
108
+ it "works properly with nil" do
109
+ session = UserSession.new(@store, :user => User.new(1))
110
+ session.user = nil
111
+ assert_nil session.user
112
+ assert_nil session.user_id
113
+ end
114
+
115
+ it "raises exception when non-user class given" do
116
+ session = UserSession.new(@store, :user_id => 1)
117
+ assert_raises(ActiveSession::AssociationTypeMismatch) { session.user = "zomg" }
118
+ end
119
+
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_session
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jakub Kuźma
9
+ - Wojciech Wnętrzak
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-10-19 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activemodel
17
+ requirement: &20755180 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.1.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *20755180
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &20754620 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *20754620
37
+ description: ActiveModel session
38
+ email:
39
+ - qoobaa@gmail.com
40
+ - w.wnetrzak@gmail.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - active_session.gemspec
51
+ - lib/active_session.rb
52
+ - lib/active_session/errors.rb
53
+ - lib/active_session/version.rb
54
+ - test/basic_session_test.rb
55
+ - test/helper.rb
56
+ - test/models/basic_session.rb
57
+ - test/models/user.rb
58
+ - test/models/user_session.rb
59
+ - test/user_session_test.rb
60
+ homepage: ''
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.6
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: ActiveModel session
84
+ test_files: []