tiny_singleton 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76e868a8bdf64ca3f34f698d482bc055a2687d89
4
+ data.tar.gz: 0b4c82ac841bb1062bd8181609b8606336e721e4
5
+ SHA512:
6
+ metadata.gz: e328944480940c4278201473ef7a23a09c91ef2704e3892752d670fa37d6c1eed0402327e850df2981b570d9ffd1b5b7f8030b98a70c7a04240746cd1155cc92
7
+ data.tar.gz: 18aed245d724dfa9f6ec04a4dda3dcb92a1d8d8c7631cf50df65c711148b605b5981912d459313126b4028d59f26ea17dfe7ea9e1974a9135fd70bda040985e0
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
@@ -0,0 +1,2 @@
1
+ doc/
2
+ pkg/
@@ -0,0 +1 @@
1
+ --markup markdown -M kramdown --title "tiny_singleton Documentation" --protected
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2015-02-23
2
+
3
+ * Initial release:
4
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Damien Robert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # tiny_singleton
2
+
3
+ * [Homepage](https://github.com/DamienRobert/tiny_singleton#readme)
4
+ * [Gems]("https://rubygems.org/gems/tiny_singleton)
5
+ * [Issues](https://github.com/DamienRobert/tiny_singleton/issues)
6
+ * [Documentation](http://rubydoc.info/gems/tiny_singleton/frames)
7
+ * [Email](mailto:Damien.Olivier.Robert+gems at gmail.com)
8
+
9
+ ## Description
10
+
11
+ Tiny Singleton is a very simple implementation of the Singleton Design
12
+ Pattern. Compared to the Singleton module in the stdlib, one does not
13
+ need to class 'klass.instance' to get access to the singleton object, one
14
+ can call 'klass.new' as usual. It is thus easier to add the Singleton
15
+ Pattern to an existing class without changing the way the code calls it.
16
+
17
+ ## Features
18
+
19
+ ## Examples
20
+
21
+ ``` ruby
22
+ require 'tiny_singleton'
23
+
24
+ class Foo
25
+ prepend TinySingleton
26
+ def initialize(foo)
27
+ @foo=foo
28
+ end
29
+ end
30
+
31
+ foo=Foo.new("foo")
32
+ bar=Foo.new("bar")
33
+ foo.object_id == bar.object_id #=> true
34
+ ```
35
+
36
+ ## Requirements
37
+
38
+ It needs at least `ruby 2.0` since it relies on `Module#prepend`
39
+
40
+ ## Install
41
+
42
+ $ gem install tiny_singleton
43
+
44
+ ## Copyright
45
+
46
+ Copyright (c) 2015 Damien Robert
47
+
48
+ MIT License. See {file:LICENSE.txt} for details.
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ gem 'rubygems-tasks', '~> 0.2'
8
+ require 'rubygems/tasks'
9
+
10
+ Gem::Tasks.new(sign: {checksum: true, pgp: true},
11
+ build: {tar: true}, scm: {status: true}) do |tasks|
12
+ tasks.console.command = 'pry'
13
+ end
14
+ rescue LoadError => e
15
+ warn e.message
16
+ warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
17
+ end
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new do |test|
22
+ test.libs << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ gem 'yard', '~> 0.8'
29
+ require 'yard'
30
+
31
+ YARD::Rake::YardocTask.new
32
+ rescue LoadError => e
33
+ task :yard do
34
+ abort "Please run `gem install yard` to install YARD."
35
+ end
36
+ end
37
+ task :doc => :yard
38
+
@@ -0,0 +1,17 @@
1
+ name: tiny_singleton
2
+ summary: "Singleton pattern using Module#prepend"
3
+ description: |
4
+ Tiny Singleton is a very simple implementation of the Singleton Design
5
+ Pattern. Compared to the Singleton module in the stdlib, one does not
6
+ need to class 'klass.instance' to get access to the singleton object, one
7
+ can call 'klass.new' as usual. It is thus easier to add the Singleton
8
+ Pattern to an existing class without changing the way the code calls it.
9
+ license: MIT
10
+ authors: Damien Robert
11
+ email: Damien.Olivier.Robert+gems@gmail.com
12
+ homepage: https://github.com/DamienRobert/tiny_singleton#readme
13
+
14
+ development_dependencies:
15
+ minitest: ~> 5.0
16
+ rubygems-tasks: ~> 0.2
17
+ yard: ~> 0.8
@@ -0,0 +1,27 @@
1
+ require 'tiny_singleton/version'
2
+
3
+ #prepend TinySingleton in a class to make it a Singleton Class
4
+ module TinySingleton
5
+ def clone
6
+ return self
7
+ end
8
+ alias dup clone
9
+
10
+ def self.prepended(klass)
11
+ klass.singleton_class.send :prepend, ClassMethods
12
+ end
13
+ def self.included(klass)
14
+ klass.singleton_class.send :prepend, ClassMethods
15
+ end
16
+
17
+ module ClassMethods
18
+ def new(*args,&b)
19
+ singl=:@_singleton_
20
+ if instance_variable_defined?(singl)
21
+ return instance_variable_get(singl)
22
+ else
23
+ instance_variable_set(singl,super)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module TinySingleton
2
+ # tiny_singleton version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+ require 'tiny_singleton'
3
+
4
+ class TestTinySingleton < Minitest::Test
5
+ def setup
6
+ @fooClass=Class.new do
7
+ prepend TinySingleton
8
+ def initialize(foo)
9
+ @foo=foo
10
+ end
11
+ end
12
+ end
13
+
14
+ def test_version
15
+ version = TinySingleton.const_get('VERSION')
16
+
17
+ assert(!version.empty?, 'should have a VERSION constant')
18
+ end
19
+
20
+ def test_singleton_pattern
21
+ foo=@fooClass.new("foo")
22
+ bar=@fooClass.new("bar")
23
+ assert_equal foo.object_id, bar.object_id, 'Objects created by new should be identical'
24
+ end
25
+
26
+ def test_clone
27
+ foo=@fooClass.new("foo")
28
+ bar=foo.clone
29
+ assert_equal foo.object_id, bar.object_id
30
+ end
31
+
32
+ def test_dup
33
+ foo=@fooClass.new("foo")
34
+ bar=foo.clone
35
+ assert_equal foo.object_id, bar.object_id
36
+ end
37
+
38
+ def test_subclass
39
+ barClass=Class.new(@fooClass)
40
+ foo=@fooClass.new("foo")
41
+ bar=@fooClass.new("bar")
42
+ assert_equal foo.object_id, bar.object_id
43
+ end
44
+
45
+ def test_include
46
+ fooClass=Class.new do
47
+ include TinySingleton
48
+ end
49
+ foo=fooClass.new
50
+ bar=fooClass.new
51
+ assert_equal foo.object_id, bar.object_id
52
+ end
53
+
54
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gemspec = YAML.load_file('gemspec.yml')
7
+
8
+ gem.name = gemspec.fetch('name')
9
+ gem.version = gemspec.fetch('version') do
10
+ lib_dir = File.join(File.dirname(__FILE__),'lib')
11
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
12
+
13
+ require 'tiny_singleton/version'
14
+ TinySingleton::VERSION
15
+ end
16
+
17
+ gem.summary = gemspec['summary']
18
+ gem.description = gemspec['description']
19
+ gem.licenses = Array(gemspec['license'])
20
+ gem.authors = Array(gemspec['authors'])
21
+ gem.email = gemspec['email']
22
+ gem.homepage = gemspec['homepage']
23
+
24
+ glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
+
26
+ gem.files = `git ls-files`.split($/)
27
+ gem.files = glob[gemspec['files']] if gemspec['files']
28
+
29
+ gem.executables = gemspec.fetch('executables') do
30
+ glob['bin/*'].map { |path| File.basename(path) }
31
+ end
32
+ gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.'
33
+
34
+ gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb']
35
+ gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}']
36
+
37
+ gem.require_paths = Array(gemspec.fetch('require_paths') {
38
+ %w[ext lib].select { |dir| File.directory?(dir) }
39
+ })
40
+
41
+ gem.requirements = gemspec['requirements']
42
+ gem.required_ruby_version = gemspec['required_ruby_version']
43
+ gem.required_rubygems_version = gemspec['required_rubygems_version']
44
+ gem.post_install_message = gemspec['post_install_message']
45
+
46
+ split = lambda { |string| string.split(/,\s*/) }
47
+
48
+ if gemspec['dependencies']
49
+ gemspec['dependencies'].each do |name,versions|
50
+ gem.add_dependency(name,split[versions])
51
+ end
52
+ end
53
+
54
+ if gemspec['development_dependencies']
55
+ gemspec['development_dependencies'].each do |name,versions|
56
+ gem.add_development_dependency(name,split[versions])
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_singleton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damien Robert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubygems-tasks
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ description: |
56
+ Tiny Singleton is a very simple implementation of the Singleton Design
57
+ Pattern. Compared to the Singleton module in the stdlib, one does not
58
+ need to class 'klass.instance' to get access to the singleton object, one
59
+ can call 'klass.new' as usual. It is thus easier to add the Singleton
60
+ Pattern to an existing class without changing the way the code calls it.
61
+ email: Damien.Olivier.Robert+gems@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files:
65
+ - ChangeLog.md
66
+ - LICENSE.txt
67
+ - README.md
68
+ files:
69
+ - ".document"
70
+ - ".gitignore"
71
+ - ".yardopts"
72
+ - ChangeLog.md
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - gemspec.yml
77
+ - lib/tiny_singleton.rb
78
+ - lib/tiny_singleton/version.rb
79
+ - test/helper.rb
80
+ - test/test_tiny_singleton.rb
81
+ - tiny_singleton.gemspec
82
+ homepage: https://github.com/DamienRobert/tiny_singleton#readme
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Singleton pattern using Module#prepend
106
+ test_files: []