classx 0.0.3 → 0.0.4

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.
Files changed (57) hide show
  1. data/ChangeLog +354 -0
  2. data/README +5 -0
  3. data/Rakefile +2 -2
  4. data/bench/attribute_get.rb +73 -0
  5. data/bench/attribute_set.rb +53 -19
  6. data/bench/define_attribute.rb +226 -0
  7. data/bench/initialize.rb +25 -7
  8. data/doc/output/coverage/index.html +74 -128
  9. data/doc/output/coverage/lib-classx-attribute_rb.html +291 -190
  10. data/doc/output/coverage/lib-classx-attributes_rb.html +167 -101
  11. data/doc/output/coverage/lib-classx-bracketable_rb.html +671 -0
  12. data/doc/output/coverage/lib-classx-class_attributes_rb.html +775 -0
  13. data/doc/output/coverage/lib-classx-commandable_rb.html +727 -0
  14. data/doc/output/coverage/{-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-block_rb.html → lib-classx-declare_rb.html} +60 -62
  15. data/doc/output/coverage/lib-classx-validate_rb.html +43 -41
  16. data/doc/output/coverage/lib-classx_rb.html +208 -78
  17. data/example/commandable.rb +1 -0
  18. data/lib/classx.rb +146 -16
  19. data/lib/classx/attribute.rb +244 -143
  20. data/lib/classx/attributes.rb +93 -27
  21. data/lib/classx/bracketable.rb +61 -0
  22. data/lib/classx/class_attributes.rb +165 -0
  23. data/lib/classx/commandable.rb +55 -4
  24. data/lib/classx/declare.rb +40 -3
  25. data/lib/classx/role/logger.rb +17 -13
  26. data/lib/classx/validate.rb +26 -24
  27. data/spec/classx/handles_spec.rb +21 -6
  28. data/spec/classx/serialize_spec.rb +57 -0
  29. data/spec/classx/{with_coerce.rb → with_coerce_spec.rb} +0 -0
  30. data/spec/classx/with_extend_spec.rb +58 -0
  31. data/spec/classx/with_include_spec.rb +58 -0
  32. data/spec/classx/with_validate_spec.rb +363 -0
  33. data/spec/classx/without_anyoption_spec.rb +1 -1
  34. data/spec/classx/writable_option_spec.rb +29 -4
  35. data/spec/classx_bracketable_spec.rb +160 -0
  36. data/spec/classx_class_attributes/default_option_spec.rb +121 -0
  37. data/spec/classx_class_attributes/dsl_accessor_spec.rb +88 -0
  38. data/spec/classx_class_attributes/handles_spec.rb +77 -0
  39. data/spec/classx_class_attributes/with_coerce_spec.rb +119 -0
  40. data/spec/classx_class_attributes/with_extend_spec.rb +64 -0
  41. data/spec/classx_class_attributes/with_include_spec.rb +64 -0
  42. data/spec/classx_class_attributes/with_multiple_class_spec.rb +60 -0
  43. data/spec/classx_class_attributes/with_validate_spec.rb +248 -0
  44. data/spec/classx_class_attributes/without_accessor_spec.rb +19 -0
  45. data/spec/classx_class_attributes/without_anyoption_spec.rb +21 -0
  46. data/spec/classx_class_attributes/writable_option_spec.rb +100 -0
  47. data/spec/classx_declare_spec.rb +117 -0
  48. data/spec/classx_validate_spec.rb +1 -3
  49. data/tasks/basic_tasks.rake +1 -1
  50. metadata +36 -26
  51. data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-callbacks_rb.html +0 -932
  52. data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-change_rb.html +0 -779
  53. data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-hunk_rb.html +0 -867
  54. data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs_rb.html +0 -1715
  55. data/doc/output/coverage/-Library-Ruby-Gems-gems-rcov-0_8_1_2_0-lib-rcov_rb.html +0 -1598
  56. data/spec/classx/with_extend.rb +0 -27
  57. data/spec/classx/with_include.rb +0 -27
