niftie-multiton 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/multiton.rb +54 -17
- data/multiton.gemspec +2 -2
- metadata +2 -2
data/lib/multiton.rb
CHANGED
@@ -1,26 +1,63 @@
|
|
1
|
-
require '
|
1
|
+
require 'monitor'
|
2
2
|
|
3
3
|
module Multiton
|
4
|
-
|
5
|
-
|
4
|
+
def self.extended(base)
|
5
|
+
base.instance_variable_set(:@instance_map, Map.new)
|
6
|
+
class << base
|
7
|
+
attr_reader :instance_map
|
8
|
+
end
|
9
|
+
end
|
6
10
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
else
|
13
|
-
args
|
11
|
+
def inherited(desc)
|
12
|
+
super
|
13
|
+
class << desc
|
14
|
+
def instance_map
|
15
|
+
superclass.instance_map
|
14
16
|
end
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def instance(*args, &block)
|
21
|
+
key = begin
|
22
|
+
multiton_key(*args, &block)
|
23
|
+
rescue NameError
|
24
|
+
args
|
25
|
+
end
|
26
|
+
|
27
|
+
instance_map.synchronize do
|
28
|
+
instance_map[key] ||= begin
|
29
|
+
multiton_new(*args, &block)
|
30
|
+
rescue NameError
|
31
|
+
new(*args, &block)
|
22
32
|
end
|
23
33
|
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Map
|
37
|
+
def initialize
|
38
|
+
@store = {}
|
39
|
+
extend MonitorMixin
|
40
|
+
end
|
24
41
|
|
42
|
+
def [](key)
|
43
|
+
@store[key]
|
44
|
+
end
|
45
|
+
|
46
|
+
def []=(key, val)
|
47
|
+
synchronize { @store[key] = val }
|
48
|
+
end
|
49
|
+
|
50
|
+
def clear
|
51
|
+
synchronize { @store.clear }
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete(key)
|
55
|
+
synchronize { @store.delete(key) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def store
|
59
|
+
synchronize { yield @store }
|
60
|
+
end
|
25
61
|
end
|
62
|
+
|
26
63
|
end
|
data/multiton.gemspec
CHANGED
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.homepage = 'http://github.com/niftie/multiton'
|
4
4
|
s.author = 'Nicolas Steenlant'
|
5
5
|
s.email = 'nicolas.steenlant@gmail.com'
|
6
|
-
s.version = '0.
|
7
|
-
s.date = '2008-11-
|
6
|
+
s.version = '0.4'
|
7
|
+
s.date = '2008-11-27'
|
8
8
|
s.summary = ''
|
9
9
|
s.files = ['multiton.gemspec', 'lib/multiton.rb']
|
10
10
|
s.require_path = 'lib'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: niftie-multiton
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.4"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Steenlant
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-27 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|