ruby_ext 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/Rakefile +2 -3
  2. data/lib/rake_ext/project.rb +1 -2
  3. data/lib/rspec_ext.rb +37 -35
  4. data/lib/rspec_ext/nokogiri.rb +26 -0
  5. data/lib/ruby_ext/core.rb +3 -5
  6. data/lib/ruby_ext/core/array.rb +1 -1
  7. data/lib/ruby_ext/core/basic_object.rb +1 -1
  8. data/lib/ruby_ext/core/deep_clone.rb +3 -10
  9. data/lib/ruby_ext/core/enumerable.rb +3 -3
  10. data/lib/ruby_ext/core/false_class.rb +1 -1
  11. data/lib/ruby_ext/core/hash.rb +23 -7
  12. data/lib/ruby_ext/core/module.rb +37 -57
  13. data/lib/ruby_ext/core/multiple_inheritance.rb +3 -3
  14. data/lib/ruby_ext/core/must.rb +0 -16
  15. data/lib/ruby_ext/core/nil_class.rb +5 -0
  16. data/lib/ruby_ext/core/object.rb +7 -7
  17. data/lib/ruby_ext/core/open_object.rb +7 -25
  18. data/lib/ruby_ext/core/string.rb +1 -6
  19. data/lib/ruby_ext/more.rb +2 -0
  20. data/lib/ruby_ext/more/callbacks.rb +37 -0
  21. data/lib/ruby_ext/more/declarative_cache.rb +45 -56
  22. data/lib/yaml_fix.rb +9 -0
  23. data/spec/core/array_spec.rb +1 -1
  24. data/spec/core/deep_clone_spec.rb +4 -2
  25. data/spec/core/enumerable.rb +1 -1
  26. data/spec/core/module_spec.rb +65 -107
  27. data/spec/core/multiple_inheritance_spec.rb +4 -4
  28. data/spec/core/must_spec.rb +1 -1
  29. data/spec/core/object_spec.rb +6 -6
  30. data/spec/core/open_object_spec.rb +1 -1
  31. data/spec/more/callbacks_spec.rb +90 -20
  32. data/spec/more/declarative_cache_spec.rb +96 -75
  33. data/spec/more/observable_spec.rb +10 -34
  34. data/spec/more/open_constructor_spec.rb +14 -11
  35. data/spec/spec_helper.rb +2 -0
  36. metadata +5 -6
  37. data/lib/rspec_ext/xhtml.rb +0 -48
  38. data/lib/ruby_ext/fixes.rb +0 -6
  39. data/spec/core/spec_helper.rb +0 -2
  40. data/spec/more/spec_helper.rb +0 -2
@@ -1,4 +1,4 @@
1
- require "core/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe "Multiple Inheritance" do
4
4
  after do
@@ -28,7 +28,7 @@ describe "Multiple Inheritance" do
28
28
  A.new.should respond_to(:instance_method)
29
29
  A.new.should respond_to(:some_accessor)
30
30
 
31
- M.directly_included_by.should == [A].to_set
31
+ M.directly_included_by.should == {A => true}
32
32
  end
33
33
 
34
34
  it "should inherit all ancestors class methods" do
@@ -58,8 +58,8 @@ describe "Multiple Inheritance" do
58
58
  B.should respond_to(:class_method2)
59
59
  B.new.should respond_to(:instance_method)
60
60
 
61
- M.directly_included_by.should == [M2].to_set
62
- M2.directly_included_by.should == [B].to_set
61
+ M.directly_included_by.should == {M2 => true}
62
+ M2.directly_included_by.should == {B => true}
63
63
  end
64
64
 
65
65
  it "shouldn't redefine ancestors class methods" do
@@ -1,4 +1,4 @@
1
- require "core/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe 'Assert' do
4
4
  it 'must & must_not' do
@@ -1,15 +1,15 @@
1
- require "core/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe 'Object' do
4
- after(:all){remove_constants :RespondTest}
4
+ after{remove_constants :Tmp}
5
5
 
6
6
  it "respond_to" do
7
- class RespondTest
7
+ class Tmp
8
8
  def test; 2 end
9
9
  end
10
10
 
11
- r = RespondTest.new
12
- r.respond_to(:not_exist).should be_nil
13
- r.respond_to(:test).should == 2
11
+ o = Tmp.new
12
+ o.respond_to(:not_exist).should be_nil
13
+ o.respond_to(:test).should == 2
14
14
  end
15
15
  end
@@ -1,4 +1,4 @@
1
- require "core/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe 'OpenObject' do
4
4
  it 'should be comparable with hashes' do
@@ -1,9 +1,8 @@
1
- require 'more/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe "Callbacks" do
4
- before :all do
5
- module CallbacksSpec; end
6
- class CallbacksSpec::Basic
4
+ before do
5
+ class ABase
7
6
  inherit RubyExt::Callbacks
