singleton_attr 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -0
- data/lib/singleton_attr.rb +110 -0
- data/spec/singleton_attr_spec.rb +57 -0
- metadata +50 -0
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Singleton Attributes #
|
2
|
+
|
3
|
+
http://rubygems.org/gems/singleton_attr
|
4
|
+
|
5
|
+
# Summary #
|
6
|
+
|
7
|
+
Adds methods to declare attribute accessor/reader/writer on singleton instance.
|
8
|
+
|
9
|
+
# Description #
|
10
|
+
|
11
|
+
Provides :singleton_attr_accessor, :singleton_attr_reader, :singleton_attr_writer, :module_attr_accessor, :module_attr_reader, :module_attr_writer, :class_attr_accessor, :class_attr_reader, :class_attr_writer.
|
12
|
+
|
13
|
+
# Install #
|
14
|
+
|
15
|
+
* sudo gem install singleton_attr
|
16
|
+
|
17
|
+
# Usage #
|
18
|
+
|
19
|
+
Just like :attr_accessor, :attr_reader, :attr_writer but instead of defining an instance method it defines a class/module method.
|
20
|
+
|
21
|
+
# License #
|
22
|
+
|
23
|
+
(The MIT License)
|
24
|
+
|
25
|
+
Copyright (c) 2013 Ridiculous Power, Asher
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
'Software'), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Adds methods to declare attribute accessors/readers/writers on singletons.
|
4
|
+
#
|
5
|
+
module ::SingletonAttr
|
6
|
+
|
7
|
+
#############################
|
8
|
+
# singleton_attr_accessor #
|
9
|
+
#############################
|
10
|
+
|
11
|
+
###
|
12
|
+
# Define an attribute accessor on the singleton of instance for which method is called.
|
13
|
+
#
|
14
|
+
# @overload singleton_attr_accessor( name, ... )
|
15
|
+
#
|
16
|
+
# @param [Symbol,String] name
|
17
|
+
#
|
18
|
+
# Name for reader method.
|
19
|
+
#
|
20
|
+
# @return [Module,Class] Self.
|
21
|
+
#
|
22
|
+
def singleton_attr_accessor( *names )
|
23
|
+
|
24
|
+
singleton_class.instance_eval { attr_accessor *names }
|
25
|
+
|
26
|
+
return self
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
###########################
|
31
|
+
# singleton_attr_reader #
|
32
|
+
###########################
|
33
|
+
|
34
|
+
###
|
35
|
+
# Define an attribute reader on the singleton of instance for which method is called.
|
36
|
+
#
|
37
|
+
# @overload singleton_attr_reader( name, ... )
|
38
|
+
#
|
39
|
+
# @param [Symbol,String] name
|
40
|
+
#
|
41
|
+
# Name for reader method.
|
42
|
+
#
|
43
|
+
# @return [Module,Class] Self.
|
44
|
+
#
|
45
|
+
def singleton_attr_reader( *names )
|
46
|
+
|
47
|
+
singleton_class.instance_eval { attr_reader *names }
|
48
|
+
|
49
|
+
return self
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
###########################
|
54
|
+
# singleton_attr_writer #
|
55
|
+
###########################
|
56
|
+
|
57
|
+
###
|
58
|
+
# Define an attribute writer on the singleton of instance for which method is called.
|
59
|
+
#
|
60
|
+
# @overload singleton_attr_writer( name, ... )
|
61
|
+
#
|
62
|
+
# @param [Symbol,String] name
|
63
|
+
#
|
64
|
+
# Name for reader method.
|
65
|
+
#
|
66
|
+
# @return [Module,Class] Self.
|
67
|
+
#
|
68
|
+
def singleton_attr_writer( *names )
|
69
|
+
|
70
|
+
singleton_class.instance_eval { attr_writer *names }
|
71
|
+
|
72
|
+
return self
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
###
|
79
|
+
# Add aliases to substitute module prefix for singleton prefix.
|
80
|
+
#
|
81
|
+
module ::SingletonAttr::Module
|
82
|
+
|
83
|
+
include ::SingletonAttr
|
84
|
+
|
85
|
+
alias_method :module_attr_accessor, :singleton_attr_accessor
|
86
|
+
alias_method :module_attr_reader, :singleton_attr_reader
|
87
|
+
alias_method :module_attr_writer, :singleton_attr_writer
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
###
|
92
|
+
# Add aliases to substitute class prefix for singleton prefix.
|
93
|
+
#
|
94
|
+
module ::SingletonAttr::Class
|
95
|
+
|
96
|
+
include ::SingletonAttr
|
97
|
+
|
98
|
+
alias_method :class_attr_accessor, :singleton_attr_accessor
|
99
|
+
alias_method :class_attr_reader, :singleton_attr_reader
|
100
|
+
alias_method :class_attr_writer, :singleton_attr_writer
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
class ::Module
|
105
|
+
include ::SingletonAttr::Module
|
106
|
+
end
|
107
|
+
|
108
|
+
class ::Class
|
109
|
+
include ::SingletonAttr::Class
|
110
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
require_relative '../lib/singleton_attr.rb'
|
3
|
+
|
4
|
+
describe ::SingletonAttr do
|
5
|
+
|
6
|
+
shared_examples_for :singleton do
|
7
|
+
it '#singleton_attr_accessor declares attr_accessor on singleton' do
|
8
|
+
instance.singleton_attr_accessor :accessor
|
9
|
+
instance.accessor.should be nil
|
10
|
+
instance.accessor = :value
|
11
|
+
instance.accessor.should be :value
|
12
|
+
end
|
13
|
+
it '#singleton_attr_reader declares attr_reader on singleton' do
|
14
|
+
instance.singleton_attr_reader :reader
|
15
|
+
instance.reader.should be nil
|
16
|
+
instance.instance_variable_set( :@reader, :value )
|
17
|
+
instance.reader.should be :value
|
18
|
+
instance.respond_to?( :reader= ).should be false
|
19
|
+
end
|
20
|
+
it '#singleton_attr_writer declares attr_writer on singleton' do
|
21
|
+
instance.singleton_attr_writer :writer
|
22
|
+
instance.instance_variable_get( :@writer ).should be nil
|
23
|
+
instance.writer = :value
|
24
|
+
instance.instance_variable_get( :@writer ).should be :value
|
25
|
+
instance.respond_to?( :writer ).should be false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'Module' do
|
30
|
+
let( :instance ) { ::Module.new }
|
31
|
+
it_behaves_like :singleton
|
32
|
+
it 'aliases #singleton_attr_accessor to #module_attr_accessor' do
|
33
|
+
instance.method( :module_attr_accessor ).should == instance.method( :singleton_attr_accessor )
|
34
|
+
end
|
35
|
+
it 'aliases #singleton_attr_reader to #module_attr_reader' do
|
36
|
+
instance.method( :module_attr_reader ).should == instance.method( :singleton_attr_reader )
|
37
|
+
end
|
38
|
+
it 'aliases #singleton_attr_writer to #module_attr_writer' do
|
39
|
+
instance.method( :module_attr_writer ).should == instance.method( :singleton_attr_writer )
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'Class' do
|
44
|
+
let( :instance ) { ::Class.new }
|
45
|
+
it_behaves_like :singleton
|
46
|
+
it 'aliases #singleton_attr_accessor to #class_attr_accessor' do
|
47
|
+
instance.method( :module_attr_accessor ).should == instance.method( :singleton_attr_accessor )
|
48
|
+
end
|
49
|
+
it 'aliases #singleton_attr_reader to #class_attr_reader' do
|
50
|
+
instance.method( :class_attr_reader ).should == instance.method( :singleton_attr_reader )
|
51
|
+
end
|
52
|
+
it 'aliases #singleton_attr_writer to #class_attr_writer' do
|
53
|
+
instance.method( :class_attr_writer ).should == instance.method( :singleton_attr_writer )
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: singleton_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Asher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-29 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Provides :singleton_attr_accessor, :singleton_attr_reader, :singleton_attr_writer,
|
15
|
+
:module_attr_accessor, :module_attr_reader, :module_attr_writer, :class_attr_accessor,
|
16
|
+
:class_attr_reader, :class_attr_writer.
|
17
|
+
email: asher@ridiculouspower.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/singleton_attr.rb
|
23
|
+
- spec/singleton_attr_spec.rb
|
24
|
+
- README.md
|
25
|
+
homepage: http://rubygems.org/gems/singleton_attr
|
26
|
+
licenses: []
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 1.9.1
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project: singleton_attr
|
45
|
+
rubygems_version: 1.8.24
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Adds methods to declare attribute accessor/reader/writer on singleton instance.
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|