instance_storage-2.0.0/0000755000004100000410000000000014052714044015072 5ustar www-datawww-datainstance_storage-2.0.0/test/0000755000004100000410000000000014052714044016051 5ustar www-datawww-datainstance_storage-2.0.0/test/instance_storage_test.rb0000644000004100000410000000253014052714044022765 0ustar www-datawww-data# -*- coding: utf-8 -*- require 'bundler/setup' require 'minitest/autorun' require 'instance_storage' class InstanceStorageTest < MiniTest::Unit::TestCase def test_get_and_create_instance klass = Class.new do include InstanceStorage end assert_same(klass[:foo], klass[:foo]) refute_same(klass[:foo], klass[:bar]) end def test_get_all_instances klass = Class.new do include InstanceStorage end assert_equal([], klass.instances) assert_equal([klass[:a], klass[:b]], klass.instances) end def test_get_all_instances_name klass = Class.new do include InstanceStorage end assert_equal([], klass.instances_name) klass[:a] klass[:b] assert_equal([:a, :b], klass.instances_name) end def test_destroy_instance klass = Class.new do include InstanceStorage end klass[:a] assert(klass.instance_exist? :a) klass.destroy(:a) assert(! klass.instance_exist?(:a)) end def test_get_existing_instance klass = Class.new do include InstanceStorage end assert_nil(klass.instance(:a)) assert_equal(klass[:a], klass.instance(:a)) end def test_should_support_inherited_class 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-2.0.0/README.md0000644000004100000410000000156514052714044016360 0ustar www-datawww-data# 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-2.0.0/.gitignore0000644000004100000410000000017614052714044017066 0ustar www-datawww-data/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ *.bundle *.so *.o *.a mkmf.log /vendor/instance_storage-2.0.0/instance_storage.gemspec0000644000004100000410000000201514052714044021765 0ustar www-datawww-data# 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 = "https://github.com/toshia/instance_storage" 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.required_ruby_version = '>= 2.6.0' spec.add_development_dependency "bundler", "~> 2.0" spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "minitest", "~> 5.1" end instance_storage-2.0.0/Rakefile0000644000004100000410000000025614052714044016542 0ustar www-datawww-datarequire "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-2.0.0/lib/0000755000004100000410000000000014052714044015640 5ustar www-datawww-datainstance_storage-2.0.0/lib/instance_storage/0000755000004100000410000000000014052714044021170 5ustar www-datawww-datainstance_storage-2.0.0/lib/instance_storage/version.rb0000644000004100000410000000005714052714044023204 0ustar www-datawww-datamodule InstanceStorage VERSION = "2.0.0" end instance_storage-2.0.0/lib/instance_storage.rb0000644000004100000410000000471714052714044021526 0ustar www-datawww-data# -*- 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.to_sym 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 = nil self 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) self end end end instance_storage-2.0.0/sig/0000755000004100000410000000000014052714044015654 5ustar www-datawww-datainstance_storage-2.0.0/sig/instance_storage.rbs0000644000004100000410000000136014052714044021714 0ustar www-datawww-data# TypeProf 0.13.0 # Classes module InstanceStorage VERSION: String extend InstanceStorageExtend attr_reader name: Symbol alias to_sym name def self.included: (Class) -> void def initialize: (Symbol|String) -> void def to_s: -> String module InstanceStorageExtend @instances: Hash[Symbol, self]? @storage_lock: Thread::Mutex? def instances_dict: -> Hash[Symbol, self] def storage_lock: -> Thread::Mutex def clear!: -> self def []: (Symbol|String) -> self def instances: -> Array[self] def instances_name: -> Array[Symbol] def instance_exist?: (Symbol|String) -> bool def instance: (Symbol|String) -> self def destroy: (Symbol|String) -> self def new: (Symbol|String) -> self end end instance_storage-2.0.0/Gemfile0000644000004100000410000000014514052714044016365 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in instance_storage.gemspec gemspec instance_storage-2.0.0/Steepfile0000644000004100000410000000075014052714044016737 0ustar www-datawww-datatarget :lib do signature "sig" check "lib" # Directory name # check "Gemfile" # File name # ignore "lib/templates/*.rb" # library "pathname", "set" # Standard libraries # library "strong_json" # Gems end target :test do signature "sig" check "test" end # target :spec do # signature "sig", "sig-private" # # check "spec" # # # library "pathname", "set" # Standard libraries # # library "rspec" # end instance_storage-2.0.0/LICENSE.txt0000644000004100000410000000205614052714044016720 0ustar www-datawww-dataCopyright (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.