private_attr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in private_attr.gemspec
4
+ gemspec
5
+ gem 'rake'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jacob Swanner
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,88 @@
1
+ # PrivateAttr [![Build Status](https://secure.travis-ci.org/jswanner/private_attr.png?branch=master)](https://travis-ci.org/jswanner/private_attr)
2
+
3
+ Perhaps it's just personal preference, but I don't like to use instance
4
+ variables througout my Ruby classes. At the same time, I don't
5
+ necessarily want public attribute readers and writers. This causes me to
6
+ tend to use something like the following:
7
+
8
+ ```ruby
9
+ class Foo
10
+ def initialize bar
11
+ self.bar = bar
12
+ end
13
+
14
+ private
15
+
16
+ attr_accessor :bar
17
+ end
18
+ ```
19
+
20
+ But, I really like to declare my attribute readers and writers near the
21
+ top of my classes, which means I sometimes use something like:
22
+
23
+ ```ruby
24
+ class Bar
25
+ attr_accessor :foo
26
+ private :foo, :foo=
27
+
28
+ def initialize foo
29
+ self.foo = foo
30
+ end
31
+ end
32
+ ```
33
+
34
+ This library is meant to be a convenient solution to that verbosity.
35
+
36
+ ## Installation
37
+
38
+ Add this line to your application's Gemfile:
39
+
40
+ gem 'private_attr'
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install private_attr
49
+
50
+ ## Usage
51
+
52
+ ```ruby
53
+ class Bar
54
+ extend PrivateAttr
55
+
56
+ private_attr_accessor :foo
57
+ private_attr_reader :baz
58
+ private_attr_writer :bat
59
+
60
+ def initialize foo
61
+ self.foo = foo
62
+ end
63
+ end
64
+ ```
65
+
66
+ It even does protected attributes:
67
+
68
+ ```ruby
69
+ class Bar
70
+ extend PrivateAttr
71
+
72
+ protected_attr_accessor :foo
73
+ protected_attr_reader :baz
74
+ protected_attr_writer :bat
75
+
76
+ def initialize foo
77
+ self.foo = foo
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.name = 'spec'
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module PrivateAttr
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ require "private_attr/version"
2
+
3
+ module PrivateAttr
4
+ def private_attr_accessor *attr
5
+ private_attr_reader *attr
6
+ private_attr_writer *attr
7
+ end
8
+
9
+ def private_attr_reader *attr
10
+ attr_reader *attr
11
+ private *attr
12
+ end
13
+
14
+ def private_attr_writer *attr
15
+ attr_writer *attr
16
+ private *attr.map { |a| "#{a}=" }
17
+ end
18
+
19
+ def protected_attr_accessor *attr
20
+ protected_attr_reader *attr
21
+ protected_attr_writer *attr
22
+ end
23
+
24
+ def protected_attr_reader *attr
25
+ attr_reader *attr
26
+ protected *attr
27
+ end
28
+
29
+ def protected_attr_writer *attr
30
+ attr_writer *attr
31
+ protected *attr.map { |a| "#{a}=" }
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'private_attr/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "private_attr"
8
+ gem.version = PrivateAttr::VERSION
9
+ gem.authors = ["Jacob Swanner"]
10
+ gem.email = ["jacob@jacobswanner.com"]
11
+ gem.description = %q{Easily create private/protected attribute readers/writers}
12
+ gem.summary = %q{Easily create private/protected attribute readers/writers}
13
+ gem.homepage = "https://github.com/jswanner/private_attr"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,245 @@
1
+ require 'minitest/autorun'
2
+ require 'private_attr'
3
+
4
+ class Dummy
5
+ def read_accessor
6
+ accessor
7
+ end
8
+
9
+ def read_accessor_other other
10
+ other.accessor
11
+ end
12
+
13
+ def write_accessor value
14
+ self.accessor = value
15
+ end
16
+
17
+ def write_accessor_other other, value
18
+ other.accessor = value
19
+ end
20
+
21
+ def read_reader
22
+ reader
23
+ end
24
+
25
+ def read_reader_other other
26
+ other.reader
27
+ end
28
+
29
+ def write_writer value
30
+ self.writer = value
31
+ end
32
+
33
+ def write_writer_other other, value
34
+ other.writer = value
35
+ end
36
+ end
37
+
38
+ class PrivateDummy < Dummy
39
+ extend PrivateAttr
40
+ private_attr_accessor :accessor
41
+ private_attr_reader :reader
42
+ private_attr_writer :writer
43
+ end
44
+
45
+ class ProtectedDummy < Dummy
46
+ extend PrivateAttr
47
+ protected_attr_accessor :accessor
48
+ protected_attr_reader :reader
49
+ protected_attr_writer :writer
50
+ end
51
+
52
+ describe PrivateAttr do
53
+ let(:dummy) { dummy_class.new }
54
+ let(:other) { dummy_class.new }
55
+ let(:old_value) { 'old value' }
56
+ let(:value) { 'value' }
57
+
58
+ describe 'private_attr_accessor' do
59
+ let(:dummy_class) { PrivateDummy }
60
+
61
+ before { dummy.instance_variable_set '@accessor', old_value }
62
+
63
+ it 'allows attribute to be read internally' do
64
+ dummy.read_accessor.must_equal old_value
65
+ end
66
+
67
+ it 'allows attribute to be written internally' do
68
+ dummy.write_accessor value
69
+ dummy.instance_variable_get('@accessor').must_equal value
70
+ end
71
+
72
+ it 'raises Error when read externally' do
73
+ -> { dummy.accessor }.must_raise NoMethodError
74
+ end
75
+
76
+ it 'raises Error when written externally' do
77
+ -> { dummy.accessor = value }.must_raise NoMethodError
78
+ end
79
+
80
+ it 'raises Error when read by other' do
81
+ -> { dummy.read_accessor_other other }.must_raise NoMethodError
82
+ end
83
+
84
+ it 'raises Error when written by other' do
85
+ -> { dummy.write_accessor_other other, value }.must_raise NoMethodError
86
+ end
87
+ end
88
+
89
+ describe 'private_attr_reader' do
90
+ let(:dummy_class) { PrivateDummy }
91
+
92
+ before { dummy.instance_variable_set '@reader', old_value }
93
+
94
+ it 'allows attribute to be read internally' do
95
+ dummy.read_reader.must_equal old_value
96
+ end
97
+
98
+ it 'raises Error when written internally' do
99
+ -> { dummy.write_reader value }.must_raise NoMethodError
100
+ end
101
+
102
+ it 'raises Error when read externally' do
103
+ -> { dummy.reader }.must_raise NoMethodError
104
+ end
105
+
106
+ it 'raises Error when written externally' do
107
+ -> { dummy.reader = value }.must_raise NoMethodError
108
+ end
109
+
110
+ it 'raises Error when read by other' do
111
+ -> { dummy.read_reader_other other }.must_raise NoMethodError
112
+ end
113
+
114
+ it 'raises Error when written by other' do
115
+ -> { dummy.write_reader_other other, value }.must_raise NoMethodError
116
+ end
117
+ end
118
+
119
+ describe 'private_attr_writer' do
120
+ let(:dummy_class) { PrivateDummy }
121
+
122
+ before { dummy.instance_variable_set '@writer', old_value }
123
+
124
+ it 'raises Error when read internally' do
125
+ -> { dummy.read_writer }.must_raise NoMethodError
126
+ end
127
+
128
+ it 'allows attribute to be written internally' do
129
+ dummy.write_writer value
130
+ dummy.instance_variable_get('@writer').must_equal value
131
+ end
132
+
133
+ it 'raises Error when read externally' do
134
+ -> { dummy.writer }.must_raise NoMethodError
135
+ end
136
+
137
+ it 'raises Error when written externally' do
138
+ -> { dummy.writer = value }.must_raise NoMethodError
139
+ end
140
+
141
+ it 'raises Error when read by other' do
142
+ -> { dummy.read_writer_other other }.must_raise NoMethodError
143
+ end
144
+
145
+ it 'raises Error when written by other' do
146
+ -> { dummy.write_writer_other other, value }.must_raise NoMethodError
147
+ end
148
+ end
149
+
150
+ describe 'protected_attr_accessor' do
151
+ let(:dummy_class) { ProtectedDummy }
152
+
153
+ before { dummy.instance_variable_set '@accessor', old_value }
154
+
155
+ it 'allows attribute to be read internally' do
156
+ dummy.read_accessor.must_equal old_value
157
+ end
158
+
159
+ it 'allows attribute to be written internally' do
160
+ dummy.write_accessor value
161
+ dummy.instance_variable_get('@accessor').must_equal value
162
+ end
163
+
164
+ it 'raises Error when read externally' do
165
+ -> { dummy.accessor }.must_raise NoMethodError
166
+ end
167
+
168
+ it 'raises Error when written externally' do
169
+ -> { dummy.accessor = value }.must_raise NoMethodError
170
+ end
171
+
172
+ it 'allows attribute to be read by other' do
173
+ other.instance_variable_set '@accessor', value
174
+ dummy.read_accessor_other(other).must_equal value
175
+ end
176
+
177
+ it 'allows attribute to be written by other' do
178
+ dummy.write_accessor_other other, value
179
+ other.instance_variable_get('@accessor').must_equal value
180
+ end
181
+ end
182
+
183
+ describe 'protected_attr_reader' do
184
+ let(:dummy_class) { ProtectedDummy }
185
+
186
+ before { dummy.instance_variable_set '@reader', old_value }
187
+
188
+ it 'allows attribute to be read internally' do
189
+ dummy.read_reader.must_equal old_value
190
+ end
191
+
192
+ it 'raises Error when written internally' do
193
+ -> { dummy.write_reader value }.must_raise NoMethodError
194
+ end
195
+
196
+ it 'raises Error when read externally' do
197
+ -> { dummy.reader }.must_raise NoMethodError
198
+ end
199
+
200
+ it 'raises Error when written externally' do
201
+ -> { dummy.reader = value }.must_raise NoMethodError
202
+ end
203
+
204
+ it 'allows attribute to be read by other' do
205
+ other.instance_variable_set '@reader', value
206
+ dummy.read_reader_other(other).must_equal value
207
+ end
208
+
209
+ it 'raises Error when written by other' do
210
+ -> { dummy.write_reader_other other, value }.must_raise NoMethodError
211
+ end
212
+ end
213
+
214
+ describe 'protected_attr_writer' do
215
+ let(:dummy_class) { ProtectedDummy }
216
+
217
+ before { dummy.instance_variable_set '@writer', old_value }
218
+
219
+ it 'raises Error when read internally' do
220
+ -> { dummy.read_writer }.must_raise NoMethodError
221
+ end
222
+
223
+ it 'allows attribute to be written internally' do
224
+ dummy.write_writer value
225
+ dummy.instance_variable_get('@writer').must_equal value
226
+ end
227
+
228
+ it 'raises Error when read externally' do
229
+ -> { dummy.writer }.must_raise NoMethodError
230
+ end
231
+
232
+ it 'raises Error when written externally' do
233
+ -> { dummy.writer = value }.must_raise NoMethodError
234
+ end
235
+
236
+ it 'raises Error when read by other' do
237
+ -> { dummy.read_writer_other other }.must_raise NoMethodError
238
+ end
239
+
240
+ it 'allows attribute to be written by other' do
241
+ dummy.write_writer_other other, value
242
+ other.instance_variable_get('@writer').must_equal value
243
+ end
244
+ end
245
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: private_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jacob Swanner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Easily create private/protected attribute readers/writers
15
+ email:
16
+ - jacob@jacobswanner.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .travis.yml
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/private_attr.rb
28
+ - lib/private_attr/version.rb
29
+ - private_attr.gemspec
30
+ - spec/private_attr_spec.rb
31
+ homepage: https://github.com/jswanner/private_attr
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.23
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Easily create private/protected attribute readers/writers
55
+ test_files:
56
+ - spec/private_attr_spec.rb