active_session 0.0.3 → 0.0.4

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,46 @@
1
+ module ActiveSession
2
+ module AttributeMethods
3
+ def initialize(store, attributes = nil, options = {})
4
+ @store = store
5
+ @attributes = {}
6
+ assign_attributes(attributes, options) if attributes
7
+ yield self if block_given?
8
+ end
9
+
10
+ def attributes
11
+ @attributes
12
+ end
13
+
14
+ def attributes=(attributes)
15
+ assign_attributes(attributes)
16
+ end
17
+
18
+ def assign_attributes(new_attributes, options = {})
19
+ return unless new_attributes
20
+
21
+ attributes = new_attributes.stringify_keys
22
+
23
+ unless options[:without_protection]
24
+ attributes = sanitize_for_mass_assignment(attributes, options.fetch(:as, :default))
25
+ end
26
+
27
+ attributes.each do |key, value|
28
+ if respond_to?("#{key}=")
29
+ send("#{key}=", value)
30
+ else
31
+ raise ActiveSession::UnknownAttributeError, "unknown attribute: #{key}"
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def attribute(attribute)
39
+ @attributes[attribute.to_sym]
40
+ end
41
+
42
+ def attribute=(attribute, value)
43
+ @attributes[attribute.to_sym] = value
44
+ end
45
+ end
46
+ end
@@ -10,8 +10,4 @@ module ActiveSession
10
10
  class AssociationTypeMismatch < StandardError
11
11
  include ActiveSessionError
12
12
  end
13
-
14
- class StoreValueNotDefined < StandardError
15
- include ActiveSessionError
16
- end
17
13
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveSession
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,115 +1,58 @@
1
1
  require "active_model"
2
- require "active_support/concern"
3
2
  require "active_support/core_ext"
3
+ require "active_session/attribute_methods"
4
4
  require "active_session/errors"
5
5
 
6
6
  module ActiveSession
7
- extend ActiveSupport::Concern
7
+ def authenticates(name, options = {})
8
+ klass = options.fetch(:class_name, name.to_s.classify)
9
+ foreign_key = options.fetch(:foreign_key, "#{name}_id").to_sym
10
+ primary_key = options.fetch(:primary_key, "id").to_sym
11
+ finder_method = options.fetch(:finder_method, "find_for_authentication").to_sym
8
12
 
9
- included do
10
13
  extend ActiveModel::Naming
11
14
  include ActiveModel::AttributeMethods
12
15
  include ActiveModel::Conversion
13
16
  include ActiveModel::MassAssignmentSecurity
14
17
  include ActiveModel::Validations
