define_method_handler 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.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 0.0.4 Allow referencing self from condition block
2
+
3
+ Allow using of normal methods as method handlers define_method_handler(:foo, :method => :foo_impl)
4
+
5
+ method handlers with multiple groups
6
+
7
+ enable_handler_group and disable_handler_group without blocks
8
+
1
9
  0.0.3 Fixed bug on naming for anonymous methods defined on the fly
2
10
 
3
11
  0.0.2 Added missing lib dir to gem
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require "rspec/core/rake_task"
7
7
 
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.name = 'define_method_handler'
10
- s.version = '0.0.3'
10
+ s.version = '0.0.4'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
@@ -19,12 +19,19 @@ along with define_method_handler. if not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
  =end
21
21
  require "set"
22
+ class Symbol
23
+ def to_a
24
+ [self]
25
+ end
26
+ end
27
+
22
28
  class Class
23
29
  class MethodHandler
24
30
  attr_reader :processor
25
31
  attr_reader :priority
26
32
  attr_reader :second_priority
27
33
  attr_reader :group
34
+ attr_accessor :method_name
28
35
 
29
36
  def initialize(processor, sprior, group, prior = 0)
30
37
  @processor = processor
@@ -33,8 +40,23 @@ class Class
33
40
  @group = group
34
41
  end
35
42
 
36
- def execute?(*args)
37
- @condition ? @condition.call(*args) : true
43
+ def execute?(chain, *args)
44
+ if @condition
45
+ tmp_method = "tmpmethod#{rand(1000000)}#{Time.now.to_i}"
46
+ begin
47
+ local_condition = @condition
48
+ chain.class.class_eval do
49
+ define_method(tmp_method, &local_condition)
50
+ end
51
+ chain.send(tmp_method,*args)
52
+ ensure
53
+ chain.class.class_eval do
54
+ remove_method(tmp_method)
55
+ end
56
+ end
57
+ else
58
+ true
59
+ end
38
60
  end
39
61
 
40
62
  def condition(&blk)
@@ -44,21 +66,41 @@ class Class
44
66
  end
45
67
 
46
68
  module ChainMethods
47
- def enable_handler_group(groupname)
69
+ def disabled_handler_groups
48
70
  @disabled_handler_groups ||= Set.new
49
- group_included = @disabled_handler_groups.include? groupname
50
- @disabled_handler_groups.delete(groupname)
51
- yield
52
- ensure
53
- @disabled_handler_groups << groupname if group_included
71
+ end
72
+
73
+ def disabled_handler_groups=(a)
74
+ @disabled_handler_groups=a
75
+ end
76
+
77
+ def enable_handler_group(groupname)
78
+ group_included = false
79
+ if block_given?
80
+ old_groups = disabled_handler_groups.dup
81
+ begin
82
+ enable_handler_group(groupname)
83
+ yield
84
+ ensure
85
+ self.disabled_handler_groups = old_groups
86
+ end
87
+ else
88
+ disabled_handler_groups.delete(groupname)
89
+ end
54
90
  end
55
91
 
56
92
  def disable_handler_group(groupname)
57
- @disabled_handler_groups ||= Set.new
58
- @disabled_handler_groups << groupname
59
- yield
60
- ensure
61
- @disabled_handler_groups.delete groupname
93
+ if block_given?
94
+ old_groups = disabled_handler_groups.dup
95
+ begin
96
+ disable_handler_group(groupname)
97
+ yield
98
+ ensure
99
+ self.disabled_handler_groups = old_groups
100
+ end
101
+ else
102
+ disabled_handler_groups << groupname
103
+ end
62
104
  end
63
105
  end
64
106
 
@@ -67,8 +109,11 @@ class Class
67
109
  end
68
110
 
69
111
  def handler_scope(options)
70
- old_options = @method_handler_options
112
+ old_options = @method_handler_options||{}
113
+ old_group = old_options[:group]
71
114
  @method_handler_options = (@method_handler_options || {}).merge(options)
115
+ @method_handler_options[:group] = (@method_handler_options[:group].to_a + old_group.to_a).uniq
116
+
72
117
  yield
73
118
  ensure
74
119
  @method_handler_options = old_options
@@ -81,7 +126,9 @@ class Class
81
126
  @method_handlers ||= Array.new
82
127
  @next_priority = (@next_priority || 0) + 1
83
128
 
84
- mh = MethodHandler.new(blk, @next_priority, options[:group] || :default, options[:priority] || 0)
129
+ mh = MethodHandler.new(blk, @next_priority, (options[:group].to_a + :default.to_a), options[:priority] || 0)
130
+ mh.method_name = options[:method]
131
+
85
132
  @method_handlers << mh
86
133
 
87
134
  include ChainMethods
@@ -89,24 +136,34 @@ class Class
89
136
  @method_handlers.sort!{|x,y|
90
137
  if x.priority == y.priority
91
138
  x.second_priority <=> y.second_priority
92
- else
139
+ else
93
140
  x.priority <=> y.priority
94
141
  end
95
142
  }
