ruby_ext 0.4.25 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/Rakefile +2 -0
  2. data/lib/rake_ext/project.rb +19 -19
  3. data/lib/rake_ext.rb +18 -18
  4. data/lib/rspec_ext/xhtml.rb +6 -6
  5. data/lib/rspec_ext.rb +40 -25
  6. data/lib/ruby_ext/core/array.rb +7 -7
  7. data/lib/ruby_ext/core/basic_object.rb +1 -1
  8. data/lib/ruby_ext/core/deep_clone.rb +2 -2
  9. data/lib/ruby_ext/core/enumerable.rb +1 -1
  10. data/lib/ruby_ext/core/hash.rb +4 -4
  11. data/lib/ruby_ext/core/module.rb +20 -30
  12. data/lib/ruby_ext/core/multiple_inheritance.rb +24 -24
  13. data/lib/ruby_ext/core/must.rb +39 -39
  14. data/lib/ruby_ext/core/object.rb +15 -3
  15. data/lib/ruby_ext/core/open_object.rb +22 -20
  16. data/lib/ruby_ext/core/string.rb +19 -19
  17. data/lib/ruby_ext/core/symbol.rb +1 -13
  18. data/lib/ruby_ext/core.rb +4 -8
  19. data/lib/ruby_ext/more/callbacks.rb +172 -0
  20. data/lib/ruby_ext/more/declarative_cache.rb +20 -22
  21. data/lib/ruby_ext/more/miscellaneous.rb +1 -46
  22. data/lib/ruby_ext/more/{observable2.rb → observable.rb} +8 -8
  23. data/lib/ruby_ext/more/open_constructor.rb +10 -10
  24. data/lib/ruby_ext/more/tuple.rb +1 -1
  25. data/lib/ruby_ext/more.rb +5 -3
  26. data/lib/ruby_ext.rb +0 -3
  27. data/lib/yaml_fix.rb +2 -2
  28. data/readme.md +51 -50
  29. data/spec/core/deep_clone_spec.rb +8 -8
  30. data/spec/core/module_spec.rb +29 -36
  31. data/spec/core/multiple_inheritance_spec.rb +32 -32
  32. data/spec/core/must_spec.rb +6 -6
  33. data/spec/core/object_spec.rb +15 -0
  34. data/spec/core/open_object_spec.rb +6 -6
  35. data/spec/more/callbacks_spec.rb +155 -0
  36. data/spec/more/declarative_cache_spec.rb +33 -33
  37. data/spec/more/{observable2_spec.rb → observable_spec.rb} +7 -7
  38. data/spec/more/open_constructor_spec.rb +5 -5
  39. metadata +7 -15
  40. data/lib/ruby_ext/core/class.rb +0 -0
  41. data/lib/ruby_ext/core/file.rb +0 -23
  42. data/lib/ruby_ext/core/kernel.rb +0 -69
  43. data/lib/ruby_ext/core/miscellaneous.rb +0 -14
  44. data/lib/ruby_ext/more/synchronize.rb +0 -26
  45. data/spec/core/kernel_spec/TheNamespace/ClassA.rb +0 -7
  46. data/spec/core/kernel_spec/another_class.rb +0 -5
  47. data/spec/core/kernel_spec/the_namespace/class_b.rb +0 -11
  48. data/spec/core/kernel_spec.rb +0 -51
  49. data/spec/more/miscellaneous_spec.rb +0 -14
  50. data/spec/more/synchronize_spec.rb +0 -79
@@ -12,7 +12,7 @@ describe "Module" do
12
12
  InheritableAccessorB
13
13
  )
14
14
  end
15
-
15
+
16
16
  it "Namespace" do
17
17
  class A
18
18
  class B
@@ -21,19 +21,19 @@ describe "Module" do
21
21
  end
22
22
  end
23
23
  end
24
-
24
+
25
25
  File.namespace.should == nil
26
26
  A::B::C.namespace.should == A::B
27
27
  end
28
-
28
+
29
29
  # class AnonymousClass
30
30
  # class << self
31
31
  # def anonymous_name
32
32
  # self.name
33
33
  # end
34
34
  # end
35
- # end
36
-
35
+ # end
36
+
37
37
  it "name" do
