wref 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Gemfile.lock +2 -0
  2. data/VERSION +1 -1
  3. data/lib/wref.rb +27 -20
  4. data/spec/wref_spec.rb +23 -15
  5. data/wref.gemspec +2 -2
  6. metadata +3 -3
data/Gemfile.lock CHANGED
@@ -9,6 +9,7 @@ GEM
9
9
  rake
10
10
  rdoc
11
11
  json (1.7.1)
12
+ json (1.7.1-java)
12
13
  rake (0.9.2.2)
13
14
  rdoc (3.12)
14
15
  json (~> 1.4)
@@ -22,6 +23,7 @@ GEM
22
23
  rspec-mocks (2.8.0)
23
24
 
24
25
  PLATFORMS
26
+ java
25
27
  ruby
26
28
 
27
29
  DEPENDENCIES
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/lib/wref.rb CHANGED
@@ -1,7 +1,9 @@
1
+ require "java" if RUBY_ENGINE == "jruby"
2
+
1
3
  #A simple weak-reference framework with mapping. Only handles the referencing of objects.
2
4
  #===Examples
3
5
  # user_obj = ob.get(:User, 1)
4
- # weak_ref = Wref(user_obj)
6
+ # weak_ref = Wref.new(user_obj)
5
7
  # user_obj = nil
6
8
  # sleep 0.5
7
9
  # GC.start
@@ -21,20 +23,27 @@ class Wref
21
23
 
22
24
  #Initializes various variables.
23
25
  def initialize(obj)
24
- @id = obj.__id__
25
-
26
26
  if RUBY_ENGINE == "jruby"
27
- require "java"
28
27
  @weakref = java.lang.ref.WeakReference.new(obj)
29
28
  else
29
+ @id = obj.__id__
30
30
  @class_name = obj.class.name.to_sym
31
+ ObjectSpace.define_finalizer(obj, self.method(:destroy))
31
32
 
32
- if obj.respond_to?("__object_unique_id__")
33
+ if obj.respond_to?(:__object_unique_id__)
33
34
  @unique_id = obj.__object_unique_id__
34
35
  end
35
36
  end
36
37
  end
37
38
 
39
+ #Destroyes most variables on the object, releasing memory and returning 'Wref::Recycled' all the time.
40
+ def destroy
41
+ @id = nil
42
+ @class_name = nil
43
+ @unique_id = nil
44
+ @weakref = nil
45
+ end
46
+
38
47
  #Returns the object that this weak reference holds or raises Wref::Recycled.
39
48
  # begin
40
49
  # obj = wref.get
@@ -44,9 +53,8 @@ class Wref
44
53
  # end
45
54
  def get
46
55
  begin
47
- raise Wref::Recycled if !@class_name or !@id
48
-
49
56
  if RUBY_ENGINE == "jruby"
57
+ raise Wref::Recycled if !@weakref
50
58
  obj = @weakref.get
51
59
 
52
60
  if obj == nil
@@ -55,23 +63,22 @@ class Wref
55
63
  return obj
56
64
  end
57
65
  else
66
+ raise Wref::Recycled if !@class_name or !@id
58
67
  obj = ObjectSpace._id2ref(@id)
59
- end
60
-
61
- #Some times this class-name will be nil for some reason - knj
62
- obj_class_name = obj.class.name
63
-
64
- if !obj_class_name or @class_name != obj_class_name.to_sym or @id != obj.__id__
65
- raise Wref::Recycled
66
- end
67
-
68
- if @unique_id
69
- if !obj.respond_to?("__object_unique_id__") or obj.__object_unique_id__ != @unique_id
68
+
69
+ #Some times this class-name will be nil for some reason - knj
70
+ obj_class_name = obj.class.name
71
+
72
+ if !obj_class_name or @class_name != obj_class_name.to_sym or @id != obj.__id__
70
73
  raise Wref::Recycled
71
74
  end
75
+
76
+ if @unique_id
77
+ raise Wref::Recycled if !obj.respond_to?(:__object_unique_id__) or obj.__object_unique_id__ != @unique_id
78
+ end
79
+
80
+ return obj
72
81
  end
73
-
74
- return obj
75
82
  rescue RangeError, TypeError
76
83
  raise Wref::Recycled
77
84
  end
data/spec/wref_spec.rb CHANGED
@@ -2,20 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Wref" do
4
4
  it "should not fail" do
5
- str = "Test"
6
- ref = Wref.new(str)
7
- raise "Should have been alive but wasnt." if !ref.alive?
8
- str = nil
9
- GC.start
10
- raise "Should have been GCed but wasnt." if ref.alive?
11
-
12
-
13
- str = "Test"
14
- map = Wref_map.new
15
- map[5] = str
16
- raise "Should have been valid but wasnt." if !map.valid?(5)
17
- str = nil
18
- GC.start
19
- raise "Should habe been garbage collected but wasnt." if !map.valid?(5)
5
+ #This test does not work under JRuby.
6
+ if RUBY_ENGINE != "jruby"
7
+ str = "Test"
8
+ ref = Wref.new(str)
9
+ raise "Should have been alive but wasnt." if !ref.alive?
10
+ str = nil
11
+
12
+ #In MRI we have to define another object finalizer, before the last will be finalized.
13
+ str2 = "Test 2"
14
+ ref2 = Wref.new(str2)
15
+ ref2 = nil
16
+ GC.start
17
+ raise "Should have been GCed but wasnt." if ref.alive?
18
+
19
+
20
+ str = "Test"
21
+ map = Wref_map.new
22
+ map[5] = str
23
+ raise "Should have been valid but wasnt." if !map.valid?(5)
24
+ str = nil
25
+ GC.start
26
+ raise "Should habe been garbage collected but wasnt." if !map.valid?(5)
27
+ end
20
28
  end
21
29
  end
data/wref.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wref}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-05-10}
12
+ s.date = %q{2012-05-12}
13
13
  s.description = %q{Lightweight weak reference and weak hash that works in 1.9 and JRuby.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wref
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.0
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-10 00:00:00 +02:00
13
+ date: 2012-05-12 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- hash: -4333171512268985658
96
+ hash: 3404293009183367169
97
97
  segments:
98
98
  - 0
99
99
  version: "0"