Skip to content

Instantly share code, notes, and snippets.

@apeiros
Forked from babney/method_cop.rb
Created October 31, 2011 23:06
Show Gist options
  • Select an option

  • Save apeiros/1329347 to your computer and use it in GitHub Desktop.

Select an option

Save apeiros/1329347 to your computer and use it in GitHub Desktop.

Revisions

  1. apeiros revised this gist Oct 31, 2011. 1 changed file with 14 additions and 17 deletions.
    31 changes: 14 additions & 17 deletions method_cop.rb
    Original 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(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
    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, :with => :run_method?

    guard_method :some_method, :run_method?
    guard_method :some_method do false end
    end
  2. @babney babney revised this gist Oct 31, 2011. 1 changed file with 15 additions and 20 deletions.
    35 changes: 15 additions & 20 deletions method_cop.rb
    Original 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 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
    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
    include MethodCop
    extend MethodCop
    def some_method
    return "I'm about to do something horribly wrong."
    end
  3. @babney babney created this gist Oct 31, 2011.
    40 changes: 40 additions & 0 deletions method_cop.rb
    Original 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