instance-storage-1.0.0/0000755000175000017500000000000012615151015013027 5ustar daidaiinstance-storage-1.0.0/metadata.yml0000644000175000017500000000425412615151015015337 0ustar daidai--- !ruby/object:Gem::Specification name: instance_storage version: !ruby/object:Gem::Version version: 1.0.0 platform: ruby authors: - Toshiaki Asai autorequire: bindir: bin cert_chain: [] date: 2015-08-03 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.7' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.7' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '10.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '10.0' - !ruby/object:Gem::Dependency name: minitest requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' description: Manage class instances with dictionary. email: - toshi.alternative@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - ".gitignore" - Gemfile - LICENSE.txt - README.md - Rakefile - instance_storage.gemspec - lib/instance_storage.rb - lib/instance_storage/version.rb - test/instance_storage_test.rb homepage: '' licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.4.5 signing_key: specification_version: 4 summary: Manage class instances with dictionary test_files: - test/instance_storage_test.rb instance-storage-1.0.0/test/0000755000175000017500000000000012615151015014006 5ustar daidaiinstance-storage-1.0.0/test/instance_storage_test.rb0000644000175000017500000000247312615151015020730 0ustar daidai# -*- coding: utf-8 -*- require 'bundler/setup' require 'minitest/autorun' require 'instance_storage' describe(InstanceStorage) do it 'get and create instance' do klass = Class.new do include InstanceStorage end assert_same(klass[:foo], klass[:foo]) refute_same(klass[:foo], klass[:bar]) end it "get all instances" do klass = Class.new do include InstanceStorage end assert_equal([], klass.instances) assert_equal([klass[:a], klass[:b]], klass.instances) end it "get all instances name" do klass = Class.new do include InstanceStorage end assert_equal([], klass.instances_name) klass[:a] klass[:b] assert_equal([:a, :b], klass.instances_name) end it "destroy instance" do klass = Class.new do include InstanceStorage end klass[:a] assert(klass.instance_exist? :a) klass.destroy(:a) assert(! klass.instance_exist?(:a)) end it "get existing instance" do klass = Class.new do include InstanceStorage end assert_nil(klass.instance(:a)) assert_equal(klass[:a], klass.instance(:a)) end it "should support inherited class" do klass = Class.new do include InstanceStorage end child = Class.new(klass) assert_same(child[:foo], child[:foo]) refute_same(child[:foo], klass[:foo]) end end instance-storage-1.0.0/lib/0000755000175000017500000000000012615151015013575 5ustar daidaiinstance-storage-1.0.0/lib/instance_storage/0000755000175000017500000000000012615151015017125 5ustar daidaiinstance-storage-1.0.0/lib/instance_storage/version.rb0000644000175000017500000000005712615151015021141 0ustar daidaimodule InstanceStorage VERSION = "1.0.0" end instance-storage-1.0.0/lib/instance_storage.rb0000644000175000017500000000467212615151015017463 0ustar daidai# -*- coding: utf-8 -*- require "instance_storage/version" # クラスに、インスタンスの辞書をもたせる。 # このモジュールをincludeすると、全てのインスタンスは一意な名前(Symbol)をもつようになり、 # その名前を通してインスタンスを取得することができるようになる。 module InstanceStorage attr_reader :name alias to_sym name def self.included(klass) super klass.class_eval do extend InstanceStorageExtend end end def initialize(name) @name = name end # 名前を文字列にして返す # ==== Return # 名前文字列 def to_s @name.to_s end module InstanceStorageExtend def instances_dict @instances ||= {} end def storage_lock @storage_lock ||= Mutex.new end # 定義されているインスタンスを全て削除する def clear! @instances = @storage_lock = nil end # インスタンス _event_name_ を返す。既に有る場合はそのインスタンス、ない場合は新しく作って返す。 # ==== Args # [name] インスタンスの名前(Symbol) # ==== Return # Event def [](name) name_sym = name.to_sym if instances_dict.has_key?(name_sym) instances_dict[name_sym] else storage_lock.synchronize{ if instances_dict.has_key?(name_sym) instances_dict[name_sym] else instances_dict[name_sym] = self.new(name_sym) end } end end # このクラスのインスタンスを全て返す # ==== Return # インスタンスの配列(Array) def instances instances_dict.values end # このクラスのインスタンスの名前を全て返す # ==== Return # インスタンスの名前の配列(Array) def instances_name instances_dict.keys end # 名前 _name_ に対応するインスタンスが存在するか否かを返す # ==== Args # [name] インスタンスの名前(Symbol) # ==== Return # インスタンスが存在するなら真 def instance_exist?(name) instances_dict.has_key? name.to_sym end # _name_ に対応するインスタンスが既にあれば真 # ==== Args # [name] インスタンスの名前(Symbol) # ==== Return # インスタンスかnil def instance(name) instances_dict[name.to_sym] end def destroy(name) instances_dict.delete(name.to_sym) end end end instance-storage-1.0.0/instance_storage.gemspec0000644000175000017500000000165612615151015017734 0ustar daidai# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'instance_storage/version' Gem::Specification.new do |spec| spec.name = "instance_storage" spec.version = InstanceStorage::VERSION spec.authors = ["Toshiaki Asai"] spec.email = ["toshi.alternative@gmail.com"] spec.summary = %q{Manage class instances with dictionary} spec.description = %q{Manage class instances with dictionary.} spec.homepage = "" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest" end instance-storage-1.0.0/Rakefile0000644000175000017500000000025612615151015014477 0ustar daidairequire "bundler/gem_tasks" require "rake/testtask" Rake::TestTask.new do |t| t.test_files = FileList['test/*_test.rb'] t.warning = true t.verbose = true end instance-storage-1.0.0/README.md0000644000175000017500000000156512615151015014315 0ustar daidai# InstanceStorage クラスにincludeすると、クラスインスタンスごとにSymbolを割り振って、あとからその名前でインスタンスにアクセスする機能を提供します。 ## Installation Add this line to your application's Gemfile: ```ruby gem 'instance_storage' ``` And then execute: $ bundle Or install it yourself as: $ gem install instance_storage ## Usage ```ruby class Tag include InstanceStorage end foo = Tag[:foo] # generate instance `foo' foo.name # => :foo foo == Tag[:foo] # => true foo == Tag[:bar] # => false ``` ## Contributing 1. Fork it ( https://github.com/toshia/instance_storage/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request instance-storage-1.0.0/LICENSE.txt0000644000175000017500000000205612615151015014655 0ustar daidaiCopyright (c) 2015 Toshiaki Asai MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. instance-storage-1.0.0/Gemfile0000644000175000017500000000014512615151015014322 0ustar daidaisource 'https://rubygems.org' # Specify your gem's dependencies in instance_storage.gemspec gemspec instance-storage-1.0.0/.gitignore0000644000175000017500000000017612615151015015023 0ustar daidai/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ *.bundle *.so *.o *.a mkmf.log /vendor/