instance_storage 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +17 -0
- data/instance_storage.gemspec +24 -0
- data/lib/instance_storage.rb +80 -0
- data/lib/instance_storage/version.rb +3 -0
- data/test/instance_storage_test.rb +49 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d979de0479edc2e246b2dec5074f04aa378fd8e4
|
4
|
+
data.tar.gz: 5ce55b2dfd968ceec11d720d0594e5d4fa52736e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 612331ea0098cfc273c31b8aeae1e79a9332bc062fa0691aad3f00c327ccc0ea98944621a1eec6ee9907e0b5840bc0113ab82149c12abcb1ff4958916ca455ad
|
7
|
+
data.tar.gz: 4cee8191d7c163848b0a1ebe2dc12c6c8a5fa3554da2ddb7d6e94034736ab5c81e5725865b51bc14d448d57ce1fa8c0ab5ec60bb4853e8ec1cfc20fd1ea3204d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Toshiaki Asai
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# InstanceStorage
|
2
|
+
|
3
|
+
クラスにincludeすると、クラスインスタンスごとにSymbolを割り振って、あとからその名前でインスタンスにアクセスする機能を提供します。
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'instance_storage'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install instance_storage
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Tag
|
25
|
+
include InstanceStorage
|
26
|
+
end
|
27
|
+
|
28
|
+
foo = Tag[:foo] # generate instance `foo'
|
29
|
+
foo.name # => :foo
|
30
|
+
foo == Tag[:foo] # => true
|
31
|
+
foo == Tag[:bar] # => false
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( https://github.com/toshia/instance_storage/fork )
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'instance_storage/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "instance_storage"
|
8
|
+
spec.version = InstanceStorage::VERSION
|
9
|
+
spec.authors = ["Toshiaki Asai"]
|
10
|
+
spec.email = ["toshi.alternative@gmail.com"]
|
11
|
+
spec.summary = %q{Manage class instances with dictionary}
|
12
|
+
spec.description = %q{Manage class instances with dictionary.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "instance_storage/version"
|
3
|
+
|
4
|
+
# クラスに、インスタンスの辞書をもたせる。
|
5
|
+
# このモジュールをincludeすると、全てのインスタンスは一意な名前(Symbol)をもつようになり、
|
6
|
+
# その名前を通してインスタンスを取得することができるようになる。
|
7
|
+
module InstanceStorage
|
8
|
+
|
9
|
+
attr_reader :name
|
10
|
+
|
11
|
+
alias to_sym name
|
12
|
+
|
13
|
+
def self.included(klass)
|
14
|
+
klass.extend InstanceStorageExtend
|
15
|
+
klass.clear!
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(name)
|
19
|
+
@name = name end
|
20
|
+
|
21
|
+
# 名前を文字列にして返す
|
22
|
+
# ==== Return
|
23
|
+
# 名前文字列
|
24
|
+
def to_s
|
25
|
+
@name.to_s end
|
26
|
+
|
27
|
+
module InstanceStorageExtend
|
28
|
+
# 定義されているインスタンスを全て削除する
|
29
|
+
def clear!
|
30
|
+
@instances = {}
|
31
|
+
@storage_lock = Mutex.new end
|
32
|
+
|
33
|
+
# インスタンス _event_name_ を返す。既に有る場合はそのインスタンス、ない場合は新しく作って返す。
|
34
|
+
# ==== Args
|
35
|
+
# [name] インスタンスの名前(Symbol)
|
36
|
+
# ==== Return
|
37
|
+
# Event
|
38
|
+
def [](name)
|
39
|
+
name_sym = name.to_sym
|
40
|
+
if @instances.has_key?(name_sym)
|
41
|
+
@instances[name_sym]
|
42
|
+
else
|
43
|
+
@storage_lock.synchronize{
|
44
|
+
if @instances.has_key?(name_sym)
|
45
|
+
@instances[name_sym]
|
46
|
+
else
|
47
|
+
@instances[name_sym] = self.new(name_sym) end } end end
|
48
|
+
|
49
|
+
# このクラスのインスタンスを全て返す
|
50
|
+
# ==== Return
|
51
|
+
# インスタンスの配列(Array)
|
52
|
+
def instances
|
53
|
+
@instances.values end
|
54
|
+
|
55
|
+
# このクラスのインスタンスの名前を全て返す
|
56
|
+
# ==== Return
|
57
|
+
# インスタンスの名前の配列(Array)
|
58
|
+
def instances_name
|
59
|
+
@instances.keys end
|
60
|
+
|
61
|
+
# 名前 _name_ に対応するインスタンスが存在するか否かを返す
|
62
|
+
# ==== Args
|
63
|
+
# [name] インスタンスの名前(Symbol)
|
64
|
+
# ==== Return
|
65
|
+
# インスタンスが存在するなら真
|
66
|
+
def instance_exist?(name)
|
67
|
+
@instances.has_key? name.to_sym end
|
68
|
+
|
69
|
+
# _name_ に対応するインスタンスが既にあれば真
|
70
|
+
# ==== Args
|
71
|
+
# [name] インスタンスの名前(Symbol)
|
72
|
+
# ==== Return
|
73
|
+
# インスタンスかnil
|
74
|
+
def instance(name)
|
75
|
+
@instances[name.to_sym] end
|
76
|
+
|
77
|
+
def destroy(name)
|
78
|
+
@instances.delete(name.to_sym) end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
require 'instance_storage'
|
7
|
+
|
8
|
+
describe(InstanceStorage) do
|
9
|
+
|
10
|
+
it 'get and create instance' do
|
11
|
+
klass = Class.new do
|
12
|
+
include InstanceStorage end
|
13
|
+
assert_same(klass[:foo], klass[:foo])
|
14
|
+
refute_same(klass[:foo], klass[:bar])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "get all instances" do
|
18
|
+
klass = Class.new do
|
19
|
+
include InstanceStorage end
|
20
|
+
assert_equal([], klass.instances)
|
21
|
+
assert_equal([klass[:a], klass[:b]], klass.instances)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "get all instances name" do
|
25
|
+
klass = Class.new do
|
26
|
+
include InstanceStorage end
|
27
|
+
assert_equal([], klass.instances_name)
|
28
|
+
klass[:a]
|
29
|
+
klass[:b]
|
30
|
+
assert_equal([:a, :b], klass.instances_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "destroy instance" do
|
34
|
+
klass = Class.new do
|
35
|
+
include InstanceStorage end
|
36
|
+
klass[:a]
|
37
|
+
assert(klass.instance_exist? :a)
|
38
|
+
klass.destroy(:a)
|
39
|
+
assert(! klass.instance_exist?(:a))
|
40
|
+
end
|
41
|
+
|
42
|
+
it "get existing instance" do
|
43
|
+
klass = Class.new do
|
44
|
+
include InstanceStorage end
|
45
|
+
assert_nil(klass.instance(:a))
|
46
|
+
assert_equal(klass[:a], klass.instance(:a))
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: instance_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Toshiaki Asai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Manage class instances with dictionary.
|
56
|
+
email:
|
57
|
+
- toshi.alternative@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- instance_storage.gemspec
|
68
|
+
- lib/instance_storage.rb
|
69
|
+
- lib/instance_storage/version.rb
|
70
|
+
- test/instance_storage_test.rb
|
71
|
+
homepage: ''
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Manage class instances with dictionary
|
95
|
+
test_files:
|
96
|
+
- test/instance_storage_test.rb
|