@@ -0,0 +1,119 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'classx'
3
+
4
+ describe ClassX::ClassAttributes do
5
+ describe '#class_has' do
6
+ describe 'with coece' do
7
+ describe 'when Array is value' do
8
+ before do
9
+ @class = Class.new
10
+ @class.class_eval do
11
+ extend ClassX::ClassAttributes
12
+
13
+ class_has :x, :isa => Integer, :coerce => [
14
+ { proc {|val| val.respond_to? :to_i } => proc {|val| val.to_i } },
15
+ { proc {|val| val.respond_to? :to_s } => proc {|val| val.to_s } },
16
+ ]
17
+ end
18
+ end
19
+
20
+ it 'attrubute :x should convert Str to Integer' do
21
+ lambda {
22
+ @class.class_eval do
23
+ self.x = "10"
24
+ end
25
+ }.should_not raise_error(Exception)
26
+ end
27
+
28
+ it 'attrubute :x should not convert Object to Integer' do
29
+ lambda {
30
+ @class.class_eval do
31
+ self.x = Object.new
32
+ end
33
+ }.should raise_error(ClassX::InvalidAttrArgument)
34
+ end
35
+ end
36
+
37
+ describe 'when Proc is key' do
38
+ before do
39
+ @class = Class.new
40
+ @class.class_eval do
41
+ extend ClassX::ClassAttributes
42
+
43
+ class_has :x, :isa => Integer, :coerce => { proc {|val| val.respond_to? :to_i } => proc {|val| val.to_i } }
44
+ end
45
+ end
46
+
47
+ it 'attrubute :x should convert Str to Integer' do
48
+ lambda {
49
+ @class.class_eval do
50
+ self.x = "10"
51
+ end
52
+ }.should_not raise_error(Exception)
53
+ end
54
+
55
+ it 'attrubute :x should not convert Object to Integer' do
56
+ lambda {
57
+ @class.class_eval do
58
+ self.x = Object.new
59
+ end
60
+ }.should raise_error(ClassX::InvalidAttrArgument)
61
+ end
62
+ end
63
+
64
+ describe 'when Symbol is key' do
65
+ before do
66
+ @class = Class.new
67
+ @class.class_eval do
68
+ extend ClassX::ClassAttributes
69
+
70
+ class_has :x, :isa => Integer, :coerce => { :to_i => proc {|val| val.to_i } }
71
+ end
72
+ end
73
+
74
+ it 'attrubute :x should convert Str to Integer' do
75
+ lambda {
76
+ @class.class_eval do
77
+ self.x = 10
78
+ end
79
+ }.should_not raise_error(Exception)
80
+ end
81
+
82
+ it 'attrubute :x should not convert Object to Integer' do
83
+ lambda {
84
+ @class.class_eval do
85
+ self.x = Object.new
86
+ end
87
+ }.should raise_error(ClassX::InvalidAttrArgument)
88
+ end
89
+ end
90
+
91
+ describe 'when Module or Class is key' do
92
+ before do
93
+ @class = Class.new
94
+ @class.class_eval do
95
+ extend ClassX::ClassAttributes
96
+
97
+ class_has :x, :isa => Integer, :coerce => { String => proc {|val| val.to_i } }
98
+ end
99
+ end
100
+
101
+ it 'attrubute :x should convert Str to Integer' do
102
+ lambda {
103
+ @class.class_eval do
104
+ self.x = 10
105
+ end
106
+ }.should_not raise_error(Exception)
107
+ end
108
+
109
+ it 'attrubute :x should not convert Object to Integer' do
110
+ lambda {
111
+ @class.class_eval do
112
+ self.x = Object.new
113
+ end
114
+ }.should raise_error(ClassX::InvalidAttrArgument)
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'classx'
3
+
4
+ describe ClassX::ClassAttributes do
5
+ describe '#class_has' do
6
+ describe 'with extend' do
7
+ describe "take a module as value" do
8
+ before do
9
+ @class = Class.new
10
+ mod = Module.new
11
+ mod.module_eval do
12
+ define_method :test do
13
+ :test
14
+ end
15
+ end
16
+ @class.class_eval do
17
+ extend ClassX::ClassAttributes
18
+
19
+ class_has :x, :extend => mod
20
+ end
21
+ end
22
+
23
+ it 'attrubute :x should have test class method' do
24
+ @class.class_eval do
25
+ self.x = 10
26
+ end
27
+ @class.class_attribute_of['x'].class.test.should == :test
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "take multi modules as value" do
33
+ before do
34
+ @class = Class.new
35
+ mod1 = Module.new
36
+ mod1.module_eval do
37
+ define_method :test1 do
38
+ :test1
39
+ end
40
+ end
41
+
42
+ mod2 = Module.new
43
+ mod2.module_eval do
44
+ define_method :test2 do
45
+ :test2
46
+ end
47
+ end
48
+ @class.class_eval do
49
+ extend ClassX::ClassAttributes
50
+
51
+ class_has :x, :extend => [ mod1, mod2 ]
52
+ end
53
+ end
54
+
55
+ it 'attrubute :x should have #test method' do
56
+ @class.class_eval do
57
+ self.x = 10
58
+ end
59
+ @class.class_attribute_of['x'].class.test1.should == :test1
60
+ @class.class_attribute_of['x'].class.test2.should == :test2
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'classx'
3
+
4
+ describe ClassX::ClassAttributes do
5
+ describe '#class_has' do
6
+ describe 'with include' do
7
+ describe "take a module as value" do
8
+ before do
9
+ @class = Class.new
10
+ mod = Module.new
11
+ mod.module_eval do
12
+ define_method :test do
13
+ :test
14
+ end
15
+ end
16
+ @class.class_eval do
17
+ extend ClassX::ClassAttributes
18
+
19
+ class_has :x, :include => mod
20
+ end
21
+ end
22
+
23
+ it 'attrubute :x should have #test method' do
24
+ @class.class_eval do
25
+ self.x = 10
26
+ end
27
+ @class.class_attribute_of['x'].test.should == :test
28
+ end
29
+ end
30
+
31
+ describe "take multi modules as value" do
32
+ before do
33
+ @class = Class.new
34
+ mod1 = Module.new
35
+ mod1.module_eval do
36
+ define_method :test1 do
37
+ :test1
38
+ end
39
+ end
40
+
41
+ mod2 = Module.new
42
+ mod2.module_eval do
43
+ define_method :test2 do
44
+ :test2
45
+ end
46
+ end
47
+ @class.class_eval do
48
+ extend ClassX::ClassAttributes
49
+
50
+ class_has :x, :include => [ mod1, mod2 ]
51
+ end
52
+ end
53
+
54
+ it 'attrubute :x should have #test method' do
55
+ @class.class_eval do
56
+ self.x = 10
57
+ end
58
+ @class.class_attribute_of['x'].test1.should == :test1
59
+ @class.class_attribute_of['x'].test2.should == :test2
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,60 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'classx'
3
+
4
+ describe ClassX::ClassAttributes do
5
+ describe '#class_has' do
6
+ describe 'with multiple class' do
7
+ before do
8
+ @class1 = Class.new
9
+ @class1.class_eval do
10
+ extend ClassX::ClassAttributes
11
+ class_has :x
12
+ end
13
+ @class2 = Class.new
14
+ @class2.class_eval do
15
+ extend ClassX::ClassAttributes
16
+ end
17
+ end
18
+
19
+ it 'should not raise AttrRequiredError when initialized anothor class' do
20
+ lambda { @class2.class_attribute_of }.should_not raise_error(ClassX::AttrRequiredError)
21
+ end
22
+ end
23
+
24
+ describe 'inherit class accessor' do
25
+ before do
26
+ @class1 = Class.new
27
+ @class1.class_eval do
28
+ extend ClassX::ClassAttributes
29
+ class_has :x, :default => :default
30
+ end
31
+ @class2 = Class.new(@class1)
32
+ end
33
+
34
+ it 'should inherit class attribute' do
35
+ @class2.class_attribute_of.keys.should == ['x']
36
+ end
37
+
38
+ it "should not be same class attribute's instance" do
39
+ @class1.class_attribute_of.each do |k1, v1|
40
+ @class2.class_attribute_of.each do |k2, v2|
41
+ if k1 == k2
42
+ v1.should_not == v2
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ it "should not affect subclass changing class attribute value" do
49
+ @class1.class_eval do
50
+ self.x = "hoge"
51
+ end
52
+ @class2.x.should_not == "hoge"
53
+ end
54
+
55
+ it 'should be able to inherit default value' do
56
+ @class2.x.should == :default
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,248 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require 'classx'
3
+
4
+ describe ClassX::ClassAttributes do
5
+ describe '#class_has' do
6
+ describe 'with :validate option' do
7
+ describe 'when value is Proc' do
8
+ before do
9
+ @class = Class.new
10
+ @class.class_eval do
11
+ extend ClassX::ClassAttributes
12
+ class_has :x,
13
+ :writable => true,
14
+ :validate => proc {|val| val.kind_of? Fixnum }
15
+ end
16
+ end
17
+
18
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
19
+ lambda { @class.x = "str" }.should raise_error(ClassX::InvalidAttrArgument)
20
+ end
21
+
22
+ it 'should not raise error when it take valid args on update value' do
23
+ lambda { @class.x = 20 }.should_not raise_error(Exception)
24
+ end
25
+ end
26
+
27
+ describe "when value is Regexp" do
28
+ before do
29
+ @class = Class.new
30
+ @class.class_eval do
31
+ extend ClassX::ClassAttributes
32
+ class_has :x,
33
+ :writable => true,
34
+ :validate => /^test_/
35
+
36
+ end
37
+ end
38
+
39
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
40
+ lambda { @class.x = "hoge" }.should raise_error(ClassX::InvalidAttrArgument)
41
+ end
42
+
43
+ it 'should raise ClassX::InvalidAttrArgument when it take none String argument on update value' do
44
+ lambda { @class.x = :fuga }.should raise_error(ClassX::InvalidAttrArgument)
45
+ end
46
+
47
+ it 'should not raise error when it take valid args on update value' do
48
+ lambda { @class.x = "test_fuga" }.should_not raise_error(Exception)
49
+ end
50
+ end
51
+
52
+ describe 'when value is not Regexp or Proc' do
53
+ before do
54
+ @class = Class.new
55
+ @class.class_eval do
56
+ extend ClassX::ClassAttributes
57
+ class_has :x,
58
+ :writable => true,
59
+ :validate => "validate_value"
60
+
61
+ end
62
+ end
63
+
64
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
65
+ lambda { @class.x = "hoge" }.should raise_error(ClassX::InvalidAttrArgument)
66
+ end
67
+
68
+ it 'should not raise error when it take valid args on update value' do
69
+ lambda { @class.x = "validate_value" }.should_not raise_error(Exception)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe 'with :kind_of option' do
75
+ before do
76
+ @class = Class.new
77
+ @class.class_eval do
78
+ extend ClassX::ClassAttributes
79
+ class_has :x,
80
+ :writable => true,
81
+ :kind_of => Fixnum
82
+ end
83
+ end
84
+
85
+ it 'should define value_class' do
86
+ @class.class_attribute_of['x'].class.value_class.should == Fixnum
87
+ end
88
+
89
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
90
+ lambda { @class.x = "str" }.should raise_error(ClassX::InvalidAttrArgument)
91
+ end
92
+
93
+ it 'should not raise error when it take valid args on update value' do
94
+ lambda { @class.x = 20 }.should_not raise_error(Exception)
95
+ end
96
+ end
97
+
98
+ describe 'with :isa option' do
99
+ before do
100
+ @class = Class.new
101
+ @class.class_eval do
102
+ extend ClassX::ClassAttributes
103
+ class_has :x,
104
+ :writable => true,
105
+ :isa => Fixnum
106
+ end
107
+ end
108
+
109
+ it 'should define value_class' do
110
+ @class.class_attribute_of['x'].class.value_class.should == Fixnum
111
+ end
112
+
113
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
114
+ lambda { @class.x = "str" }.should raise_error(ClassX::InvalidAttrArgument)
115
+ end
116
+
117
+ it 'should not raise error when it take valid args on update value' do
118
+ lambda { @class.x = 20 }.should_not raise_error(Exception)
119
+ end
120
+ end
121
+
122
+ describe 'with :respond_to option' do
123
+ before do
124
+ @class = Class.new
125
+ @class.class_eval do
126
+ extend ClassX::ClassAttributes
127
+ class_has :x,
128
+ :writable => true,
129
+ :respond_to => :to_int
130
+ end
131
+ end
132
+
133
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
134
+ lambda { @class.x = "str" }.should raise_error(ClassX::InvalidAttrArgument)
135
+ end
136
+
137
+ it 'should not raise error when it take valid args on update value' do
138
+ lambda { @class.x = 20 }.should_not raise_error(Exception)
139
+ end
140
+ end
141
+
142
+ describe 'with :validate_each option and take Array' do
143
+ before do
144
+ @class = Class.new
145
+ @class.class_eval do
146
+ extend ClassX::ClassAttributes
147
+ class_has :x,
148
+ :writable => true,
149
+ :validate_each => proc {|item| item.kind_of? String }
150
+ end
151
+ end
152
+
153
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
154
+ lambda { @class.x = ["str", 10] }.should raise_error(ClassX::InvalidAttrArgument)
155
+ end
156
+
157
+ it 'should not raise error when it take valid args on update value' do
158
+ lambda { @class.x = ['ghi', 'jkl'] }.should_not raise_error(Exception)
159
+ end
160
+ end
161
+
162
+ describe 'with :validate_each option and take Array' do
163
+ before do
164
+ @class = Class.new
165
+ @class.class_eval do
166
+ extend ClassX::ClassAttributes
167
+ class_has :x,
168
+ :writable => true,
169
+ :kind_of => Array,
170
+ :validate_each => proc {|item| item.kind_of? String }
171
+ end
172
+ end
173
+
174
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
175
+ lambda { @class.x = ["str", 10] }.should raise_error(ClassX::InvalidAttrArgument)
176
+ end
177
+
178
+ it 'should not raise error when it take valid args on update value' do
179
+ lambda { @class.x = ['ghi', 'jkl'] }.should_not raise_error(Exception)
180
+ end
181
+ end
182
+
183
+ describe 'with :validate_each option and take Hash' do
184
+ before do
185
+ @class = Class.new
186
+ @class.class_eval do
187
+ extend ClassX::ClassAttributes
188
+ class_has :x,
189
+ :writable => true,
190
+ :validate_each => proc {|key,val| val.kind_of? String }
191
+ end
192
+ end
193
+
194
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
195
+ lambda { @class.x = {'abc' => 10 } }.should raise_error(ClassX::InvalidAttrArgument)
196
+ end
197
+
198
+ it 'should not raise error when it take valid args on update value' do
199
+ lambda { @class.x = { 'ghi' => 'jkl' } }.should_not raise_error(Exception)
200
+ end
201
+ end
202
+
203
+ describe 'with :validate_each option with arity two and :kind_of and take Hash' do
204
+ before do
205
+ @class = Class.new
206
+ @class.class_eval do
207
+ extend ClassX::ClassAttributes
208
+ class_has :x,
209
+ :writable => true,
210
+ :kind_of => Hash,
211
+ :validate_each => proc {|key,val| val.kind_of? String }
212
+ end
213
+ end
214
+
215
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
216
+ lambda { @class.x = {'abc' => 10 } }.should raise_error(ClassX::InvalidAttrArgument)
217
+ end
218
+
219
+ it 'should not raise error when it take valid args on update value' do
220
+ lambda { @class.x = { 'ghi' => 'jkl' } }.should_not raise_error(Exception)
221
+ end
222
+ end
223
+
224
+ describe 'with :validate_each option with arity two and :kind_of and take Hash' do
225
+ before do
226
+ @class = Class.new
227
+ @class.class_eval do
228
+ extend ClassX::ClassAttributes
229
+ class_has :x,
230
+ :writable => true,
231
+ :kind_of => Hash,
232
+ :validate_each => proc {|mod|
233
+ has :foo
234
+ has :bar
235
+ }
236
+ end
237
+ end
238
+
239
+ it 'should raise ClassX::InvalidAttrArgument when it take invalid args on update value' do
240
+ lambda { @class.x = {'abc' => 10 } }.should raise_error(ClassX::AttrRequiredError)
241
+ end
242
+
243
+ it 'should not raise error when it take valid args on update value' do
244
+ lambda { @class.x = {'foo' => :foo, "bar" => :bar, "baz" => :baz } }.should_not raise_error(Exception)
245
+ end
246
+ end
247
+ end
248
+ end