38
38
  class A
39
39
  class B
@@ -42,15 +42,15 @@ describe "Module" do
42
42
  end
43
43
  end
44
44
  end
45
-
45
+
46
46
  A::B::C.name.should == "A::B::C"
47
47
  end
48
-
48
+
49
49
  it "each_ancestor" do
50
50
  class X; end
51
51
  class Y < X; end
52
52
  class Z < Y; end
53
-
53
+
54
54
  list = []
55
55
  Z.each_ancestor{|a| list << a}
56
56
  list.should include Y
@@ -58,7 +58,7 @@ describe "Module" do
58
58
  list.should_not include Z
59
59
  list.should_not include Object
60
60
  list.should_not include Kernel
61
-
61
+
62
62
  list = []
63
63
  Z.each_ancestor(true){|a| list << a}
64
64
  list.should include Y
@@ -67,7 +67,7 @@ describe "Module" do
67
67
  list.should include Object
68
68
  list.should include Kernel
69
69
  end
70
-
70
+
71
71
  it "each_namespace" do
72
72
  class A
73
73
  class B
@@ -76,33 +76,26 @@ describe "Module" do
76
76
  end
77
77
  end
78
78
  end
79
-
79
+
80
80
  list = []
81
81
  A::B::C.each_namespace{|n| list << n}
82
82
  list.should == [A::B, A]
83
83
  end
84
-
84
+
85
85
  it "is?" do
86
86
  File.is?(IO).should be_true
87
87
  end
88
-
88
+
89
89
  class WrapMethod
90
90
  def method_name value
91
91
  10*value
92
92
  end
93
93
  end
94
-
95
- it "wrap_method" do
96
- WrapMethod.wrap_method :method_name do |old, value|
97
- send(old, value)*value
98
- end
99
- WrapMethod.new.method_name(10).should == 1000
100
- end
101
-
94
+
102
95
  it "escape_method" do
103
96
  Object.escape_method("><=?")
104
97
  end
105
-
98
+
106
99
  it "inheritable_accessor" do
107
100
  module InheritableAccessorForModule
108
101
  module ClassMethods
@@ -110,48 +103,48 @@ describe "Module" do
110
103
  inheritable_accessor :layout, "for_module"
111
104
  end
112
105
  end
113
-
106
+
114
107
  class InheritableAccessorForClass
115
108
  class << self
116
- inheritable_accessor :callbacks2, []
117
- inheritable_accessor :layout2, "for_class"
109
+ inheritable_accessor :callbacks2, []
110
+ inheritable_accessor :layout2, "for_class"
118
111
  end
119
- callbacks2 << :set_user
112
+ callbacks2 << :set_user
120
113
  end
121
-
114
+
122
115
  class InheritableAccessor < InheritableAccessorForClass
123
116
  inherit InheritableAccessorForModule
124
117
  callbacks << :set_model
125
- callbacks2 << :set_model
126
- self.layout2 = "anoter_layout"
127
- end
128
-
118
+ callbacks2 << :set_model
119
+ self.layout2 = "anoter_layout"
120
+ end
121
+
129
122
  InheritableAccessorForClass.callbacks2.should == [:set_user]
130
123
  InheritableAccessor.callbacks.should == [:set_user, :set_model]
131
124
  InheritableAccessor.callbacks2.should == [:set_user, :set_model]
132
-
125
+
133
126
  InheritableAccessorForClass.layout2.should == "for_class"
134
127
  InheritableAccessor.layout2.should == "anoter_layout"
135
128
  InheritableAccessor.layout.should == "for_module"
136
129
  end
137
-
130
+
138
131
  it "inheritable_accessor (from error)" do
139
132
  module InheritableAccessorBase
140
133
  module ClassMethods
141
134
  inheritable_accessor :callbacks, []
142
135
  end
143
136
  end
144
-
137
+
145
138
  class InheritableAccessorA
146
139
  inherit InheritableAccessorBase
147
140
  callbacks << :a
148
141
  end
149
-
142
+
150
143
  class InheritableAccessorB
151
144
  inherit InheritableAccessorBase
152
145
  callbacks << :b
153
146
  end
154
-
147
+
155
148
  InheritableAccessorA.callbacks.should == [:a]