8
7
 
9
8
  set_callback :save, :before, :before_save
@@ -17,10 +16,10 @@ describe "Callbacks" do
17
16
  end
18
17
  end
19
18
  end
20
- after(:all){remove_constants :CallbacksSpec}
19
+ after{remove_constants :ABase, :SomeClass, :AnotherClass}
21
20
 
22
21
  it "basic" do
23
- o = CallbacksSpec::Basic.new
22
+ o = ABase.new
24
23
  o.should_receive :before_save
25
24
  o.should_receive :around_save_called
26
25
  o.should_receive :after_save
@@ -28,7 +27,7 @@ describe "Callbacks" do
28
27
  end
29
28
 
30
29
  it "should be possible to call before & after separatelly (in this mode :around is not available)" do
31
- o = CallbacksSpec::Basic.new
30
+ o = ABase.new
32
31
  o.should_receive :before_save
33
32
  o.run_before_callbacks(:save)
34
33
  o.should_receive :after_save
@@ -36,7 +35,7 @@ describe "Callbacks" do
36
35
  end
37
36
 
38
37
  it "blocks" do
39
- class CallbacksSpec::Blocks
38
+ class SomeClass
40
39
  inherit RubyExt::Callbacks
41
40
 
42
41
  set_callback(:save, :before){|controller| controller.result << :before}
@@ -55,13 +54,13 @@ describe "Callbacks" do
55
54
  end
56
55
  end
57
56
 
58
- o = CallbacksSpec::Blocks.new
57
+ o = SomeClass.new
59
58
  o.run_callbacks(:save){"result"}.should == "result"
60
59
  o.result.should == [:before, :around_begin, :after, :around_end]
61
60
  end
62
61
 
63
62
  it "execution order" do
64
- class CallbacksSpec::ExecutionOrder
63
+ class SomeClass
65
64
  inherit RubyExt::Callbacks
66
65
 
67
66
  # order is important, don't change it
@@ -77,7 +76,7 @@ describe "Callbacks" do
77
76
  end
78
77
  end
79
78
 
80
- o = CallbacksSpec::ExecutionOrder.new
79
+ o = SomeClass.new
81
80
  o.should_receive(:before1).ordered.once
82
81
  o.should_receive(:around1_called).ordered.once
83
82
  o.should_receive(:before2).ordered.once
@@ -86,11 +85,11 @@ describe "Callbacks" do
86
85
  end
87
86
 
88
87
  it "inheritance" do
89
- class CallbacksSpec::Inheritance < CallbacksSpec::Basic
88
+ class SomeClass < ABase
90
89
  set_callback :save, :before, :before_save2
91
90
  end
92
91
 
93
- o = CallbacksSpec::Inheritance.new
92
+ o = SomeClass.new
94
93
  o.should_receive :before_save
95
94
  o.should_receive :before_save2
96
95
  o.should_receive :around_save_called
@@ -99,7 +98,7 @@ describe "Callbacks" do
99
98
  end
100
99
 
101
100
  it 'terminator' do
102
- class CallbacksSpec::Terminator
101
+ class SomeClass
103
102
  inherit RubyExt::Callbacks
104
103
 
105
104
  set_callback :save, :before, :before_save, terminator: false
@@ -117,23 +116,23 @@ describe "Callbacks" do
117
116
  end
118
117
  end
119
118
 
120
- o = CallbacksSpec::Terminator.new
119
+ o = SomeClass.new
121
120
  o.should_not_receive :before_save2
122
121
  o.run_callbacks(:save){"result"}.should_not == "result"
123
122
  end
124
123
 
125
124
  it 'conditions' do
126
- class CallbacksSpec::Conditions
125
+ class SomeClass
127
126
  inherit RubyExt::Callbacks
128
127
 
129
128
  set_callback :save, :before, :before_save, only: :another_method
130
129
  end
131
130
 
132
- o = CallbacksSpec::Conditions.new
131
+ o = SomeClass.new
133
132
  o.should_not_receive :before_save
134
133
  o.run_callbacks(:save, method: :method){"result"}.should == 'result'
135
134
 
136
- o = CallbacksSpec::Conditions.new
135
+ o = SomeClass.new
137
136
  o.should_receive :before_save
138
137
  o.run_callbacks(:save, method: :another_method){"result"}.should == 'result'
139
138
  end
@@ -173,7 +172,7 @@ describe "Callbacks" do
173
172
 
174
173
 
175
174
  it "around callback should be able to change result value" do
176
- class CallbacksSpec::ChangeResult
175
+ class SomeClass
177
176
  inherit RubyExt::Callbacks
178
177
 
179
178
  set_callback :save, :around, :around_save
@@ -184,7 +183,78 @@ describe "Callbacks" do
184
183
  end
185
184
  end
