define_method_handler 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +4 -0
  2. data/Rakefile +1 -1
  3. data/lib/define_method_handler.rb +32 -24
  4. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.0.5 added error handling on remove_method
2
+
3
+ refactor: changed state variables of chain to changes to allow keeping state of multiple methods by nam
4
+
1
5
  0.0.4 Allow referencing self from condition block
2
6
 
3
7
  Allow using of normal methods as method handlers define_method_handler(:foo, :method => :foo_impl)
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.4'
10
+ s.version = '0.0.5'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
@@ -50,8 +50,11 @@ class Class
50
50
  end
51
51
  chain.send(tmp_method,*args)
52
52
  ensure
53
- chain.class.class_eval do
54
- remove_method(tmp_method)
53
+ begin
54
+ chain.class.class_eval do
55
+ remove_method(tmp_method)
56
+ end
57
+ rescue NameError
55
58
  end
56
59
  end
57
60
  else
@@ -67,39 +70,37 @@ class Class
67
70
 
68
71
  module ChainMethods
69
72
  def disabled_handler_groups
70
- @disabled_handler_groups ||= Set.new
71
- end
72
-
73
- def disabled_handler_groups=(a)
74
- @disabled_handler_groups=a
73
+ @disabled_handler_groups ||= Hash.new
75
74
  end
76
75
 
77
- def enable_handler_group(groupname)
76
+ def enable_handler_group(groupname, mname = self.class.method_handlers.keys.first)
78
77
  group_included = false
79
78
  if block_given?
80
- old_groups = disabled_handler_groups.dup
79
+ old_groups = (disabled_handler_groups[mname]||Set.new).dup
81
80
  begin
82
- enable_handler_group(groupname)
81
+ enable_handler_group(groupname,mname)
83
82
  yield
84
83
  ensure
85
- self.disabled_handler_groups = old_groups
84
+ self.disabled_handler_groups[mname] = old_groups
86
85
  end
87
86
  else
88
- disabled_handler_groups.delete(groupname)
87
+ disabled_handler_groups[mname] ||= Set.new
88
+ disabled_handler_groups[mname].delete(groupname)
89
89
  end
90
90
  end
91
91
 
92
- def disable_handler_group(groupname)
92
+ def disable_handler_group(groupname, mname = self.class.method_handlers.keys.first)
93
93
  if block_given?
94
- old_groups = disabled_handler_groups.dup
94
+ old_groups = (disabled_handler_groups[mname]||Set.new).dup
95
95
  begin
96
- disable_handler_group(groupname)
96
+ disable_handler_group(groupname,mname)
97
97
  yield
98
98
  ensure
99
- self.disabled_handler_groups = old_groups
99
+ self.disabled_handler_groups[mname] = old_groups
100
100
  end
101
101
  else
102
- disabled_handler_groups << groupname
102
+ disabled_handler_groups[mname] ||= Set.new
103
+ disabled_handler_groups[mname] << groupname
103
104
  end
104
105
  end
105
106
  end
@@ -123,17 +124,18 @@ class Class
123
124
  options = options.inject(&:merge) || {}
124
125
  options.merge!(@method_handler_options) if @method_handler_options
125
126
 
126
- @method_handlers ||= Array.new
127
+ @method_handlers ||= Hash.new
128
+ @method_handlers[mname] ||= Array.new
127
129
  @next_priority = (@next_priority || 0) + 1
128
130
 
129
131
  mh = MethodHandler.new(blk, @next_priority, (options[:group].to_a + :default.to_a), options[:priority] || 0)
130
132
  mh.method_name = options[:method]
131
133
 
132
- @method_handlers << mh
134
+ @method_handlers[mname] << mh
133
135
 
134
136
  include ChainMethods
135
137
 
136
- @method_handlers.sort!{|x,y|
138
+ @method_handlers[mname].sort!{|x,y|
137
139
  if x.priority == y.priority
138
140
  x.second_priority <=> y.second_priority
139
141
  else
@@ -142,9 +144,12 @@ class Class
142
144
  }
143
145
 
144
146
  define_method(mname) do |*x, &callblk|
145
- self.class.method_handlers.reject{|mhh|
147
+ self.class.method_handlers[mname].reject{|mhh|
146
148
  mhh.group.count { |gr|
147
- (@disabled_handler_groups||[]).include? gr
149
+ @disabled_handler_groups ||= Hash.new
150
+ @disabled_handler_groups[mname]||= Set.new
151
+
152
+ @disabled_handler_groups[mname].include? gr
148
153
  } > 0 }.reverse_each do |mhh|
149
154
 
150
155
  if mhh.execute?(self,*x)
@@ -161,8 +166,11 @@ class Class
161
166
  end
162
167
  ensure
163
168
  unless mhh.method_name
164
- self.class.class_eval do
165
- remove_method(tmp_method)
169
+ begin
170
+ self.class.class_eval do
171
+ remove_method(tmp_method)
172
+ end
173
+ rescue NameError
166
174
  end
167
175
  end
168
176
  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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000Z
12
+ date: 2012-01-29 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Chain of responsability implementation in a ruby fashion way. See README
15
15
  for examples