niftie-multiton 0.6.1 → 0.6.5
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.
- data/README +0 -0
- data/lib/multiton.rb +17 -113
- data/multiton.gemspec +4 -4
- metadata +4 -3
data/README
ADDED
File without changes
|
data/lib/multiton.rb
CHANGED
@@ -1,32 +1,26 @@
|
|
1
1
|
require 'monitor'
|
2
2
|
|
3
|
-
module Kernel
|
4
|
-
def Multiton(limit)
|
5
|
-
const_set('INSTANCE_MAP', Multiton::LRUMap.new(limit))
|
6
|
-
Multiton
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
3
|
module Multiton
|
11
4
|
def self.append_features(base)
|
12
|
-
base.const_set('INSTANCE_MAP', Multiton::Map.new)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def base.instance(*args, &block)
|
19
|
-
key = begin
|
20
|
-
multiton_key(*args, &block)
|
21
|
-
rescue NameError
|
22
|
-
args
|
5
|
+
base.const_set('INSTANCE_MAP', Multiton::Map.new)
|
6
|
+
base.class_eval do
|
7
|
+
def self.instance_map
|
8
|
+
const_get('INSTANCE_MAP')
|
23
9
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
10
|
+
|
11
|
+
def self.instance(*args, &block)
|
12
|
+
key = begin
|
13
|
+
multiton_key(*args, &block)
|
28
14
|
rescue NameError
|
29
|
-
|
15
|
+
args
|
16
|
+
end
|
17
|
+
|
18
|
+
instance_map.synchronize do
|
19
|
+
instance_map[key] ||= begin
|
20
|
+
multiton_new(*args, &block)
|
21
|
+
rescue NameError
|
22
|
+
new(*args, &block)
|
23
|
+
end
|
30
24
|
end
|
31
25
|
end
|
32
26
|
end
|
@@ -64,94 +58,4 @@ module Multiton
|
|
64
58
|
end
|
65
59
|
end
|
66
60
|
|
67
|
-
class LRUMap < Map
|
68
|
-
Item = Struct.new(:key, :value, :prev, :next)
|
69
|
-
|
70
|
-
def initialize(limit)
|
71
|
-
raise ArgumentError, "limit must be an Integer" unless limit.is_a? Integer
|
72
|
-
super()
|
73
|
-
@limit = limit
|
74
|
-
setup
|
75
|
-
end
|
76
|
-
|
77
|
-
def [](key)
|
78
|
-
if item = super
|
79
|
-
synchronize do
|
80
|
-
remove(item)
|
81
|
-
append(head, item)
|
82
|
-
end
|
83
|
-
item.value
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def []=(key, value)
|
88
|
-
item = Item.new(key, value)
|
89
|
-
synchronize do
|
90
|
-
super(key, item)
|
91
|
-
|
92
|
-
delete(last.key) if size > @limit
|
93
|
-
append(head, item)
|
94
|
-
end
|
95
|
-
item.value
|
96
|
-
end
|
97
|
-
|
98
|
-
def delete_if
|
99
|
-
synchronize do
|
100
|
-
@store.keys.each { |key| delete(key) if yield key, @store[key].value }
|
101
|
-
end
|
102
|
-
self
|
103
|
-
end
|
104
|
-
|
105
|
-
def delete(key)
|
106
|
-
synchronize do
|
107
|
-
if item = super
|
108
|
-
remove(item)
|
109
|
-
return item.value
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def clear
|
115
|
-
synchronize do
|
116
|
-
super
|
117
|
-
setup
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
private
|
122
|
-
|
123
|
-
def head
|
124
|
-
@head
|
125
|
-
end
|
126
|
-
|
127
|
-
def tail
|
128
|
-
@tail
|
129
|
-
end
|
130
|
-
|
131
|
-
def last
|
132
|
-
@tail.prev
|
133
|
-
end
|
134
|
-
|
135
|
-
def setup
|
136
|
-
@head = Item.new
|
137
|
-
@tail = Item.new
|
138
|
-
join(head, tail)
|
139
|
-
end
|
140
|
-
|
141
|
-
def remove(item)
|
142
|
-
join(item.prev, item.next)
|
143
|
-
item
|
144
|
-
end
|
145
|
-
|
146
|
-
def append(x, y)
|
147
|
-
join(y, x.next)
|
148
|
-
join(x, y)
|
149
|
-
end
|
150
|
-
|
151
|
-
def join(x, y)
|
152
|
-
x.next = y
|
153
|
-
y.prev = x
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
61
|
end
|
data/multiton.gemspec
CHANGED
@@ -3,9 +3,9 @@ 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.6.
|
7
|
-
s.date = '2008-12-
|
8
|
-
s.summary = ''
|
9
|
-
s.files = ['multiton.gemspec', 'lib/multiton.rb']
|
6
|
+
s.version = '0.6.5'
|
7
|
+
s.date = '2008-12-18'
|
8
|
+
s.summary = 'implementation of the multiton pattern'
|
9
|
+
s.files = ['README', 'multiton.gemspec', 'lib/multiton.rb']
|
10
10
|
s.require_path = 'lib'
|
11
11
|
end
|
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.6.
|
4
|
+
version: 0.6.5
|
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-12-
|
12
|
+
date: 2008-12-18 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,6 +22,7 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
+
- README
|
25
26
|
- multiton.gemspec
|
26
27
|
- lib/multiton.rb
|
27
28
|
has_rdoc: false
|
@@ -49,6 +50,6 @@ rubyforge_project:
|
|
49
50
|
rubygems_version: 1.2.0
|
50
51
|
signing_key:
|
51
52
|
specification_version: 2
|
52
|
-
summary:
|
53
|
+
summary: implementation of the multiton pattern
|
53
54
|
test_files: []
|
54
55
|
|