156
149
  InheritableAccessorB.callbacks.should == [:b]
157
150
  end
@@ -4,7 +4,7 @@ describe "Multiple Inheritance" do
4
4
  after do
5
5
  remove_constants %w(M A M2 B AddedAfterInheritance)
6
6
  end
7
-
7
+
8
8
  it "showcase" do
9
9
  module M
10
10
  def instance_method; end
@@ -16,30 +16,30 @@ describe "Multiple Inheritance" do
16
16
  inherited do
17
17
  attr_accessor :some_accessor
18
18
  end
19
- end
20
-
19
+ end
20
+
21
21
  M.should respond_to(:class_method)
22
22
 
23
23
  class A
24
24
  inherit M
25
25
  end
26
-
26
+
27
27
  A.should respond_to(:class_method)
28
28
  A.new.should respond_to(:instance_method)
29
29
  A.new.should respond_to(:some_accessor)
30
-
31
- M.directly_included_by.should == [A].to_set
32
- end
30
+
31
+ M.directly_included_by.should == [A].to_set
32
+ end
33
33
 
34
34
  it "should inherit all ancestors class methods" do
35
35
  module M
36
36
  def instance_method; end
37
37
 
38
- class_methods do
38
+ class_methods do
39
39
  def class_method; end
40
40
  end
41
41
  end
42
-
42
+
43
43
  module M2
44
44
  inherit M
45
45
 
@@ -51,24 +51,24 @@ describe "Multiple Inheritance" do
51
51
  class B
52
52
  inherit M2
53
53
  end
54
-
54
+
55
55
  M2.should respond_to(:class_method)
56
-
56
+
57
57
  B.should respond_to(:class_method)
58
58
  B.should respond_to(:class_method2)
59
59
  B.new.should respond_to(:instance_method)
60
-
60
+
61
61
  M.directly_included_by.should == [M2].to_set
62
62
  M2.directly_included_by.should == [B].to_set
63
63
  end
64
-
64
+
65
65
  it "shouldn't redefine ancestors class methods" do
66
66
  module M
67
67
  class_methods do
68
68
  def class_method; end
69
69
  end
70
70
  end
71
-
71
+
72
72
  module M2
73
73
  inherit M
74
74
 
@@ -76,71 +76,71 @@ describe "Multiple Inheritance" do
76
76
  def class_method2; end
77
77
  end
78
78
  end
79
-
79
+
80
80
  class A
81
81
  inherit M
82
82
  end
83
-
83
+
84
84
  A.should_not respond_to(:class_method2)
85
85
  end
86
-
86
+
87
87
  it "should also allow to explicitly use ClassMethods prototype (from error)" do
88
88
  module A
89
89
  module ClassMethods
90
90
  attr_accessor :callbacks
91
91
  end
92
92
  end
93
-
93
+
94
94
  class B
95
95
  inherit A
96
96
  end
97
-
97
+
98
98
  B.should respond_to(:callbacks)
99
99
  end
100
-
100
+
101
101
  it "methods defined on base class after inheritance must be propagated to all descendants" do
102
- module M; end
103
-
102
+ module M; end
103
+
104
104
  class A
105
105
  inherit M
106
106
  end
107
-
107
+
108
108
  A.instance_methods.should_not include(:method_added_after_inheritance)
109
109
  M.send(:define_method, :method_added_after_inheritance){}
110
110
  M.instance_methods.should include(:method_added_after_inheritance)
111
111
  A.instance_methods.should include(:method_added_after_inheritance)
112
112
  end
113
-
113
+
114
114
  it "modules included in base class after inheritance must be propagated to all descendants" do
115
115
  module M; end
116
-
116
+
117
117
  class A
118
118
  inherit M
119
119
  end
120
-
120
+
121
121
  module AddedAfterInheritance
122
122
  def module_added_after_inheritance; end
123
123
  end
124
-
124
+
125
125
  M.instance_methods.should_not include(:module_added_after_inheritance)
126
126
  M.inherit AddedAfterInheritance
127
127
  M.instance_methods.should include(:module_added_after_inheritance)
128
128
  A.instance_methods.should include(:module_added_after_inheritance)
129
129
  end
