attr_plus 0.2.2 → 0.3.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/README.md +48 -4
- data/lib/attr_plus/instance.rb +18 -19
- data/spec/attr_plus/class_spec.rb +13 -6
- data/spec/attr_plus/instance_spec.rb +71 -0
- metadata +25 -37
data/README.md
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
Adds `#class_attr_accessor` (and reader/writer) and `#inheritable_class_attr_accessor` (and
|
6
6
|
reader/writer of course) to Class.
|
7
7
|
|
8
|
+
require 'attr_plus'
|
9
|
+
|
8
10
|
class Polygon
|
9
11
|
class_attr_accessor :sides
|
10
12
|
end
|
@@ -33,6 +35,8 @@ You can provide default values using a hash with :default set for the last value
|
|
33
35
|
creating one accessor/reader/writer add `=> defaultvalue` to the end, this example should
|
34
36
|
make it more clear:
|
35
37
|
|
38
|
+
require 'attr_plus/class'
|
39
|
+
|
36
40
|
class Person
|
37
41
|
class_attr_accessor :name => 'John Doe'
|
38
42
|
inheritable_class_attr_accessor :arms, :legs, :default => 2
|
@@ -52,10 +56,12 @@ make it more clear:
|
|
52
56
|
|
53
57
|
## ModuleAttr
|
54
58
|
|
55
|
-
Almost exactly the same as `class_*` but for modules. __Note__ there is no module
|
59
|
+
Almost exactly the same as `class_*` but for modules. __Note__ there is no module inheritance
|
56
60
|
so `inheritable_module_*` will not work, I am thinking of adding `includable_module_*` later
|
57
61
|
but they aren't in yet so can't be used!
|
58
62
|
|
63
|
+
require 'attr_plus/module'
|
64
|
+
|
59
65
|
module MyHouse
|
60
66
|
module_attr_accessor :width, :height, :default => 200
|
61
67
|
module_attr_accessor :rooms => []
|
@@ -67,6 +73,44 @@ but they aren't in yet so can't be used!
|
|
67
73
|
House.rooms.first #=> :living_room
|
68
74
|
|
69
75
|
|
76
|
+
## Instance Extensions
|
77
|
+
|
78
|
+
I've also added extensions to the methods for creating accessors on instances. These create
|
79
|
+
private versions, working exactly the same in every other way.
|
80
|
+
|
81
|
+
require 'attr_plus/instance'
|
82
|
+
|
83
|
+
class SecretBox
|
84
|
+
attr_writer :stuff
|
85
|
+
private_attr_reader :stuff
|
86
|
+
|
87
|
+
def initialize
|
88
|
+
@stuff = []
|
89
|
+
end
|
90
|
+
|
91
|
+
def <<(val)
|
92
|
+
stuff << val
|
93
|
+
end
|
94
|
+
|
95
|
+
def shake
|
96
|
+
stuff[rand(stuff.size)]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
box = SecretBox.new
|
101
|
+
box << "giraffe"
|
102
|
+
box << "elephant"
|
103
|
+
box << "camel"
|
104
|
+
box << "cat"
|
105
|
+
p box.shake #=> "giraffe"
|
106
|
+
p box.stuff # NoMethodError: private method 'stuff' called
|
107
|
+
box.stuff = %w(dog hamster fish)
|
108
|
+
p box.shake #=> "fish"
|
109
|
+
|
110
|
+
And then as usual there are `private_attr_accessor` and `private_attr_writer` which work
|
111
|
+
as expected.
|
112
|
+
|
113
|
+
|
70
114
|
## Install
|
71
115
|
|
72
116
|
(sudo) gem install attr_plus
|
@@ -79,13 +123,13 @@ but they aren't in yet so can't be used!
|
|
79
123
|
# or for specific methods
|
80
124
|
require 'attr_plus/class' # for only class_*
|
81
125
|
require 'attr_plus/module' # for only module_*
|
126
|
+
require 'attr_plus/instance' # for only private_attr_*
|
82
127
|
|
83
128
|
|
84
129
|
### Important!
|
85
130
|
|
86
|
-
If in a class you define the `self.inherited` method, make sure to call super
|
87
|
-
otherwise default values will not be set for the subclass.
|
88
|
-
anyway in a method like that.
|
131
|
+
If in a class you define the `self.inherited` method, make sure to call super
|
132
|
+
somewhere otherwise default values will not be set for the subclass.
|
89
133
|
|
90
134
|
|
91
135
|
## Thanks
|
data/lib/attr_plus/instance.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
1
|
+
class Module
|
2
|
+
|
3
|
+
def private_attr_accessor(*args)
|
4
|
+
private_attr_writer(*args)
|
5
|
+
private_attr_reader(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
def private_attr_writer(*args)
|
9
|
+
attr_writer(*args)
|
10
|
+
private(*args.map {|i| "#{i.to_s}=".to_sym })
|
11
|
+
end
|
12
|
+
|
13
|
+
def private_attr_reader(*args)
|
14
|
+
attr_reader(*args)
|
15
|
+
private(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Class do
|
4
4
|
|
5
|
-
describe "
|
5
|
+
describe ".class_attr_reader" do
|
6
6
|
subject { Class.new { class_attr_reader :test } }
|
7
7
|
|
8
8
|
it "defines a read method for the class" do
|
@@ -14,7 +14,7 @@ describe Class do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
describe "
|
17
|
+
describe ".class_attr_writer" do
|
18
18
|
subject { Class.new { class_attr_writer :test } }
|
19
19
|
|
20
20
|
it "defines a write method for the class" do
|
@@ -22,7 +22,7 @@ describe Class do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
describe "
|
25
|
+
describe ".class_attr_accessor" do
|
26
26
|
subject { Class.new { class_attr_accessor :test } }
|
27
27
|
|
28
28
|
it "defines a read method for the class" do
|
@@ -69,7 +69,7 @@ describe Class do
|
|
69
69
|
|
70
70
|
# Inheritable class attributes
|
71
71
|
|
72
|
-
describe "
|
72
|
+
describe ".inheritable_class_attr_reader" do
|
73
73
|
subject { Class.new { inheritable_class_attr_reader :test } }
|
74
74
|
|
75
75
|
it "defines a read method for the class" do
|
@@ -81,7 +81,7 @@ describe Class do
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
|
-
describe "
|
84
|
+
describe ".inheritable_class_attr_writer" do
|
85
85
|
subject { Class.new { inheritable_class_attr_writer :test } }
|
86
86
|
|
87
87
|
it "defines a write method for the class" do
|
@@ -89,7 +89,7 @@ describe Class do
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
describe "
|
92
|
+
describe ".inheritable_class_attr_accessor" do
|
93
93
|
subject { Class.new { inheritable_class_attr_accessor :test } }
|
94
94
|
|
95
95
|
it "defines a read method for the class" do
|
@@ -162,4 +162,11 @@ describe Class do
|
|
162
162
|
|
163
163
|
end
|
164
164
|
|
165
|
+
|
166
|
+
# Class Variables
|
167
|
+
|
168
|
+
describe ".cattr_accessor" do
|
169
|
+
|
170
|
+
end
|
171
|
+
|
165
172
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Module do
|
4
|
+
|
5
|
+
describe ".private_attr_reader" do
|
6
|
+
subject {
|
7
|
+
Class.new {
|
8
|
+
private_attr_reader :test
|
9
|
+
attr_writer :test
|
10
|
+
def read; test; end
|
11
|
+
}.new
|
12
|
+
}
|
13
|
+
|
14
|
+
it "defines a private reader method" do
|
15
|
+
subject.should_not respond_to :test
|
16
|
+
subject.respond_to?(:test, true).should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "allows the class to call it" do
|
20
|
+
subject.test = "hi"
|
21
|
+
subject.instance_eval("test").should == "hi"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".private_attr_writer" do
|
26
|
+
subject {
|
27
|
+
Class.new {
|
28
|
+
private_attr_writer :test
|
29
|
+
attr_reader :test
|
30
|
+
}.new
|
31
|
+
}
|
32
|
+
|
33
|
+
it "defines a private reader method" do
|
34
|
+
subject.should_not respond_to :test=
|
35
|
+
subject.respond_to?(:test=, true).should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "allows the class to call it" do
|
39
|
+
subject.instance_eval("self.test = 'hi'")
|
40
|
+
subject.test.should == "hi"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ".private_attr_accessor" do
|
45
|
+
subject {
|
46
|
+
Class.new {
|
47
|
+
private_attr_accessor :test
|
48
|
+
def read; test; end
|
49
|
+
}.new
|
50
|
+
}
|
51
|
+
|
52
|
+
it "defines private writer and reader methods" do
|
53
|
+
subject.should_not respond_to :test
|
54
|
+
subject.respond_to?(:test, true).should be_true
|
55
|
+
|
56
|
+
subject.should_not respond_to :test=
|
57
|
+
subject.respond_to?(:test=, true).should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "allows the class to write to it" do
|
61
|
+
subject.instance_eval("self.test = 'hi'")
|
62
|
+
subject.read.should == "hi"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "allows the class to read it" do
|
66
|
+
subject.send(:test=, "hi")
|
67
|
+
subject.instance_eval("test").should == "hi"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
CHANGED
@@ -1,32 +1,25 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_plus
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 2
|
9
|
-
version: 0.2.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Joshua Hawxwell
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2011-02-22 00:00:00 +00:00
|
12
|
+
date: 2011-04-27 00:00:00.000000000 +01:00
|
18
13
|
default_executable:
|
19
14
|
dependencies: []
|
20
|
-
|
21
|
-
|
15
|
+
description: ! " Provides attr_accessor style methods for easily creating methods
|
16
|
+
for\n working with class level instance variables and module level instance\n
|
17
|
+
\ variables. Variables can be inherited and have default values.\n"
|
22
18
|
email: m@hawx.me
|
23
19
|
executables: []
|
24
|
-
|
25
20
|
extensions: []
|
26
|
-
|
27
21
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
files:
|
22
|
+
files:
|
30
23
|
- README.md
|
31
24
|
- LICENSE
|
32
25
|
- lib/attr_plus/class.rb
|
@@ -36,43 +29,38 @@ files:
|
|
36
29
|
- lib/attr_plus.rb
|
37
30
|
- lib/class_attr.rb
|
38
31
|
- spec/attr_plus/class_spec.rb
|
32
|
+
- spec/attr_plus/instance_spec.rb
|
39
33
|
- spec/attr_plus/module_spec.rb
|
40
34
|
- spec/class_attr_spec.rb
|
41
35
|
- spec/spec_helper.rb
|
42
36
|
has_rdoc: false
|
43
37
|
homepage: http://github.com/hawx/attr_plus
|
44
38
|
licenses: []
|
45
|
-
|
46
39
|
post_install_message:
|
47
40
|
rdoc_options: []
|
48
|
-
|
49
|
-
require_paths:
|
41
|
+
require_paths:
|
50
42
|
- lib
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
44
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
|
58
|
-
version: "0"
|
59
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
50
|
none: false
|
61
|
-
requirements:
|
62
|
-
- -
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
|
65
|
-
- 0
|
66
|
-
version: "0"
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
67
55
|
requirements: []
|
68
|
-
|
69
56
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.
|
57
|
+
rubygems_version: 1.6.2
|
71
58
|
signing_key:
|
72
59
|
specification_version: 3
|
73
60
|
summary: attr_accessor for class and module level instance variables.
|
74
|
-
test_files:
|
61
|
+
test_files:
|
75
62
|
- spec/attr_plus/class_spec.rb
|
63
|
+
- spec/attr_plus/instance_spec.rb
|
76
64
|
- spec/attr_plus/module_spec.rb
|
77
65
|
- spec/class_attr_spec.rb
|
78
66
|
- spec/spec_helper.rb
|