simple-object-container 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple-object-container.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Shinpei Maruyama
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,53 @@
1
+ # SimpleObjectContainer
2
+
3
+ SimpleObjectContainer is a gem like Object::Container Perl module(http://search.cpan.org/~typester/Object-Container-0.14/lib/Object/Container.pm)
4
+
5
+ ## Installation
6
+
7
+ $ gem install simple-object-container
8
+
9
+ ## Usage
10
+
11
+ # register Class by class name
12
+ SimpleObjectCotainer.register(MyClass)
13
+ # then, you can get a instance
14
+ SimpleObjectContainer.get(MyClass) # => the instance of MyClass
15
+ # and always return *the same* instance
16
+ SimpleObjectContainer.get(MyClass).equal_to? SimpleObjectContaner.get(MyClass) # => true
17
+
18
+ # you can register Class with a symbol as a key
19
+ SimpleObjectContainer.register(:key, AnotherClass)
20
+ # and you can get a instance using the key
21
+ SimpleObjectContainer.get(:key) # => the instance of AnotherClass
22
+ # and always return *the same* instance
23
+ SimpleObjectContainer.get(:key).equal_to? SimpleObjectContaner.get(:key) # => true
24
+
25
+ # you can register a lambda which return a object you want
26
+ SimpleObjectContainer.register(:key, lambda{ SomeClass.new(parameters) })
27
+ # and you can get a instance which is returned by the lambda
28
+ SimpleObjectContainer.get(:key) # => the instance of SomeClass initialized with parameters
29
+ # and always return *the same* instance
30
+ SimpleObjectContainer.get(:key).equal_to? SimpleObjectContaner.get(:key) # => true
31
+
32
+ SimpleObjectContainer has singleton interface like avobe and object interface like bellow.
33
+
34
+ container = SimpleObjectCotainer.new
35
+ container.register(MyClass)
36
+
37
+ # you can use object interface by same way to singleton interface
38
+ container.get(MyClass) # => the instance of MyClass
39
+ # and always return *the same* instance
40
+ container.get(MyClass).equal_to? container.get(MyClass) # => true
41
+
42
+ another_container = SimpleObjectCotainer.new
43
+ another_container.register(MyClass)
44
+ container.get(MyClass).equal_to? another_container.get(MyClass) # => false
45
+
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,64 @@
1
+ require "simple-object-container/version"
2
+ class SimpleObjectContainer
3
+ module Containable
4
+ def register(key, rest = nil)
5
+ if key.class != Class && key.class !=Symbol
6
+ raise ArgumentError,"key should be Class or Symbol"
7
+ end
8
+
9
+ if key.class == Class
10
+ register_class_by_class(key)
11
+ elsif key.class == Symbol && rest.class == Class
12
+ register_class_by_symbol(key,rest)
13
+ elsif key.class == Symbol && rest.class == Proc
14
+ register_lambda_by_symbol(key,rest)
15
+ else
16
+ raise "assert"; #never reach here ;)
17
+ end
18
+ end
19
+ def get(klass)
20
+ get_instance(klass)
21
+ end
22
+
23
+ private
24
+ def instance_table
25
+ @instance_table ||= {}
26
+ end
27
+ def loader_table
28
+ @loader_table ||= {}
29
+ end
30
+
31
+ def register_class_by_class(klass)
32
+ register_loader(klass, ->(){klass.new})
33
+ end
34
+ def register_class_by_symbol(key,klass)
35
+ register_loader(key, ->(){klass.new})
36
+ end
37
+ def register_lambda_by_symbol(key,lambda)
38
+ register_loader(key, lambda)
39
+ end
40
+ def register_loader(key,loader)
41
+ loader_table[key] = loader
42
+ end
43
+
44
+ def get_instance(key)
45
+ return instance_table[key] if instance_table[key]
46
+
47
+ loader = loader_table[key]
48
+ raise KeyIsNotRegisterd if loader.nil?
49
+
50
+ instance_table[key] ||= loader.call
51
+ instance_table[key]
52
+ end
53
+ end
54
+
55
+ class << self
56
+ include Containable
57
+ end
58
+ include Containable
59
+ end
60
+
61
+ class SimpleObjectContainer
62
+ class Error < StandardError;end
63
+ class KeyIsNotRegisterd < Error;end
64
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleObjectContainer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple-object-container/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple-object-container"
8
+ gem.version = SimpleObjectContainer::VERSION
9
+ gem.authors = ["Shinpei Maruyama"]
10
+ gem.email = ["shinpeim@gmail.com"]
11
+ gem.description = %q{simple object container for ruby}
12
+ gem.summary = %q{SimpleObjectContainer is a gem like Object::Container Perl module(http://search.cpan.org/~typester/Object-Container-0.14/lib/Object/Container.pm)
13
+ }
14
+ gem.homepage = "https://github.com/Shinpeim/SimpleObjectContainer"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rspec", "~> 2.12.0"
22
+ end
@@ -0,0 +1,112 @@
1
+ require "spec_helper"
2
+ require "simple-object-container"
3
+
4
+ class SimpleObjectContainer
5
+ module Containable
6
+ def clear
7
+ @instance_table = {}
8
+ @loader_table = {}
9
+ end
10
+ end
11
+ end
12
+ shared_examples_for "SimpleObjectContainer behavior" do
13
+ context "when SomeClass is registered" do
14
+ before do
15
+ subject.register(SomeClass)
16
+ end
17
+ after do
18
+ subject.clear
19
+ end
20
+ describe "#get" do
21
+ it "load object lazy" do
22
+ subject.send(:instance_table)[SomeClass].should be_nil
23
+ subject.get(SomeClass)
24
+ subject.send(:instance_table)[SomeClass].should be_instance_of SomeClass
25
+ end
26
+ context "when given SomeClass with a class name" do
27
+ let(:returned_value) {subject.get(SomeClass)}
28
+ it "return a instance of SomeClass" do
29
+ returned_value.should be_instance_of SomeClass
30
+ end
31
+ it "always return same object" do
32
+ returned_value.should be_equal subject.get(SomeClass)
33
+ end
34
+ end
35
+ context "when Given OtherClass" do
36
+ it "should raise error if given no registerd class" do
37
+ ->(){subject.get(OtherClass)}.should raise_error SimpleObjectContainer::KeyIsNotRegisterd
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ context "when SomeClass is registered with a simbol(:key)" do
44
+ before do
45
+ subject.register(:key, SomeClass)
46
+ end
47
+ after do
48
+ subject.clear
49
+ end
50
+ describe "#get" do
51
+ it "load object lazy" do
52
+ subject.send(:instance_table)[:key].should be_nil
53
+ subject.get(:key)
54
+ subject.send(:instance_table)[:key].should be_instance_of SomeClass
55
+ end
56
+ context "when given :key" do
57
+ let (:returned_value) {subject.get(:key)}
58
+ it "return a instance of SomeClass" do
59
+ returned_value.should be_instance_of SomeClass
60
+ end
61
+ it "always return same object" do
62
+ returned_value.should be_equal subject.get(:key)
63
+ end
64
+ end
65
+ context "when Given OtherClass" do
66
+ it "should raise error if given no registerd class" do
67
+ ->(){subject.get(:other_key)}.should raise_error SimpleObjectContainer::KeyIsNotRegisterd
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ context "when lambda(that return a instance of SomeClass) is registered with a simbol(:key)" do
74
+ before do
75
+ subject.register(:key, ->(){SomeClass.new})
76
+ end
77
+ after do
78
+ subject.clear
79
+ end
80
+ describe "#get" do
81
+ it "load object lazy" do
82
+ subject.send(:instance_table)[:key].should be_nil
83
+ subject.get(:key)
84
+ subject.send(:instance_table)[:key].should be_instance_of SomeClass
85
+ end
86
+ context "when given :key" do
87
+ let(:returned_value) {subject.get(:key)}
88
+ it "return a instance of SomeClass" do
89
+ returned_value.should be_instance_of SomeClass
90
+ end
91
+ it "always return same object" do
92
+ returned_value.should be_equal subject.get(:key)
93
+ end
94
+ end
95
+ context "when Given OtherClass" do
96
+ it "should raise error if given no registerd class" do
97
+ ->(){subject.get(:other_key)}.should raise_error SimpleObjectContainer::KeyIsNotRegisterd
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "Singleton Interface" do
105
+ subject {SimpleObjectContainer}
106
+ it_should_behave_like "SimpleObjectContainer behavior"
107
+ end
108
+
109
+ describe "Object Interface" do
110
+ subject {SimpleObjectContainer.new}
111
+ it_should_behave_like "SimpleObjectContainer behavior"
112
+ end
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'simple-object-container'
5
+
6
+ class SomeClass;end
7
+ class OtherClass;end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-object-container
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shinpei Maruyama
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ description: simple object container for ruby
31
+ email:
32
+ - shinpeim@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/simple-object-container.rb
43
+ - lib/simple-object-container/version.rb
44
+ - simple-object-container.gemspec
45
+ - spec/simple-object-container_spec.rb
46
+ - spec/spec_helper.rb
47
+ homepage: https://github.com/Shinpeim/SimpleObjectContainer
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.24
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: SimpleObjectContainer is a gem like Object::Container Perl module(http://search.cpan.org/~typester/Object-Container-0.14/lib/Object/Container.pm)
71
+ test_files:
72
+ - spec/simple-object-container_spec.rb
73
+ - spec/spec_helper.rb