classx 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +408 -2
- data/README +2 -2
- data/Rakefile +4 -3
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-block_rb.html +1 -1
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-callbacks_rb.html +1 -1
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-change_rb.html +1 -1
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs-hunk_rb.html +1 -1
- data/doc/output/coverage/-Library-Ruby-Gems-gems-diff-lcs-1_1_2-lib-diff-lcs_rb.html +1 -1
- data/doc/output/coverage/-Library-Ruby-Gems-gems-rcov-0_8_1_2_0-lib-rcov_rb.html +1 -1
- data/doc/output/coverage/index.html +37 -172
- data/doc/output/coverage/lib-classx-attribute_rb.html +788 -0
- data/doc/output/coverage/{-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-drb-invokemethod_rb.html → lib-classx-attributes_rb.html} +96 -47
- data/doc/output/coverage/lib-classx-validate_rb.html +36 -36
- data/doc/output/coverage/lib-classx_rb.html +74 -151
- data/example/commandable.rb +28 -0
- data/lib/classx.rb +35 -112
- data/lib/classx/attribute.rb +178 -0
- data/lib/classx/attributes.rb +80 -8
- data/lib/classx/commandable.rb +38 -0
- data/lib/classx/validate.rb +1 -1
- data/spec/classx/default_option_spec.rb +140 -0
- data/spec/classx/handles_spec.rb +44 -0
- data/spec/classx/with_coerce.rb +79 -0
- data/spec/classx/with_extend.rb +25 -0
- data/spec/classx/with_include.rb +25 -0
- data/spec/classx/with_multiple_class_spec.rb +23 -0
- data/spec/classx/without_accessor_spec.rb +26 -0
- data/spec/classx/without_anyoption_spec.rb +23 -0
- data/spec/classx/writable_option_spec.rb +73 -0
- data/spec/classx_attributes_spec.rb +30 -0
- data/tasks/basic_config.rake +5 -1
- metadata +22 -15
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-drb-drb_rb.html +0 -2373
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-drb-eq_rb.html +0 -626
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-forwardable_rb.html +0 -828
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-pp_rb.html +0 -1257
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-prettyprint_rb.html +0 -1506
- data/doc/output/coverage/-System-Library-Frameworks-Ruby_framework-Versions-1_8-usr-lib-ruby-1_8-timeout_rb.html +0 -715
- data/lib/classx.rb.new +0 -102
- data/spec/classx_spec.rb +0 -261
data/lib/classx.rb.new
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
|
2
|
-
class ClassX
|
3
|
-
class AttrRequiredError < Exception; end
|
4
|
-
class InvalidSetterArgument < Exception; end
|
5
|
-
class LazyOptionShouldHaveDefault < Exception; end
|
6
|
-
class <<self
|
7
|
-
def has name, attrs={ :is => :ro, :required => false }
|
8
|
-
__send__ :attr_accessor, name
|
9
|
-
|
10
|
-
setter_definition = ''
|
11
|
-
if !attrs[:respond_to].nil?
|
12
|
-
setter_definition += <<-END_OF_RESPOND_TO
|
13
|
-
raise InvalidSetterArgument, "param :#{name}'s value \#{val.inspect} should respond_to #{attrs[:respond_to]}}" unless val.respond_to? #{attrs[:respond_to]}
|
14
|
-
END_OF_RESPOND_TO
|
15
|
-
end
|
16
|
-
if !attrs[:kind_of].nil?
|
17
|
-
setter_definition += <<-END_OF_KIND_OF
|
18
|
-
raise InvalidSetterArgument, "param :#{name}'s value \#{val.inspect} should kind_of #{attrs[:kind_of]}" unless val.kind_of? #{attrs[:kind_of]}
|
19
|
-
END_OF_KIND_OF
|
20
|
-
end
|
21
|
-
|
22
|
-
if !attrs[:default].nil?
|
23
|
-
case attrs[:default]
|
24
|
-
when Proc
|
25
|
-
register_attr_default_value_proc name, &attrs[:default]
|
26
|
-
else
|
27
|
-
register_attr_default_value name, attrs[:default]
|
28
|
-
end
|
29
|
-
else
|
30
|
-
raise LazyOptionShouldHaveDefault, "in :#{name}: :lazy option need specifying :default" if attrs[:lazy]
|
31
|
-
end
|
32
|
-
|
33
|
-
if attrs[:required]
|
34
|
-
register_attr_required name
|
35
|
-
end
|
36
|
-
|
37
|
-
self.class_eval <<-END_OF_ACCESSOR
|
38
|
-
attr_reader "#{name.to_s}"
|
39
|
-
|
40
|
-
def #{name.to_s}= val
|
41
|
-
#{setter_definition}
|
42
|
-
@#{name.to_s} = val
|
43
|
-
end
|
44
|
-
END_OF_ACCESSOR
|
45
|
-
|
46
|
-
if attrs[:is] == :ro
|
47
|
-
__send__ :private,"#{name.to_s}="
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def register_attr_default_value name, value
|
52
|
-
self.class_eval do
|
53
|
-
define_method "set_attr_default_value_of[#{name.to_s}]" do
|
54
|
-
self.__send__ "#{name.to_s}=", value
|
55
|
-
end
|
56
|
-
|
57
|
-
private "set_attr_default_value_of[#{name.to_s}]"
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def register_attr_default_value_proc name, &block
|
62
|
-
self.class_eval do
|
63
|
-
define_method "set_attr_default_value_of[#{name.to_s}]" do
|
64
|
-
self.__send__ "#{name.to_s}=", block.call(self)
|
65
|
-
end
|
66
|
-
|
67
|
-
private "set_attr_default_value_of[#{name.to_s}]"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def register_attr_required name
|
72
|
-
@@attr_required ||= {}
|
73
|
-
@@attr_required[name] = true
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def initialize hash={}
|
78
|
-
before_init
|
79
|
-
@@attr_required ||= {}
|
80
|
-
@@attr_required.each do |req, is_required|
|
81
|
-
if is_required
|
82
|
-
raise AttrRequiredError, "param :#{req} is required to initialize #{self.class}" unless hash.keys.include?(req)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
hash.each do |key,val|
|
86
|
-
__send__ "#{key.to_s}=", val
|
87
|
-
end
|
88
|
-
private_methods.select do |meth|
|
89
|
-
meth.to_s =~ /^set_attr_default_value_of\[(.*?)\]$/
|
90
|
-
end.each do |meth|
|
91
|
-
__send__ meth
|
92
|
-
end
|
93
|
-
after_init
|
94
|
-
end
|
95
|
-
|
96
|
-
# just extend point
|
97
|
-
def before_init
|
98
|
-
end
|
99
|
-
|
100
|
-
def after_init
|
101
|
-
end
|
102
|
-
end
|
data/spec/classx_spec.rb
DELETED
@@ -1,261 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
-
require 'classx'
|
3
|
-
|
4
|
-
describe ClassX do
|
5
|
-
describe '#has' do
|
6
|
-
describe 'without accessor' do
|
7
|
-
before do
|
8
|
-
@class = Class.new(ClassX)
|
9
|
-
@class.class_eval do
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should not raise_error' do
|
14
|
-
lambda { @class.new }.should_not raise_error(Exception)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should raise ArgumentError when recieved nil as initialize argument' do
|
18
|
-
lambda { @class.new(nil) }.should raise_error(ArgumentError)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should raise ArgumentError when recieved not kind of Hash instance as initialize argument' do
|
22
|
-
lambda { @class.new([]) }.should raise_error(ArgumentError)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should have empty attributes' do
|
26
|
-
@class.attributes.should be_empty
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe 'without any option' do
|
31
|
-
before do
|
32
|
-
@class = Class.new(ClassX)
|
33
|
-
@class.class_eval do
|
34
|
-
has :x
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should required :x on initialize' do
|
39
|
-
lambda { @class.new }.should raise_error(ClassX::AttrRequiredError)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should define #x= private method to class' do
|
43
|
-
@class.private_instance_methods.map {|meth| meth.to_s }.should be_include("x=")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe 'with :writable option' do
|
48
|
-
describe 'when you specify false for attribute' do
|
49
|
-
before do
|
50
|
-
@class = Class.new(ClassX)
|
51
|
-
@class.class_eval do
|
52
|
-
has :x, :writable => false
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'should define #x public method to class' do
|
57
|
-
@class.instance_methods.map {|meth| meth.to_s }.should be_include('x')
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'should define #x= private method to class' do
|
61
|
-
@class.private_instance_methods.map {|meth| meth.to_s }.should be_include("x=")
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should have attributes [:x]' do
|
65
|
-
@class.attributes.should == ['x']
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe 'when you specify true for attribute' do
|
70
|
-
before do
|
71
|
-
@class = Class.new(ClassX)
|
72
|
-
@class.class_eval do
|
73
|
-
has :x, :writable => true
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'should define #x public method to class' do
|
78
|
-
@class.instance_methods.map {|meth| meth.to_s }.should be_include('x')
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'should define #x= public method to class' do
|
82
|
-
@class.public_instance_methods.map {|meth| meth.to_s }.should be_include("x=")
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'should have attributes [:x]' do
|
86
|
-
@class.attributes.should == ['x']
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'with :default option' do
|
92
|
-
describe 'when value is Proc' do
|
93
|
-
before do
|
94
|
-
@class = Class.new(ClassX)
|
95
|
-
@class.class_eval do
|
96
|
-
has :x, :default => proc { Object.new }
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'shouold have any value when instanciate' do
|
101
|
-
@class.new.x.should_not be_nil
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'should have difference of object_id between some instance' do
|
105
|
-
@class.new.x.should_not equal(@class.new.x)
|
106
|
-
end
|
107
|
-
|
108
|
-
# TODO: with lazy option
|
109
|
-
# it "can use self as Proc's argument" do
|
110
|
-
# @class.class_eval do
|
111
|
-
# has :y, :default => proc {|mine| mine.x }
|
112
|
-
# end
|
113
|
-
|
114
|
-
# @class.new.y.should equal(@class.new.x)
|
115
|
-
# end
|
116
|
-
end
|
117
|
-
|
118
|
-
describe 'when value is not Proc' do
|
119
|
-
before do
|
120
|
-
@class = Class.new(ClassX)
|
121
|
-
@class.class_eval do
|
122
|
-
has :x, :default => []
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'should have any value when instanciate' do
|
127
|
-
@class.new.x.should == []
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'should have the same object_id between some instance' do
|
131
|
-
@class.new.x.should equal(@class.new.x)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
describe 'with :optional is false' do
|
137
|
-
before do
|
138
|
-
@class = Class.new(ClassX)
|
139
|
-
@class.class_eval do
|
140
|
-
has :x, :optional => false
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
it "should raise AttrRequiredError without value" do
|
145
|
-
lambda { @class.new }.should raise_error(ClassX::AttrRequiredError)
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should not raise AttrRequiredError with value" do
|
149
|
-
lambda { @class.new(:x => Object.new) }.should_not raise_error(ClassX::AttrRequiredError)
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'should not raise AttrRequiredError with key as String' do
|
153
|
-
lambda { @class.new('x' => Object.new) }.should_not raise_error(ClassX::AttrRequiredError)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
describe ':optional is false and with :default option' do
|
158
|
-
it 'should raise ClassX::RequiredAttrShouldNotHaveDefault' do
|
159
|
-
lambda {
|
160
|
-
klass = Class.new(ClassX)
|
161
|
-
klass.class_eval do
|
162
|
-
has :x, :optional => false, :default => 1
|
163
|
-
end
|
164
|
-
}.should raise_error(ClassX::RequiredAttrShouldNotHaveDefault)
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
describe 'declare attribute without :optional and :default option' do
|
169
|
-
before do
|
170
|
-
@class = Class.new(ClassX)
|
171
|
-
@class.class_eval do
|
172
|
-
has :x, :kind_of => Integer
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
it 'should be required attribute' do
|
177
|
-
lambda { @class.new }.should raise_error(ClassX::AttrRequiredError)
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
describe ':optional is true and without :default option' do
|
182
|
-
before do
|
183
|
-
@class = Class.new(ClassX)
|
184
|
-
@class.class_eval do
|
185
|
-
has :x, :optional => true, :kind_of => Integer
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'should be required attribute' do
|
190
|
-
lambda { @class.new }.should_not raise_error(ClassX::AttrRequiredError)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
describe 'with multiple class' do
|
195
|
-
before do
|
196
|
-
@class1 = Class.new(ClassX)
|
197
|
-
@class1.class_eval do
|
198
|
-
has :x
|
199
|
-
end
|
200
|
-
@class2 = Class.new(ClassX)
|
201
|
-
@class2.class_eval do
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
it 'should not raise AttrRequiredError when initialized anothor class' do
|
206
|
-
lambda { @class2.new }.should_not raise_error(ClassX::AttrRequiredError)
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
describe 'with :optional is true' do
|
211
|
-
describe 'without :writable option' do
|
212
|
-
before do
|
213
|
-
@class = Class.new(ClassX)
|
214
|
-
@class.class_eval do
|
215
|
-
has :x, :optional => true
|
216
|
-
end
|
217
|
-
end
|
218
|
-
it 'should not raise AttrRequiredError' do
|
219
|
-
lambda { @class.new }.should_not raise_error(ClassX::AttrRequiredError)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
describe 'with :writable is false' do
|
224
|
-
it 'should raise ClassX::OptionalAttrShouldBeWritable' do
|
225
|
-
lambda {
|
226
|
-
klass = Class.new(ClassX)
|
227
|
-
klass.class_eval do
|
228
|
-
has :x, :optional => true, :writable => false
|
229
|
-
end
|
230
|
-
}.should raise_error(ClassX::OptionalAttrShouldBeWritable)
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
describe 'with :writable is true' do
|
235
|
-
it 'should raise ClassX::OptionalAttrShouldBeWritable' do
|
236
|
-
lambda {
|
237
|
-
klass = Class.new(ClassX)
|
238
|
-
klass.class_eval do
|
239
|
-
has :x, :optional => true, :writable => true
|
240
|
-
end
|
241
|
-
}.should_not raise_error(ClassX::OptionalAttrShouldBeWritable)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
describe 'not attribute param exist in #initialize argument' do
|
247
|
-
before do
|
248
|
-
@class = Class.new(ClassX)
|
249
|
-
@class.class_eval do
|
250
|
-
has :x
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
# TODO: I'll change it to be able to choosee wheather check this strictly or not.
|
255
|
-
# In many case, lack argument cause any problem. On the other hand, extra argument does not cause any problem, I think.
|
256
|
-
it 'should be ignored' do
|
257
|
-
lambda { @class.new(:x => 10, :y => 20 ) }.should_not raise_error(Exception)
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
|
-
end
|