186
185
 
187
- o = CallbacksSpec::ChangeResult.new
186
+ o = SomeClass.new
188
187
  o.run_callbacks(:save){"result"}.should == 'another result'
189
188
  end
189
+
190
+ describe "wrapping methods in callbacks" do
191
+ it "wrap_method_with_callbacks" do
192
+ class SomeClass
193
+ inherit RubyExt::Callbacks
194
+
195
+ set_callback :save, :before, :before_save
196
+
197
+ def update
198
+ 'ok'
199
+ end
200
+
201
+ wrap_method_with_callbacks :update, :save
202
+ end
203
+
204
+ o = SomeClass.new
205
+ o.should_receive(:before_save).once
206
+ o.update.should == 'ok'
207
+ end
208
+
209
+ it "wrap_with_callbacks" do
210
+ class SomeClass
211
+ inherit RubyExt::Callbacks
212
+ def update; end
213
+
214
+ should_receive(:wrap_method_with_callbacks).with(:update, :save)
215
+ wrap_with_callback :save
216
+ end
217
+ end
218
+
219
+ it "callbacks for method with super call should be called only once" do
220
+ class SomeClass
221
+ inherit RubyExt::Callbacks
222
+
223
+ set_callback :save, :before, :before_save
224
+
225
+ def update
226
+ 'ok'
227
+ end
228
+ wrap_method_with_callbacks :update, :save
229
+ end
230
+
231
+ class AnotherClass < SomeClass
232
+ def update
233
+ super
234
+ 'ok2'
235
+ end
236
+ wrap_method_with_callbacks :update, :save
237
+ end
238
+
239
+ o = AnotherClass.new
240
+ o.should_receive(:before_save).once
241
+ o.update.should == 'ok2'
242
+ end
243
+
244
+ it "run_callbacks_only_once" do
245
+ class SomeClass
246
+ inherit RubyExt::Callbacks
247
+
248
+ set_callback :save, :before, :before_save
249
+ end
250
+
251
+ o = SomeClass.new
252
+ o.should_receive(:before_save).once
253
+ (o.run_callbacks_only_once :save do
254
+ o.run_callbacks_only_once :save do
255
+ 'result'
256
+ end
257
+ end).should == 'result'
258
+ end
259
+ end
190
260
  end
@@ -1,44 +1,58 @@
1
- require "more/spec_helper"
1
+ require "spec_helper"
2
2
 
3
3
  describe 'DeclarativeCache' do
4
- class CachedClass
5
- attr_accessor :value
6
- def value_get; @value end
7
- cache_method :value_get
8
-
9
- attr_accessor :value2
10
- def value2_get; @value2 end
11
-
12
- attr_accessor :params
13
- def params_get param; @params[param] end
14
-
15
- attr_accessor :multiplier
16
- def *(arg)
17
- @multiplier * arg
4
+ after{remove_constants :Tmp, :TmpModule}
5
+
6
+ # class CachedClass
7
+ # attr_accessor :value
8
+ # def value_get; @value end
9
+ # cache_method :value_get
10
+ #
11
+ # attr_accessor :value2
12
+ # def value2_get; @value2 end
13
+ #
14
+ # attr_accessor :params
15
+ # def params_get param; @params[param] end
16
+ #
17
+ # attr_accessor :multiplier
18
+ # def *(arg)
19
+ # @multiplier * arg
20
+ # end
21
+ # cache_method_with_params '*'
22
+ # end
23
+ #
24
+ # RubyExt::DeclarativeCache.cache_method CachedClass, :value2_get
25
+ # RubyExt::DeclarativeCache.cache_method_with_params CachedClass, :params_get
26
+
27
+ it "basics" do
28
+ class Tmp
29
+ attr_accessor :value
30
+ def value_get; @value end
31
+ cache_method :value_get
18
32
  end
19
- cache_method_with_params '*'
20
- end
21
-
22
- RubyExt::DeclarativeCache.cache_method CachedClass, :value2_get
23
- RubyExt::DeclarativeCache.cache_method_with_params CachedClass, :params_get
24
33
 
25
- it "Simple Cache" do
26
- o = CachedClass.new
34
+ o = Tmp.new
27
35
  o.value = 0
28
- o.value2 = 0
29
36
  o.value_get.should == 0
30
- o.value2_get.should == 0
31
37
 
32
38
  o.value = 1
33
- o.value2 = 1
34
39
  o.value_get.should == 0
35
- o.value2_get.should == 0
36
40
  end
37
41
 
38
- it "should define <method>_with_cache and <method>_without_cache methods" do
39
- o = CachedClass.new
42
+ it "should define {method}_with_cache and {method}_without_cache methods" do
43
+ class Tmp
44
+ attr_accessor :value
45
+ def value_get; @value end
46
+ cache_method :value_get
47
+
48
+ attr_accessor :params
49
+ def params_get param; @params[param] end
50
+ cache_method_with_params :params_get
51
+ end
52
+
53
+ o = Tmp.new
40
54
 
