instance_storage 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/instance_storage.rb +22 -15
- data/lib/instance_storage/version.rb +1 -1
- data/test/instance_storage_test.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ab3fca4c92a0d5c88ff4fff5751feda09592ede
|
4
|
+
data.tar.gz: db4128fe44f81bd8895e9c9b3c38fe695047d034
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 747dabd4ac2cc2c925fc0127422bf350455d032ba79f9177b8cd29f8cef6ff4051ef9d9264923a93f714ad3046b9883e25e775feb7241d84cae51995d507a367
|
7
|
+
data.tar.gz: 6b590975c1845dba1ce2db36b315c35835332d7b57b384c1d5aefeb45f6375e02201a26728c64838a9453506faac5fc48c9e2555a177eafa400701713125bbec
|
data/lib/instance_storage.rb
CHANGED
@@ -11,8 +11,10 @@ module InstanceStorage
|
|
11
11
|
alias to_sym name
|
12
12
|
|
13
13
|
def self.included(klass)
|
14
|
-
|
15
|
-
klass.
|
14
|
+
super
|
15
|
+
klass.class_eval do
|
16
|
+
extend InstanceStorageExtend
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
18
20
|
def initialize(name)
|
@@ -25,10 +27,15 @@ module InstanceStorage
|
|
25
27
|
@name.to_s end
|
26
28
|
|
27
29
|
module InstanceStorageExtend
|
30
|
+
def instances_dict
|
31
|
+
@instances ||= {} end
|
32
|
+
|
33
|
+
def storage_lock
|
34
|
+
@storage_lock ||= Mutex.new end
|
35
|
+
|
28
36
|
# 定義されているインスタンスを全て削除する
|
29
37
|
def clear!
|
30
|
-
@instances =
|
31
|
-
@storage_lock = Mutex.new end
|
38
|
+
@instances = @storage_lock = nil end
|
32
39
|
|
33
40
|
# インスタンス _event_name_ を返す。既に有る場合はそのインスタンス、ない場合は新しく作って返す。
|
34
41
|
# ==== Args
|
@@ -37,26 +44,26 @@ module InstanceStorage
|
|
37
44
|
# Event
|
38
45
|
def [](name)
|
39
46
|
name_sym = name.to_sym
|
40
|
-
if
|
41
|
-
|
47
|
+
if instances_dict.has_key?(name_sym)
|
48
|
+
instances_dict[name_sym]
|
42
49
|
else
|
43
|
-
|
44
|
-
if
|
45
|
-
|
50
|
+
storage_lock.synchronize{
|
51
|
+
if instances_dict.has_key?(name_sym)
|
52
|
+
instances_dict[name_sym]
|
46
53
|
else
|
47
|
-
|
54
|
+
instances_dict[name_sym] = self.new(name_sym) end } end end
|
48
55
|
|
49
56
|
# このクラスのインスタンスを全て返す
|
50
57
|
# ==== Return
|
51
58
|
# インスタンスの配列(Array)
|
52
59
|
def instances
|
53
|
-
|
60
|
+
instances_dict.values end
|
54
61
|
|
55
62
|
# このクラスのインスタンスの名前を全て返す
|
56
63
|
# ==== Return
|
57
64
|
# インスタンスの名前の配列(Array)
|
58
65
|
def instances_name
|
59
|
-
|
66
|
+
instances_dict.keys end
|
60
67
|
|
61
68
|
# 名前 _name_ に対応するインスタンスが存在するか否かを返す
|
62
69
|
# ==== Args
|
@@ -64,7 +71,7 @@ module InstanceStorage
|
|
64
71
|
# ==== Return
|
65
72
|
# インスタンスが存在するなら真
|
66
73
|
def instance_exist?(name)
|
67
|
-
|
74
|
+
instances_dict.has_key? name.to_sym end
|
68
75
|
|
69
76
|
# _name_ に対応するインスタンスが既にあれば真
|
70
77
|
# ==== Args
|
@@ -72,9 +79,9 @@ module InstanceStorage
|
|
72
79
|
# ==== Return
|
73
80
|
# インスタンスかnil
|
74
81
|
def instance(name)
|
75
|
-
|
82
|
+
instances_dict[name.to_sym] end
|
76
83
|
|
77
84
|
def destroy(name)
|
78
|
-
|
85
|
+
instances_dict.delete(name.to_sym) end
|
79
86
|
end
|
80
87
|
end
|
@@ -46,4 +46,12 @@ describe(InstanceStorage) do
|
|
46
46
|
assert_equal(klass[:a], klass.instance(:a))
|
47
47
|
end
|
48
48
|
|
49
|
+
it "should support inherited class" do
|
50
|
+
klass = Class.new do
|
51
|
+
include InstanceStorage end
|
52
|
+
child = Class.new(klass)
|
53
|
+
assert_same(child[:foo], child[:foo])
|
54
|
+
refute_same(child[:foo], klass[:foo])
|
55
|
+
end
|
56
|
+
|
49
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: instance_storage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiaki Asai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|