96
143
 
97
144
  define_method(mname) do |*x, &callblk|
98
- self.class.method_handlers.reject{|mhh| (@disabled_handler_groups||[]).include? mhh.group }.reverse_each do |mhh|
99
- if mhh.execute?(*x)
145
+ self.class.method_handlers.reject{|mhh|
146
+ mhh.group.count { |gr|
147
+ (@disabled_handler_groups||[]).include? gr
148
+ } > 0 }.reverse_each do |mhh|
149
+
150
+ if mhh.execute?(self,*x)
100
151
  tmp_method = "tmpmethod#{rand(1000000)}#{Time.now.to_i}"
101
152
 
102
153
  begin
103
- self.class.class_eval do
104
- define_method(tmp_method, &mhh.processor)
154
+ if mhh.method_name
155
+ return send(mhh.method_name, *x, &callblk)
156
+ else
157
+ self.class.class_eval do
158
+ define_method(tmp_method, &mhh.processor)
159
+ end
160
+ return method(tmp_method).call(*x, &callblk)
105
161
  end
106
- return method(tmp_method).call(*x, &callblk)
107
162
  ensure
108
- self.class.class_eval do
109
- remove_method(tmp_method)
163
+ unless mhh.method_name
164
+ self.class.class_eval do
165
+ remove_method(tmp_method)
166
+ end
110
167
  end
111
168
  end
112
169
  end
data/spec/base_spec.rb CHANGED
@@ -49,5 +49,18 @@ describe "define_method_handler" do
49
49
  chain4 = CHAIN4.new
50
50
  chain4.fact(5).should be == 120
51
51
  end
52
+
53
+ it "should accept normal methods as handlers" do
54
+ class CHAIN5
55
+ define_method_handler(:foo, :method => :foo_impl)
56
+
57
+ def foo_impl
58
+ 100
59
+ end
60
+ end
61
+
62
+ chain5 = CHAIN5.new
63
+ chain5.foo.should be == 100
64
+ end
52
65
 
53
66
  end
data/spec/group_spec.rb CHANGED
@@ -87,5 +87,65 @@ describe "define_method_handler" do
87
87
 
88
88
  ret.should be == 10
89
89
  end
90
+
91
+
92
+ it "one method handler defined with group should not run after disable_handler_group" do
93
+ class CHAIN2_1
94
+ define_method_handler(:foo, :group => :testgroup) {
95
+ 10
96
+ }
97
+ end
98
+
99
+ chain = CHAIN2_1.new
100
+ chain.disable_handler_group(:testgroup)
101
+ chain.foo.should be == nil
102
+ end
103
+
104
+ it "one method handler defined with group should run after enable_handler_group" do
105
+ class CHAIN2_1
106
+ define_method_handler(:foo, :group => :testgroup) {
107
+ 10
108
+ }
109
+ end
110
+
111
+ chain = CHAIN2_1.new
112
+ chain.disable_handler_group(:testgroup)
113
+ chain.enable_handler_group(:testgroup)
114
+ chain.foo.should be == 10
115
+ end
116
+
117
+ it "one method handler defined with group should not be enabled after disable_handler_group block is closed inside another disable_handler_group block of the same group" do
118
+ class CHAIN2_1
119
+ define_method_handler(:foo, :group => :testgroup) {
120
+ 10
121
+ }
122
+ end
123
+
124
+ chain = CHAIN2_1.new
125
+
126
+ ret = nil
127
+ chain.disable_handler_group(:testgroup) do
128
+ chain.disable_handler_group(:testgroup) do
129
+ end
130
+
131
+ ret = chain.foo
132
+ end
90
133
 
134
+ ret.should be == nil
135
+ end
136
+
137
+ it "one method handler defined with group should enabled after enable_handler_group block is closed" do
138
+ class CHAIN2_1
139
+ define_method_handler(:foo, :group => :testgroup) {
140
+ 10
141
+ }
142
+ end
143
+
144
+ chain = CHAIN2_1.new
145
+
146
+ chain.enable_handler_group(:testgroup) do
147
+ end
148
+
149
+ chain.foo.should be == 10
150
+ end
91
151
  end
@@ -79,4 +79,31 @@ describe "define_method_handler" do
79
79
  chain = CHAIN4_4.new
80
80
  chain.foo.should be == 100
81
81
  end
82
+
83
+
84
+ it "nested handler_scopes with groups should define multiple groups for handler" do
85
+ class CHAIN4_5
86
+ handler_scope(:group => :a) do
87
+ handler_scope(:group => :b) do
88
+ define_method_handler(:foo) do
89
+ 99
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ chain = CHAIN4_5.new
96
+ chain.disable_handler_group(:a) do
97
+ chain.foo.should be == nil # nil
98
+ end
99
+
100
+ chain.disable_handler_group(:b) do
101
+ chain.foo.should be == nil # nil
102
+ end
103
+
104
+ chain.disable_handler_group(:default) do
105
+ chain.foo.should be == nil # nil
106
+ end
107
+ end
108
+
82
109
  end
