cattr 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/cattr.rb +1 -8
- data/test/test_cattr.rb +17 -7
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/cattr.rb
CHANGED
@@ -11,14 +11,7 @@ class Class
|
|
11
11
|
def cattr_writer(*meths)
|
12
12
|
meths.each do |meth|
|
13
13
|
self.class.send(:define_method, :"#{meth}=") do |arg|
|
14
|
-
|
15
|
-
# we don't want to overwrite it.
|
16
|
-
begin
|
17
|
-
class_variable_get("@@#{meth}")
|
18
|
-
rescue NameError
|
19
|
-
# if we get a nameerror, then it didn't exist so let's set it
|
20
|
-
class_variable_set("@@#{meth}", arg)
|
21
|
-
end
|
14
|
+
class_variable_set("@@#{meth}", arg)
|
22
15
|
end
|
23
16
|
end
|
24
17
|
end
|
data/test/test_cattr.rb
CHANGED
@@ -6,28 +6,38 @@ class TestCattr < Test::Unit::TestCase
|
|
6
6
|
cattr_accessor :cow
|
7
7
|
end
|
8
8
|
Moo.cow = 'test'
|
9
|
-
|
9
|
+
assert_equal('test',Moo.cow)
|
10
10
|
end
|
11
11
|
|
12
12
|
should "create class setter" do
|
13
|
-
class
|
13
|
+
class Moo1
|
14
14
|
cattr_writer :cow
|
15
15
|
def self.get
|
16
16
|
@@cow
|
17
17
|
end
|
18
18
|
end
|
19
|
-
|
20
|
-
|
19
|
+
Moo1.cow = 'test'
|
20
|
+
assert_equal('test',Moo1.get)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "set and reset attributes" do
|
24
|
+
class Moo2
|
25
|
+
cattr_accessor :cow
|
26
|
+
end
|
27
|
+
Moo2.cow = 'test'
|
28
|
+
assert('test',Moo2.cow)
|
29
|
+
Moo2.cow = 'test2'
|
30
|
+
assert_equal('test2',Moo2.cow)
|
21
31
|
end
|
22
32
|
|
23
33
|
should "create class reder" do
|
24
|
-
class
|
34
|
+
class Moo3
|
25
35
|
cattr_reader :cow
|
26
36
|
def self.get=(arg)
|
27
37
|
@@cow = arg
|
28
38
|
end
|
29
39
|
end
|
30
|
-
|
31
|
-
|
40
|
+
Moo3.get = 'test'
|
41
|
+
assert_equal('test',Moo3.cow)
|
32
42
|
end
|
33
43
|
end
|