15
18
 
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)
30
- foreign_key = options.fetch(:foreign_key, "#{name}_id")
31
-
32
- class_eval <<-END, __FILE__, __LINE__ + 1
33
- def #{name}
34
- return @#{name} if defined?(@#{name})
35
- @#{name} = #{klass}.find(#{foreign_key})
36
- end
37
-
38
- def #{name}=(model)
39
- raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got \#\{model.class\}" unless model.nil? or model.is_a?(#{klass})
40
- self.#{foreign_key} = model.try(:id)
41
- end
42
-
43
- def #{foreign_key}
44
- @attributes[:#{foreign_key}]
45
- end
46
-
47
- def #{foreign_key}=(model_id)
48
- remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
49
- @attributes[:#{foreign_key}] = model_id
50
- end
51
- END
52
- end
53
- end
54
-
55
- module InstanceMethods
56
- def initialize(store, attributes = nil, options = {})
57
- @store = store
58
- @attributes = {}
59
- assign_attributes(attributes, options) if attributes
60
- yield self if block_given?
61
- end
19
+ include AttributeMethods
62
20
 
63
- def attributes=(attributes)
64
- assign_attributes(attributes)
65
- end
66
-
67
- def assign_attributes(new_attributes, options = {})
68
- return unless new_attributes
21
+ attribute_method_suffix "", "="
69
22
 
70
- attributes = new_attributes.stringify_keys
23
+ validates name, :presence => true
71
24
 
72
- unless options[:without_protection]
73
- attributes = sanitize_for_mass_assignment(attributes, options.fetch(:as, :default))
25
+ class_eval <<-END, __FILE__, __LINE__ + 1
26
+ def #{name}
27
+ return @#{name} if defined?(@#{name})
28
+ @#{name} = #{klass}.#{finder_method}(:#{primary_key} => #{foreign_key})
74
29
  end
75
30
 
76
- attributes.each do |key, value|
77
- if respond_to?("#{key}=")
78
- send("#{key}=", value)
79
- else
80
- raise ActiveSession::UnknownAttributeError, "unknown attribute: #{key}"
81
- end
31
+ def #{name}=(model)
32
+ raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got \#{model.class}" unless model.nil? or model.is_a?(#{klass})
33
+ self.#{foreign_key} = model.try(:#{primary_key})
82
34
  end
83
- end
84
-
85
- def persisted?
86
- @store[self.class.store_key].present? and @store[self.class.store_key] == store_value
87
- end
88
-
89
- def destroy
90
- persisted? && self.class.clear_store(@store) && true
91
- end
92
-
93
- def store_value
94
- raise ActiveSession::StoreValueNotDefined, "you have to override store_value method to return something useful (usually user_id)"
95
- end
96
35
 
97
- def save
98
- valid? && serialize && true
99
- end
36
+ def #{foreign_key}
37
+ @attributes[:#{foreign_key}]
38
+ end
100
39
 
101
- private
40
+ def #{foreign_key}=(model_id)
41
+ remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
42
+ @attributes[:#{foreign_key}] = model_id
43
+ end
102
44
 
103
- def attribute(attribute)
104
- @attributes[attribute.to_sym]
105
- end
45
+ def persisted?
46
+ @store[:#{foreign_key}].present? and @store[:#{foreign_key}] == #{foreign_key}
47
+ end
106
48
 
107
- def attribute=(attribute, value)
108
- @attributes[attribute.to_sym] = value
109
- end
49
+ def destroy
50
+ persisted? && @store.delete(:#{foreign_key}) && true
51
+ end
110
52
 
111
- def serialize
112
- @store[self.class.store_key] = store_value
113
- end
53
+ def save
54
+ valid? && (@store[:#{foreign_key}] = #{foreign_key}) && true
55
+ end
56
+ END
114
57
  end
115
58
  end
data/test/models/user.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  class User
2
2
  attr_reader :id
3
3
 
4
- def self.find(id)
5
- new(id) if id.to_i > 0
4
+ def self.find_for_authentication(conditions)
5
+ new(conditions[:id]) if conditions[:id].to_i > 0
6
6
  end
7
7
 
8
8
  def initialize(id)
@@ -1,10 +1,4 @@
1
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
2
+ extend ActiveSession
3
+ authenticates :user
10
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2011-10-19 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: &13989540 !ruby/object:Gem::Requirement
17
+ requirement: &16721260 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.1.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *13989540
25
+ version_requirements: *16721260
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &13972600 !ruby/object:Gem::Requirement
28
+ requirement: &16720720 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 3.1.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *13972600
36
+ version_requirements: *16720720
37
37
  description: ActiveModel session
38
38
  email:
39
39
  - qoobaa@gmail.com
@@ -49,11 +49,10 @@ files:
49
49
  - Rakefile
50
50
  - active_session.gemspec
51
51
  - lib/active_session.rb
52
+ - lib/active_session/attribute_methods.rb
52
53
  - lib/active_session/errors.rb
53
54
  - lib/active_session/version.rb
54
- - test/basic_session_test.rb
55
55
  - test/helper.rb
56
- - test/models/basic_session.rb
57
56
  - test/models/user.rb
58
57
  - test/models/user_session.rb
59
58
  - test/user_session_test.rb
@@ -1,12 +0,0 @@
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
@@ -1,3 +0,0 @@
1
- class BasicSession
2
- include ActiveSession
3
- end