data/spec/scope_spec.rb CHANGED
@@ -41,4 +41,18 @@ private
41
41
  chain = CHAIN3_3.new
42
42
  chain.foo.should be == 92
43
43
  end
44
+
45
+
46
+ it "should allow referencing self from condition" do
47
+ class CHAIN3_4
48
+ def bar
49
+ 100
50
+ end
51
+
52
+ define_method_handler(:foo) {92}.condition{self.bar == 100}
53
+ end
54
+ chain = CHAIN3_4.new
55
+ chain.foo.should be == 92
56
+ end
57
+
44
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: define_method_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -20,7 +20,6 @@ extra_rdoc_files:
20
20
  - README
21
21
  files:
22
22
  - lib/define_method_handler.rb
23
- - lib/define_method_handler.rb~
24
23
  - spec/priority_spec.rb
25
24
  - spec/base_spec.rb~
26
25
  - spec/base_spec.rb
@@ -1,120 +0,0 @@
1
- =begin
2
-
3
- This file is part of the define_method_handler project, http://github.com/tario/define_method_handler
4
-
5
- Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
-
7
- define_method_handler is free software: you can redistribute it and/or modify
8
- it under the terms of the gnu general public license as published by
9
- the free software foundation, either version 3 of the license, or
10
- (at your option) any later version.
11
-
12
- define_method_handler is distributed in the hope that it will be useful,
13
- but without any warranty; without even the implied warranty of
14
- merchantability or fitness for a particular purpose. see the
15
- gnu general public license for more details.
16
-
17
- you should have received a copy of the gnu general public license
18
- along with define_method_handler. if not, see <http://www.gnu.org/licenses/>.
19
-
20
- =end
21
- require "set"
22
- class Class
23
- class MethodHandler
24
- attr_reader :processor
25
- attr_reader :priority
26
- attr_reader :second_priority
27
- attr_reader :group
28
-
29
- def initialize(processor, sprior, group, prior = 0)
30
- @processor = processor
31
- @priority = prior
32
- @second_priority = sprior
33
- @group = group
34
- end
35
-
36
- def execute?(*args)
37
- @condition ? @condition.call(*args) : true
38
- end
39
-
40
- def condition(&blk)
41
- @condition = blk
42
- self
43
- end
44
- end
45
-
46
- module ChainMethods
47
- def enable_handler_group(groupname)
48
- @disabled_handler_groups ||= Set.new
49
- group_included = @disabled_handler_groups.include? groupname
50
- @disabled_handler_groups.delete(groupname)
51
- yield
52
- ensure
53
- @disabled_handler_groups << groupname if group_included
54
- end
55
-
56
- def disable_handler_group(groupname)
57
- @disabled_handler_groups ||= Set.new
58
- @disabled_handler_groups << groupname
59
- yield
60
- ensure
61
- @disabled_handler_groups.delete groupname
62
- end
63
- end
64
-
65
- def method_handlers
66
- @method_handlers
67
- end
68
-
69
- def handler_scope(options)
70
- old_options = @method_handler_options
71
- @method_handler_options = (@method_handler_options || {}).merge(options)
72
- yield
73
- ensure
74
- @method_handler_options = old_options
75
- end
76
-
77
- def define_method_handler(mname, *options, &blk)
78
- options = options.inject(&:merge) || {}
79
- options.merge!(@method_handler_options) if @method_handler_options
80
-
81
- @method_handlers ||= Array.new
82
- @next_priority = (@next_priority || 0) + 1
83
-
84
- mh = MethodHandler.new(blk, @next_priority, options[:group] || :default, options[:priority] || 0)
85
- @method_handlers << mh
86
-
87
- include ChainMethods
88
-
89
- @method_handlers.sort!{|x,y|
90
- if x.priority == y.priority
91
- x.second_priority <=> y.second_priority
92
- else
93
- x.priority <=> y.priority
94
- end
95
- }
96
-
97
- define_method(mname) do |*x, &callblk|
98
- self.class.method_handlers.reject{|mhh| (@disabled_handler_groups||[]).include? mhh.group }.reverse_each do |mhh|
99
- if mhh.execute?(*x)
100
- tmp_method = 'tmpmethod#{rand(10000000000000)}'
101
-
102
- begin
103
- self.class.class_eval do
104
- define_method(tmp_method, &mhh.processor)
105
- end
106
- return method(tmp_method).call(*x, &callblk)
107
- ensure
108
- self.class.class_eval do
109
- remove_method(tmp_method)
110
- end
111
- end
112
- end
113
- end
114
-
115
- nil
116
- end
117
-
118
- mh
119
- end
120
- end