scoped_attr_accessor 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +48 -27
- data/lib/scoped_attr_accessor/version.rb +1 -1
- data/lib/scoped_attr_accessor.rb +6 -6
- data/scoped_attr_accessor.gemspec +1 -1
- data/test/scoped_attr_accessor_test_module.rb +28 -15
- metadata +11 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9fd58a73e8641fcfbd9205039fad4c77fb1920a2
|
4
|
+
data.tar.gz: 788bb68336faabada51d7454d56769bea5924830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4edfae715f4bbb70688ba3326e4928f174e2174d646834f9772d4aeb75c453c092accce186762805148e88a3a63fa4ee57a4a89d5a9524da701615f8d98af08a
|
7
|
+
data.tar.gz: f92e78883d16dc0d5220ef4aeab45fd1fd956c79de9bfb963390ca2bf58e389a6287992aa190c25dcd15e65edf7f45c2b05862f53f9db194b0fade829bec1a0e
|
data/README.md
CHANGED
@@ -59,7 +59,9 @@ classes able to have protected and private accessors.
|
|
59
59
|
|
60
60
|
The '/include' form of this gem is a ruby-wide monkeypatch. Please
|
61
61
|
remember that it's perfectly fine to use it this way in your own
|
62
|
-
applications, but quite rude to use it this way in a library.
|
62
|
+
applications, but quite rude to use it this way in a library. People
|
63
|
+
who use your library would then be infected by this monkeypatch as
|
64
|
+
well. Not cool.
|
63
65
|
|
64
66
|
If you use scoped accessors in a gem or library of your own, please
|
65
67
|
consider using the non-include version, or, in Ruby 2.0, use
|
@@ -85,12 +87,12 @@ general is somewhat divided on the meaning of the concept of privacy
|
|
85
87
|
in ruby. For some, especially those who come from Java, C++, VB, or
|
86
88
|
some other language that enforces encapsulation with privacy, ruby's
|
87
89
|
ability to get around privacy means that encapsulation cannot be
|
88
|
-
enforced. Most of us (I
|
89
|
-
use of privacy in ruby altogether, as if it were
|
90
|
-
half-hearted nod to other languages' encapsulation
|
91
|
-
everything public, since everything's
|
92
|
-
|
93
|
-
security but does add hassle and headache.
|
90
|
+
enforced. Most of us (I have counted myself in this camp until now)
|
91
|
+
have discarded the use of privacy in ruby altogether, as if it were
|
92
|
+
nothing more than a half-hearted nod to other languages' encapsulation
|
93
|
+
practices. Make everything public, we say; since everything's
|
94
|
+
ultimately public anyway, using the private keyword is merely a
|
95
|
+
speedbump which adds no security but does add hassle and headache.
|
94
96
|
|
95
97
|
I am becoming more and more aware of a number of rubyists who consider
|
96
98
|
the use of private and protected methods in ruby to be a useful way to
|
@@ -105,23 +107,42 @@ maintainers of the code, meaning that a maintainer will understand the
|
|
105
107
|
intent of the privacy, and be less likely to *accidentally* create an
|
106
108
|
external dependency on on a method that should have been kept private.
|
107
109
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
110
|
+
Another way of looking at this difference of opinion is that the old
|
111
|
+
way of thinking is that `private` is there to protect my code from
|
112
|
+
your prying eyes, which implies that I think I am smarter than every
|
113
|
+
programmer ever, even all the ones who come from the future. It also
|
114
|
+
means, at least in ruby, that I think hiding stuff inside a wet paper
|
115
|
+
bag is a pretty good way of securing such code from you future
|
116
|
+
programmers. This new way of thinking admits to the wet paper bag, but
|
117
|
+
more importantly it reverses the direction of the protection: it is
|
118
|
+
only a speedbump, but it is there to protect YOU, a smart future
|
119
|
+
programmer, from code that I, a programmer from the past who knows he
|
120
|
+
will never be as dumb as he is right now, wrote and got working but
|
121
|
+
it's volatile and dangerous to depend upon. You may figure out a way
|
122
|
+
to use that code safely, and if you do, then by all means cut through
|
123
|
+
that paper bag.
|
124
|
+
|
125
|
+
I have become swayed by this second way of thinking, but I find that
|
126
|
+
it falls short in one key area: accessors. It is easy to create a
|
127
|
+
private method in ruby, but creating a private accessor method is
|
128
|
+
actually a bit tortuous. As a result, I see programmers who strongly
|
129
|
+
believe in using privacy to communicate undependable interfaces
|
130
|
+
frequently making one of two bad tradeoffs: they either use instance
|
131
|
+
variables or public accessors for their private variables. These
|
132
|
+
variables are not dependable, at least from a "stable interface"
|
133
|
+
standpoint, so both solutions are suboptimal. While instance variables
|
134
|
+
communicate privacy, they force the class to depend on its own
|
135
|
+
undependable internals. Making an accessor allows the class to create
|
136
|
+
an internal interface to isolates itself from its own undependable
|
137
|
+
internals, but by making it public the communication to the maintainer
|
138
|
+
is that other classes can and should depend on that interface.
|
139
|
+
|
140
|
+
What is needed is a clean, easy, *elegant* way to create private and
|
141
|
+
protected accessors, so that classes can create internal interfaces
|
142
|
+
to their instance variables, reducing their exposure to their own
|
143
|
+
volatility WITHOUT communicating that interface publically.
|
144
|
+
|
145
|
+
We need a clean and easy way to create private and protected accessors
|
146
|
+
in ruby. My goal with this gem, then, is to create such a way, so that
|
147
|
+
those who wish to communicate "this variable is undependable and it
|
148
|
+
should be kept isolated for now" can do so easily.
|
data/lib/scoped_attr_accessor.rb
CHANGED
@@ -25,22 +25,22 @@ module ScopedAttrAccessor
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def private_attr_accessor(*names)
|
28
|
-
|
29
|
-
names
|
28
|
+
private_attr_reader(*names)
|
29
|
+
private_attr_writer(*names)
|
30
30
|
end
|
31
31
|
|
32
32
|
def protected_attr_reader(*names)
|
33
|
-
protected
|
34
33
|
attr_reader(*names)
|
34
|
+
names.each {|name| protected name}
|
35
35
|
end
|
36
36
|
|
37
37
|
def protected_attr_writer(*names)
|
38
|
-
protected
|
39
38
|
attr_writer(*names)
|
39
|
+
names.each {|name| protected "#{name}=" }
|
40
40
|
end
|
41
41
|
|
42
42
|
def protected_attr_accessor(*names)
|
43
|
-
|
44
|
-
|
43
|
+
protected_attr_reader(*names)
|
44
|
+
protected_attr_writer(*names)
|
45
45
|
end
|
46
46
|
end
|
@@ -20,6 +20,6 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "debugger", "~> 1.5"
|
23
|
-
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
24
|
spec.add_development_dependency "minitest", "~> 4.0"
|
25
25
|
end
|
@@ -120,19 +120,30 @@ module ScopedAttrAccessorTests
|
|
120
120
|
# Happy path test 1. If priv_read1-3 are private, they should be
|
121
121
|
# inaccessible
|
122
122
|
def test_private_readers_are_in_fact_private
|
123
|
-
|
124
|
-
|
125
|
-
|
123
|
+
-> { @foo.priv_read1 }.must_raise NoMethodError
|
124
|
+
-> { @foo.priv_read2 }.must_raise NoMethodError
|
125
|
+
-> { @foo.priv_read3 }.must_raise NoMethodError
|
126
|
+
-> { @bar.priv_read1 }.must_raise NoMethodError
|
127
|
+
-> { @bar.priv_read2 }.must_raise NoMethodError
|
128
|
+
-> { @bar.priv_read3 }.must_raise NoMethodError
|
126
129
|
end
|
127
130
|
|
128
131
|
def test_private_writers_are_in_fact_private
|
129
|
-
|
130
|
-
|
132
|
+
-> { @foo.priv_write1 = "OH NOES" }.must_raise NoMethodError
|
133
|
+
-> { @foo.priv_write2 = "OH NOES" }.must_raise NoMethodError
|
134
|
+
-> { @bar.priv_write1 = "OH NOES" }.must_raise NoMethodError
|
135
|
+
-> { @bar.priv_write2 = "OH NOES" }.must_raise NoMethodError
|
131
136
|
end
|
132
137
|
|
133
138
|
def test_private_accessors_are_in_fact_private
|
134
|
-
|
135
|
-
|
139
|
+
-> { @foo.priv_access1 = "OH NOES" }.must_raise NoMethodError
|
140
|
+
-> { @foo.priv_access2 = "OH NOES" }.must_raise NoMethodError
|
141
|
+
-> { @foo.priv_access1 }.must_raise NoMethodError
|
142
|
+
-> { @foo.priv_access2 }.must_raise NoMethodError
|
143
|
+
-> { @bar.priv_access1 = "OH NOES" }.must_raise NoMethodError
|
144
|
+
-> { @bar.priv_access2 = "OH NOES" }.must_raise NoMethodError
|
145
|
+
-> { @bar.priv_access1 }.must_raise NoMethodError
|
146
|
+
-> { @bar.priv_access2 }.must_raise NoMethodError
|
136
147
|
end
|
137
148
|
|
138
149
|
# Happy path test 2. priv_read1 and priv_read2 may be inaccessible, but are they
|
@@ -160,9 +171,9 @@ module ScopedAttrAccessorTests
|
|
160
171
|
# Happy path test 3. If prot_read1-3 are protected, they should be
|
161
172
|
# inaccessible
|
162
173
|
def test_protected_readers_are_in_fact_private
|
163
|
-
|
164
|
-
|
165
|
-
|
174
|
+
-> { @foo.prot_read1 }.must_raise NoMethodError
|
175
|
+
-> { @foo.prot_read2 }.must_raise NoMethodError
|
176
|
+
-> { @foo.prot_read3 }.must_raise NoMethodError
|
166
177
|
end
|
167
178
|
|
168
179
|
# Happy path test 4. prot_read1 and prot_read2 may be inaccessible, but are they
|
@@ -188,17 +199,19 @@ module ScopedAttrAccessorTests
|
|
188
199
|
# private scope, after we created protected reader 3, did
|
189
200
|
# attr_accessor priv_read3 still get created privately?
|
190
201
|
def test_private_readers_AFTER_protected_accessors_are_STILL_private
|
191
|
-
|
202
|
+
-> { @foo.peek_at_priv_read3_via_protected_access_BAD }.must_raise NoMethodError
|
192
203
|
end
|
193
204
|
|
194
205
|
def test_protected_writers_are_in_fact_protected
|
195
|
-
|
196
|
-
|
206
|
+
-> { @bar.prot_write1 = "OH NOES" }.must_raise NoMethodError
|
207
|
+
-> { @bar.prot_write2 = "OH NOES" }.must_raise NoMethodError
|
197
208
|
end
|
198
209
|
|
199
210
|
def test_protected_accessors_are_in_fact_protected
|
200
|
-
|
201
|
-
|
211
|
+
-> { @bar.prot_access1 }.must_raise NoMethodError
|
212
|
+
-> { @bar.prot_access2 }.must_raise NoMethodError
|
213
|
+
-> { @bar.prot_access1 = "OH NOES" }.must_raise NoMethodError
|
214
|
+
-> { @bar.prot_access2 = "OH NOES" }.must_raise NoMethodError
|
202
215
|
end
|
203
216
|
|
204
217
|
def test_protected_writers_do_in_fact_work_as_writers
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scoped_attr_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- David Brady
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: debugger
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,23 +41,20 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
47
|
+
version: '10.0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
54
|
+
version: '10.0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: minitest
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ~>
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ~>
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -99,33 +90,26 @@ files:
|
|
99
90
|
homepage: https://github.com/dbrady/scoped_attr_accessor.git
|
100
91
|
licenses:
|
101
92
|
- MIT
|
93
|
+
metadata: {}
|
102
94
|
post_install_message:
|
103
95
|
rdoc_options: []
|
104
96
|
require_paths:
|
105
97
|
- lib
|
106
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
99
|
requirements:
|
109
|
-
- -
|
100
|
+
- - '>='
|
110
101
|
- !ruby/object:Gem::Version
|
111
102
|
version: '0'
|
112
|
-
segments:
|
113
|
-
- 0
|
114
|
-
hash: -2597970377507506024
|
115
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
-
none: false
|
117
104
|
requirements:
|
118
|
-
- -
|
105
|
+
- - '>='
|
119
106
|
- !ruby/object:Gem::Version
|
120
107
|
version: '0'
|
121
|
-
segments:
|
122
|
-
- 0
|
123
|
-
hash: -2597970377507506024
|
124
108
|
requirements: []
|
125
109
|
rubyforge_project:
|
126
|
-
rubygems_version:
|
110
|
+
rubygems_version: 2.0.0.rc.2
|
127
111
|
signing_key:
|
128
|
-
specification_version:
|
112
|
+
specification_version: 4
|
129
113
|
summary: Adds private_* and protected_* attr_reader, attr_writer, and attr_accessor
|
130
114
|
test_files:
|
131
115
|
- test/lib/test_scoped_attr_accessor.rb
|