-
-
Save apeiros/1329347 to your computer and use it in GitHub Desktop.
Revisions
-
apeiros revised this gist
Oct 31, 2011 . 1 changed file with 14 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,22 +2,17 @@ module MethodCop # guard methods that have side effects with a callback that fires before the method is invoked. If the callback returns a "falsey" value, # the method is halted and will not be called. The callback will return nil instead. # if the method does not have side effects or you depend on its return value, you should NOT use this on that method! def guard_method(guarded_method, guard=nil, &callback) # normalize guard guard = method(guard) if guard.is_a?(Symbol) guard = callback if callback raise ArgumentError, "You can only supply either a guard argument or a block" if block && guard raise ArgumentError, "guard argument must respond to call" unless guard.respond_to?(:guard) original = method(guarded_method) define_method(guarded_method) do |*args, &block| original.call(*args, &block) if guard.call(*args, &block) end end end @@ -31,5 +26,7 @@ def run_method? # block everything return false end guard_method :some_method, :run_method? guard_method :some_method do false end end -
babney revised this gist
Oct 31, 2011 . 1 changed file with 15 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,33 +2,28 @@ module MethodCop # guard methods that have side effects with a callback that fires before the method is invoked. If the callback returns a "falsey" value, # the method is halted and will not be called. The callback will return nil instead. # if the method does not have side effects or you depend on its return value, you should NOT use this on that method! def guard_method(name, options) method = name.to_sym original_method = "#{name}_orig".to_sym alias_method original_method, method define_method(name.to_sym) do unless options[:with].nil? unless !self.send(options[:with].to_sym) self.send(original_method) else # THIS METHOD IS UNDER ARREST nil end else # no method to guard with was given self.send(method) end end end end class Testing extend MethodCop def some_method return "I'm about to do something horribly wrong." end -
babney created this gist
Oct 31, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ module MethodCop # guard methods that have side effects with a callback that fires before the method is invoked. If the callback returns a "falsey" value, # the method is halted and will not be called. The callback will return nil instead. # if the method does not have side effects or you depend on its return value, you should NOT use this on that method! def self.included(base) base.class_eval do def self.guard_method(name, options) method = name.to_sym original_method = "#{name}_orig".to_sym alias_method original_method, method define_method(name.to_sym) do unless options[:with].nil? unless !self.send(options[:with].to_sym) self.send(original_method) else # THIS METHOD IS UNDER ARREST nil end else # no method to guard with was given self.send(method) end end end end end end class Testing include MethodCop def some_method return "I'm about to do something horribly wrong." end def run_method? # block everything return false end guard_method :some_method, :with => :run_method? end