active_session 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ActiveSession
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -26,27 +26,35 @@ module ActiveSession
26
26
  end
27
27
 
28
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
29
+ klass = options.fetch(:class_name, name.to_s.classify)
30
+ foreign_key = options.fetch(:foreign_key, "#{name}_id")
31
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
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
+ END
36
38
 
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
39
+ class_eval <<-END, __FILE__, __LINE__ + 1
40
+ def #{name}=(model)
41
+ raise ActiveSession::AssociationTypeMismatch, "#{klass} expected, got \#\{model.class\}" unless model.nil? or model.is_a?(#{klass})
42
+ self.#{foreign_key} = model.try(:id)
43
+ end
44
+ END
41
45
 
42
- define_method(foreign_key) do
43
- @attributes[foreign_key]
44
- end
46
+ class_eval <<-END, __FILE__, __LINE__ + 1
47
+ def #{foreign_key}
48
+ @attributes[:#{foreign_key}]
49
+ end
50
+ END
45
51
 
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
52
+ class_eval <<-END, __FILE__, __LINE__ + 1
53
+ def #{foreign_key}=(model_id)
54
+ remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
55
+ @attributes[:#{foreign_key}] = model_id
56
+ end
57
+ END
50
58
  end
51
59
  end
52
60
 
@@ -13,6 +13,11 @@ describe UserSession do
13
13
 
14
14
  describe "valid?" do
15
15
 
16
+ it "returns false if new record" do
17
+ session = UserSession.new(@store)
18
+ assert_equal false, session.valid?
19
+ end
20
+
16
21
  it "returns true with existing user_id" do
17
22
  session = UserSession.new(@store, :user_id => 1)
18
23
  assert_equal true, session.valid?
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.1
4
+ version: 0.0.2
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: &20755180 !ruby/object:Gem::Requirement
17
+ requirement: &19802200 !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: *20755180
25
+ version_requirements: *19802200
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &20754620 !ruby/object:Gem::Requirement
28
+ requirement: &19801700 !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: *20754620
36
+ version_requirements: *19801700
37
37
  description: ActiveModel session
38
38
  email:
39
39
  - qoobaa@gmail.com