41
- # without params
55
+ # Without params.
42
56
  o.value = 0
43
57
  o.value_get.should == 0
44
58
  o.value = 1
@@ -46,7 +60,7 @@ describe 'DeclarativeCache' do
46
60
  o.value_get_with_cache.should == 0
47
61
  o.value_get_without_cache.should == 1
48
62
 
49
- # with params
63
+ # With params.
50
64
  o.params = {a: :b}
51
65
  o.params_get(:a).should == :b
52
66
  o.params = {a: :c}
@@ -56,7 +70,13 @@ describe 'DeclarativeCache' do
56
70
  end
57
71
 
58
72
  it "clear_cache" do
59
- o = CachedClass.new
73
+ class Tmp
74
+ attr_accessor :value
75
+ def value_get; @value end
76
+ cache_method :value_get
77
+ end
78
+
79
+ o = Tmp.new
60
80
  o.value = 0
61
81
  o.value_get.should == 0
62
82
 
@@ -67,13 +87,24 @@ describe 'DeclarativeCache' do
67
87
  o.value_get.should == 1
68
88
  end
69
89
 
70
- it "should check for parameters" do
71
- o = CachedClass.new
90
+ it "should check for method signature" do
91
+ class Tmp
92
+ def value_get; @value end
93
+ cache_method :value_get
94
+ end
95
+
96
+ o = Tmp.new
72
97
  lambda{o.value_get(1)}.should raise_error(/cache_method_with_params/)
73
98
  end
74
99
 
75
- it "Cache With Params" do
76
- o = CachedClass.new
100
+ it "cache_method_with_params" do
101
+ class Tmp
102
+ attr_accessor :params
103
+ def params_get param; @params[param] end
104
+ cache_method_with_params :params_get
105
+ end
106
+
107
+ o = Tmp.new
77
108
  o.params = {a: :b}
78
109
  o.params_get(:a).should == :b
79
110
  o.params = {a: :c}
@@ -81,62 +112,52 @@ describe 'DeclarativeCache' do
81
112
  end
82
113
 
83
114
  it "should works with operators" do
84
- o = CachedClass.new
115
+ class Tmp
116
+ attr_accessor :multiplier
117
+ def *(arg)
118
+ @multiplier * arg
119
+ end
120
+ cache_method_with_params '*'
121
+ end
122
+
123
+ o = Tmp.new
85
124
  o.multiplier = 2
86
125
  (o * 2).should == 4
87
126
  o.multiplier = 3
88
127
  (o * 2).should == 4
89
128
  end
90
129
 
91
- class CachedClass2
92
- class << self
93
- attr_accessor :value
94
- def value_get; @value end
95
- cache_method :value_get
130
+ it "should works with singleton classes" do
131
+ class Tmp
132
+ class << self
133
+ attr_accessor :value
134
+ def value_get; @value end
135
+ cache_method :value_get
136
+ end
96
137
  end
97
- end
98
138
 
99
- it "Simple Cache" do
100
- CachedClass2.value = 0
101
- CachedClass2.value_get.should == 0
102
- CachedClass2.value = 1
103
- CachedClass2.value_get.should == 0
139
+ Tmp.value = 0
140
+ Tmp.value_get.should == 0
141
+ Tmp.value = 1
142
+ Tmp.value_get.should == 0
104
143
  end
105
144
 
106
- module CachedModule
107
- attr_accessor :value
108
- def value_get; @value end
109
- cache_method :value_get
110
- end
145
+ it "should works with included modules (from error)" do
146
+ module TmpModule
147
+ attr_accessor :value
148
+ def value_get; @value end
149
+ cache_method :value_get
150
+ end
111
151
 
112
- class CachedClass3
113
- include CachedModule
114
- end
152
+ class Tmp
153
+ include TmpModule
154
+ end
115
155
 
116
- it "should also works with modules (from error)" do
117
- o = CachedClass.new
156
+ o = Tmp.new
118
157
  o.value = 0
119
158
  o.value_get.should == 0
120
159
 
121
160
  o.value = 1
122
161
  o.value_get.should == 0
123
162
  end
124
-
125
- class CachedClass4
126
- attr_accessor :value
127
- def value_get; @value end
128
- end
129
-
130
- it "should not cache twice, and should works" do
131
- CachedClass4.cache_method :value_get
132
-
133
- Object.should_receive(:warn){|msg| msg =~ /twice/}
134
- CachedClass4.cache_method :value_get
135
-
136
- o = CachedClass.new
137
- o.value = 0
138
- o.value_get.should == 0
139
- o.value = 1
140
- o.value_get.should == 0
141
- end
142
163
  end