MarkMenard-sattr_accessor 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.
- data/VERSION.yml +4 -0
- data/lib/sattr_accessor.rb +50 -0
- data/test/sattr_accessor_test.rb +129 -0
- data/test/test_helper.rb +10 -0
- metadata +57 -0
data/VERSION.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class Class
|
2
|
+
|
3
|
+
def sattr_reader (sym, options = {})
|
4
|
+
options = { :inheritable => false }.merge(options)
|
5
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
6
|
+
def self.#{sym}
|
7
|
+
@#{sym}
|
8
|
+
end
|
9
|
+
|
10
|
+
def #{sym}
|
11
|
+
result = self.class.#{sym}
|
12
|
+
#{
|
13
|
+
"
|
14
|
+
if result.is_a?(Array)
|
15
|
+
# Get the value from our ancestor if there is one.
|
16
|
+
if self.class.superclass.respond_to? :#{sym}
|
17
|
+
result << self.class.superclass.#{sym} if self.class.superclass.#{sym}.is_a?(Array)
|
18
|
+
end
|
19
|
+
result.flatten!
|
20
|
+
end
|
21
|
+
if result.is_a?(Hash)
|
22
|
+
# Get the value from our ancestor if there is one.
|
23
|
+
if self.class.superclass.respond_to? :#{sym}
|
24
|
+
result = self.class.superclass.#{sym}.merge(result) if self.class.superclass.#{sym}.is_a?(Hash)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
" if options[:inheritable] }
|
28
|
+
result
|
29
|
+
end
|
30
|
+
EOS
|
31
|
+
end
|
32
|
+
|
33
|
+
def sattr_writer (sym)
|
34
|
+
class_eval %Q{
|
35
|
+
def self.#{sym}= (value)
|
36
|
+
@#{sym} = value
|
37
|
+
end
|
38
|
+
|
39
|
+
def #{sym}= (value)
|
40
|
+
self.class.#{sym} = value
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def sattr_accessor (sym, options = {})
|
46
|
+
sattr_reader(sym, options)
|
47
|
+
sattr_writer(sym)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class SattrAccessorTest < Test::Unit::TestCase
|
4
|
+
context "SingletonAttrExample1" do
|
5
|
+
setup do
|
6
|
+
@ob = SingletonAttrExample1.new
|
7
|
+
SingletonAttrExample1.name = '1'
|
8
|
+
end
|
9
|
+
|
10
|
+
should "have name == '1' " do
|
11
|
+
assert_equal @ob.name, '1'
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with a SingletonAttrExample2" do
|
15
|
+
setup do
|
16
|
+
@ob2 = SingletonAttrExample2.new
|
17
|
+
SingletonAttrExample2.name = '2'
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have name == '2'" do
|
21
|
+
assert_equal @ob2.name, '2'
|
22
|
+
end
|
23
|
+
|
24
|
+
context "instance changes name on SingletonAttrExample1 to '1prime' " do
|
25
|
+
setup do
|
26
|
+
@ob.name = '1prime'
|
27
|
+
end
|
28
|
+
|
29
|
+
context "SingletonAttrExample1" do
|
30
|
+
should "have name == '1prime'" do
|
31
|
+
assert_equal SingletonAttrExample1.name, '1prime'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "SingletonAttrExample1 instance" do
|
36
|
+
should "have name == '1prime' " do
|
37
|
+
assert_equal @ob.name, '1prime'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
context "SingletonAttrExample1" do
|
44
|
+
should "have name == '1' " do
|
45
|
+
assert_equal SingletonAttrExample1.name, '1'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "SingletonAttrExample2 instance changes name" do
|
50
|
+
setup do
|
51
|
+
@ob2.name = '2prime'
|
52
|
+
end
|
53
|
+
|
54
|
+
context "SingletonAttrExample2 name" do
|
55
|
+
should "== '2prime'" do
|
56
|
+
assert_equal SingletonAttrExample2.name, '2prime'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "Using [] attributes in a hierarchy" do
|
66
|
+
setup do
|
67
|
+
@ex1 = SingletonAttrExample1.new
|
68
|
+
@ex2 = SingletonAttrExample2.new
|
69
|
+
@ex3 = SingletonAttrExample3.new
|
70
|
+
|
71
|
+
@ex1.name = [ '1' ]
|
72
|
+
@ex2.name = [ '2' ]
|
73
|
+
@ex3.name = [ '3' ]
|
74
|
+
end
|
75
|
+
|
76
|
+
context "@ex1.name" do
|
77
|
+
should "have one entry" do
|
78
|
+
puts @ex1.name.inspect
|
79
|
+
assert_equal @ex1.name.size, 1
|
80
|
+
end
|
81
|
+
|
82
|
+
should "have a '1' entry in name" do
|
83
|
+
assert_equal @ex1.name[0], '1'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "@ex2.name" do
|
88
|
+
should "have 2 entries" do
|
89
|
+
puts "\n@ex2.name.inspect #{@ex2.name.inspect}"
|
90
|
+
puts "@ex2.name.size = #{@ex2.name.size}"
|
91
|
+
puts "@ex2.name.size = #{@ex2.name.size}"
|
92
|
+
puts "@ex2.name.size = #{@ex2.name.size}"
|
93
|
+
puts "@ex2.name.size = #{@ex2.name.size}"
|
94
|
+
puts "@ex2.name.size = #{@ex2.name.size}"
|
95
|
+
assert @ex2.name.size == 2, "@ex2.name.size = #{@ex2.name.size}"
|
96
|
+
assert @ex2.name.size == 2, "@ex2.name.size = #{@ex2.name.size}"
|
97
|
+
assert @ex2.name.size == 2, "@ex2.name.size = #{@ex2.name.size}"
|
98
|
+
assert @ex2.name.size == 2, "@ex2.name.size = #{@ex2.name.size}"
|
99
|
+
end
|
100
|
+
|
101
|
+
should "should contain '1'" do
|
102
|
+
assert @ex2.name.any? { |v| v == '1' }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "@ex3.name" do
|
107
|
+
should "have 3 entries" do
|
108
|
+
puts "\n@ex3.name.inspect = #{@ex3.name.inspect}"
|
109
|
+
puts "@ex3.name.size = #{@ex3.name.size}"
|
110
|
+
assert_equal @ex3.name.size, 3
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
class SingletonAttrExample1
|
122
|
+
sattr_accessor :name, :inheritable => true
|
123
|
+
end
|
124
|
+
|
125
|
+
class SingletonAttrExample2 < SingletonAttrExample1
|
126
|
+
end
|
127
|
+
|
128
|
+
class SingletonAttrExample3 < SingletonAttrExample2
|
129
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: MarkMenard-sattr_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Menard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: mark@mjm.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/sattr_accessor.rb
|
27
|
+
- test/sattr_accessor_test.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/MarkMenard/sattr_accessor
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --inline-source
|
34
|
+
- --charset=UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: TODO
|
56
|
+
test_files: []
|
57
|
+
|