130
-
130
+
131
131
  it "use case from error" do
132
132
  class ItemSpec
133
133
  end
134
-
134
+
135
135
  class PageSpec < ItemSpec
136
136
  end
137
-
137
+
138
138
  module ::ItemSpecHelper
139
139
  end
140
-
140
+
141
141
  module ::PageSpecHelper
142
142
  end
143
-
143
+
144
144
  ItemSpec.inherit ItemSpecHelper
145
145
  PageSpec.inherit PageSpecHelper
146
146
  end
@@ -13,19 +13,19 @@ describe 'Assert' do
13
13
  "".must_be.a String
14
14
  "".must_be.a String, Symbol
15
15
  1.must_be.< 2
16
-
16
+
17
17
  1.must_be.defined
18
18
  -> {nil.must_be.defined}.should raise_error(/must be defined/)
19
- end
20
-
19
+ end
20
+
21
21
  it 'must_be & must_not_be' do
22
- [].must_be.empty
22
+ [].must_be.empty
23
23
  [''].must_not_be.empty
24
-
24
+
25
25
  -> {[''].must_be.empty}.should raise_error(/must be/)
26
26
  -> {[].must_not_be.empty}.should raise_error(/must not be/)
27
27
  end
28
-
28
+
29
29
  it "should return result" do
30
30
  [].must_be.empty.should == []
31
31
  end
@@ -0,0 +1,15 @@
1
+ require "core/spec_helper"
2
+
3
+ describe 'Object' do
4
+ after(:all){remove_constants :RespondTest}
5
+
6
+ it "respond_to" do
7
+ class RespondTest
8
+ def test; 2 end
9
+ end
10
+
11
+ r = RespondTest.new
12
+ r.respond_to(:not_exist).should be_nil
13
+ r.respond_to(:test).should == 2
14
+ end
15
+ end
@@ -4,25 +4,25 @@ describe 'OpenObject' do
4
4
  it 'should be comparable with hashes' do
5
5
  {}.to_openobject.should == {}
6
6
  {}.should == {}.to_openobject
7
-
7
+
8
8
  {a: :b}.to_openobject.should == {a: :b}
9
9
  {'a' => :b}.to_openobject.should == {a: :b}
10
-
10
+
11
11
  {a: :b}.to_openobject.should == {'a' => :b}
12
12
  {'a' => :b}.to_openobject.should == {'a' => :b}
13
-
13
+
14
14
  {a: :b}.to_openobject.should_not == {a: :c}
15
15
  end
16
-
16
+
17
17
  it "must be hash (from error)" do
18
18
  {}.to_openobject.is_a?(Hash).should be_true
19
19
  end
20
-
20
+
21
21
  it 'merge! should be indifferent to string and symbol' do
22
22
  oo = OpenObject.new
23
23
  oo.merge! a: true
24
24
  oo.a.should be_true
25
- oo.merge! 'b' => true
25
+ oo.merge! 'b' => true
26
26
  oo.b.should be_true
27
27
  end
28
28
  end
