niftie-multiton 0.6.5 → 2009.01

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/multiton.rb +34 -32
  2. data/multiton.gemspec +3 -3
  3. metadata +3 -3
data/lib/multiton.rb CHANGED
@@ -1,60 +1,62 @@
1
- require 'monitor'
1
+ require 'sync'
2
2
 
3
3
  module Multiton
4
4
  def self.append_features(base)
5
- base.const_set('INSTANCE_MAP', Multiton::Map.new)
6
- base.class_eval do
7
- def self.instance_map
8
- const_get('INSTANCE_MAP')
9
- end
10
-
11
- def self.instance(*args, &block)
12
- key = begin
13
- multiton_key(*args, &block)
14
- rescue NameError
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
24
- end
5
+ base.const_set(:INSTANCE_MAP, Map.new)
6
+
7
+ def base.instance_map
8
+ const_get(:INSTANCE_MAP)
9
+ end
10
+
11
+ def base.instance(*args, &block)
12
+ instance_map.lock.synchronize(:EX) do
13
+ instance_map[multiton_key(*args, &block)] ||= multiton_new(*args, &block)
25
14
  end
26
15
  end
16
+
17
+ def base.multiton_key(*args, &block)
18
+ args
19
+ end
20
+
21
+ def base.multiton_new(*args, &block)
22
+ new(*args, &block)
23
+ end
24
+
25
+ base.private_class_method(:multiton_key)
26
+ base.private_class_method(:multiton_new)
27
27
  end
28
28
 
29
29
  class Map
30
+ attr_reader :lock
31
+
30
32
  def initialize
31
- extend MonitorMixin
32
- @store = {}
33
+ @hash = Hash.new
34
+ @lock = Sync.new
33
35
  end
34
36
 
35
37
  def [](key)
36
- @store[key]
38
+ lock.synchronize(:SH) { @hash[key] }
37
39
  end
38
40
 
39
41
  def []=(key, value)
40
- synchronize { @store[key] = value }
42
+ lock.synchronize(:EX) { @hash[key] = value }
43
+ end
44
+
45
+ def size
46
+ lock.synchronize(:SH) { @hash.size }
41
47
  end
42
48
 
43
49
  def delete_if(&block)
44
- synchronize { @store.delete_if(&block) }
50
+ lock.synchronize(:EX) { @hash.delete_if(&block) }
45
51
  self
46
52
  end
47
53
 
48
54
  def delete(key)
49
- synchronize { @store.delete(key) }
55
+ lock.synchronize(:EX) { @hash.delete(key) }
50
56
  end
51
57
 
52
58
  def clear
53
- synchronize { @store.clear }
54
- end
55
-
56
- def size
57
- @store.size
59
+ lock.synchronize(:EX) { @hash.clear }
58
60
  end
59
61
  end
60
62
 
data/multiton.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'multiton'
3
- s.homepage = 'http://github.com/niftie/multiton'
3
+ s.homepage = 'http://github.comn/niftie/multiton'
4
4
  s.author = 'Nicolas Steenlant'
5
5
  s.email = 'nicolas.steenlant@gmail.com'
6
- s.version = '0.6.5'
7
- s.date = '2008-12-18'
6
+ s.version = '2009.01'
7
+ s.date = '2009-01-03'
8
8
  s.summary = 'implementation of the multiton pattern'
9
9
  s.files = ['README', '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.6.5
4
+ version: "2009.01"
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-18 00:00:00 -08:00
12
+ date: 2009-01-03 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,7 +26,7 @@ files:
26
26
  - multiton.gemspec
27
27
  - lib/multiton.rb
28
28
  has_rdoc: false
29
- homepage: http://github.com/niftie/multiton
29
+ homepage: http://github.comn/niftie/multiton
30
30
  post_install_message:
31
31
  rdoc_options: []
32
32