Module: RubyExt::Callbacks::ClassMethods
- Defined in:
- lib/ruby_ext/more/callbacks.rb
Instance Method Summary collapse
- #set_callback(name, type, *executor_or_options, &block) ⇒ Object
- #wrap_method_with_callbacks(method, callback) ⇒ Object
- #wrap_with_callback(callback) ⇒ Object
Instance Method Details
#set_callback(name, type, *executor_or_options, &block) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ruby_ext/more/callbacks.rb', line 202 def set_callback name, type, *, &block name.must.be_a Symbol type = type.to_sym # Parsing arguments. opt = . "You can't provide both method name and block for filter!" if block and !.empty? executor = block || .first type.must.be_in [:before, :around, :after] executor.must.be_defined # Creating callback. callback = AbstractCallback.new callback = case type when :before then BeforeCallback.new when :around then AroundCallback.new when :after then AfterCallback.new end callback.executor = executor callback.terminator = opt.delete :terminator if type == :before callback.conditions = opt (self.callbacks[name] ||= []) << callback end |
#wrap_method_with_callbacks(method, callback) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ruby_ext/more/callbacks.rb', line 229 def wrap_method_with_callbacks method, callback method_without_callback = :"#{method}_without_#{callback}_of_#{self.alias}" if method_defined? method_without_callback raise "can't wrap method #{method} with #{callback} of #{self.alias} twice!" end alias_method method_without_callback, method define_method method do |*args, &block| # We can't use run_callbacks, because in case of the `super` # call it will be runned twice. run_callbacks callback, method do send method_without_callback, *args, &block end end end |
#wrap_with_callback(callback) ⇒ Object
245 246 247 248 249 |
# File 'lib/ruby_ext/more/callbacks.rb', line 245 def wrap_with_callback callback instance_methods(false).each do |method| wrap_method_with_callbacks method, callback end end |