@@ -0,0 +1,155 @@
1
+ require 'more/spec_helper'
2
+
3
+ describe "Callbacks" do
4
+ before :all do
5
+ class BasicCallbacksSpec
6
+ inherit RubyExt::Callbacks
7
+
8
+ set_callback :callback_name, :before, :before_callback
9
+ set_callback :callback_name, :after, :after_callback
10
+ set_callback :callback_name, :around, :around_callback
11
+
12
+ protected
13
+ def around_callback
14
+ around_callback_called
15
+ yield
16
+ end
17
+ end
18
+ end
19
+
20
+ it "basic" do
21
+ o = BasicCallbacksSpec.new
22
+ o.should_receive :before_callback
23
+ o.should_receive :around_callback_called
24
+ o.should_receive :after_callback
25
+ o.run_callbacks(:callback_name){"result"}.should == "result"
26
+ end
27
+
28
+ it "blocks" do
29
+ class BlockCallbacksSpec
30
+ inherit RubyExt::Callbacks
31
+
32
+ set_callback(:callback_name, :before){|controller| controller.result << :before}
33
+ set_callback :callback_name, :around do |controller, block|
34
+ begin
35
+ controller.result << :around_begin
36
+ block.call
37
+ ensure
38
+ controller.result << :around_end
39
+ end
40
+ end
41
+ set_callback(:callback_name, :after){|controller| controller.result << :after}
42
+
43
+ def result
44
+ @result ||= []
45
+ end
46
+ end
47
+
48
+ o = BlockCallbacksSpec.new
49
+ o.run_callbacks(:callback_name){"result"}.should == "result"
50
+ o.result.should == [:before, :around_begin, :after, :around_end]
51
+ end
52
+
53
+ it "inheritance" do
54
+ class InheritedCallbacksSpec < BasicCallbacksSpec
55
+ set_callback :callback_name, :before, :before_callback2
56
+ end
57
+
58
+ o = InheritedCallbacksSpec.new
59
+ o.should_receive :before_callback
60
+ o.should_receive :before_callback2
61
+ o.should_receive :around_callback_called
62
+ o.should_receive :after_callback
63
+ o.run_callbacks(:callback_name){"result"}.should == "result"
64
+ end
65
+
66
+ it 'terminator' do
67
+ class CallbackTerminatorSpec
68
+ inherit RubyExt::Callbacks
69
+
70
+ set_callback :callback_name, :before, :before_callback, terminator: false
71
+ set_callback :callback_name, :before, :before_callback2
72
+
73
+ def method
74
+ run_callbacks :callback_name do
75
+ "result"
76
+ end
77
+ end
78
+
79
+ protected
80
+ def before_callback
81
+ false
82
+ end
83
+ end
84
+
85
+ o = CallbackTerminatorSpec.new
86
+ o.should_not_receive :before_callback2
87
+ o.run_callbacks(:callback_name){"result"}.should_not == "result"
88
+ end
89
+
90
+ it 'conditions' do
91
+ class CallbackConditionsSpec
92
+ inherit RubyExt::Callbacks
93
+
94
+ set_callback :callback_name, :before, :before_callback, only: :another_method
95
+ end
96
+
97
+ o = CallbackConditionsSpec.new
98
+ o.should_not_receive :before_callback
99
+ o.run_callbacks(:callback_name, method: :method){"result"}.should == 'result'
100
+
101
+ o = CallbackConditionsSpec.new
102
+ o.should_receive :before_callback
103
+ o.run_callbacks(:callback_name, method: :another_method){"result"}.should == 'result'
104
+ end
105
+
106
+ it "if, unless conditions" do
107
+ c = RubyExt::Callbacks::AbstractCallback.new
108
+ c.conditions = {if: lambda{|target, inf| true}}
109
+ c.run?(nil, {}).should be_true
110
+
111
+ c.conditions = {if: lambda{|target, inf| false}}
112
+ c.run?(nil, {}).should be_false
113
+
114
+ c.conditions = {unless: lambda{|target, inf| true}}
115
+ c.run?(nil, {}).should be_false
116
+
117
+ c.conditions = {unless: lambda{|target, inf| false}}
118
+ c.run?(nil, {}).should be_true
119
+ end
120
+
121
+ it "only, except conditions" do
122
+ c = RubyExt::Callbacks::AbstractCallback.new
123
+ c.conditions = {only: :a}
124
+ c.run?(nil, {method: :a}).should be_true
125
+
126
+ c.conditions = {only: :b}
127
+ c.run?(nil, {method: :a}).should be_false
128
+
129
+ c.conditions = {except: :a}
130
+ c.run?(nil, {method: :a}).should be_false
131
+
132
+ c.conditions = {except: :b}
133
+ c.run?(nil, {method: :a}).should be_true
134
+
135
+ c.conditions = {only: :a}
136
+ c.run?(nil, {method: :a}).should be_true
137
+ end
138
+
139
+
140
+ it "around callback should be able to change result value" do
141
+ class ChangeResultValueSpec
142
+ inherit RubyExt::Callbacks
143
+
144
+ set_callback :callback_name, :around, :around_callback
145
+
146
+ def around_callback
147
+ yield
148
+ 'another result'
149
+ end
150
+ end
151
+
152
+ o = ChangeResultValueSpec.new
153
+ o.run_callbacks(:callback_name){"result"}.should == 'another result'
154
+ end
155
+ end