class MonitorMethod

Example usage:

instance_monitor Array, :map do |map|
  method_monitor nil, If[empty: true], If[args: 1] do |*args, &block|
    block.call nil
    [args, block]
  end
end

Public Class Methods

class_monitors(klass, method_predicate=nil, &block) click to toggle source
# File lib/utils/monitor_method.rb, line 64
def self.class_monitors(klass, method_predicate=nil, &block)
  if method_predicate
    klass.public_methods.each do |method|
      if method_predicate.call klass, method
        self.class_monitor     klass, method, &block
      end
    end
  else
    klass.public_methods.each do |method|
      self.class_monitor       klass, method, &block
    end
  end
end
classes_monitors(klass_predicate=nil, method_predicate=nil, &block) click to toggle source
# File lib/utils/monitor_method.rb, line 78
def self.classes_monitors(klass_predicate=nil, method_predicate=nil, &block)
  if klass_predicate
    ObjectSpace.each_object(Class) do |klass|
      if klass_predicate.call klass
        self.class_monitors   klass, method_predicate, &block
      end
    end
  else
    ObjectSpace.each_object(Class) do |klass|
      self.class_monitors     klass, method_predicate, &block
    end
  end
end
instance_monitors(klass, method_predicate=nil, &block) click to toggle source
# File lib/utils/monitor_method.rb, line 97
def self.instance_monitors(klass, method_predicate=nil, &block)
  if method_predicate
    klass.public_instance_methods.each do |method|
      if method_predicate.call klass, method
        self.instance_monitor  klass, method, &block
      end
    end
  else
    klass.public_instance_methods.each do |method|
      self.instance_monitor    klass, method, &block
    end
  end
end
instances_monitors(klass_predicate=nil, method_predicate=nil, &block) click to toggle source
# File lib/utils/monitor_method.rb, line 111
def self.instances_monitors(klass_predicate=nil, method_predicate=nil, &block)
  if klass_predicate
    ObjectSpace.each_object(Class) do |klass|
      if klass_predicate.call  klass
        self.instance_monitors klass, method_predicate, &block
      end
    end
  else
    ObjectSpace.each_object(Class) do |klass|
      self.instance_monitors   klass, method_predicate, &block
    end
  end
end
method_monitor(args_predicate=nil, block_predicate=nil, &passed_block) click to toggle source

return a proc that takes a method block takes |*args, &block| and returns [new_args, new_block] or falsey

# File lib/utils/monitor_method.rb, line 19
def self.method_monitor(args_predicate=nil, block_predicate=nil, &passed_block)
  unless passed_block.parameters.map(&:first) == [:rest, :block]
    raise ArgumentError, "must pass a block with arguments of the form: |*args, &block|, was of the form: #{passed_block&.parameters.inspect}".freeze
  end
  lambda do |_|
    lambda do |*args, &block|
      if !args_predicate || args_predicate.call(args)
        if !block_predicate || block_predicate.call(block)
          passed_block.call(*args, &block)
        end
      end || [args, block]
    end
  end.freeze
end
new() click to toggle source
# File lib/utils/monitor_method.rb, line 13
def self.new
  raise NoMethodError
end
singleton_monitors(object, method_predicate=nil, &block) click to toggle source
# File lib/utils/monitor_method.rb, line 130
def self.singleton_monitors(object, method_predicate=nil, &block)
  if method_predicate
    object.public_methods.each do |method|
      if method_predicate.call object, method
        self.instance_monitor  object, method, &block
      end
    end
  else
    object.public_methods.each do |method|
      self.singleton_monitor   object, method, &block
    end
  end
end
unbound_method_monitor(bound_predicate=nil, args_predicate=nil, block_predicate=nil, &passed_block) click to toggle source

return a proc that takes an unbound method block takes |bound_to, *args, &block| and returns [new_args, new_block] or falsey

# File lib/utils/monitor_method.rb, line 36
def self.unbound_method_monitor(bound_predicate=nil, args_predicate=nil, block_predicate=nil, &passed_block)
  unless passed_block.parameters.map(&:first) == [:opt, :rest, :block]
    raise ArgumentError, "must pass a block with arguments of the form: |bound_to, *args, &block|, was of the form: #{passed_block&.parameters.inspect}".freeze
  end
  lambda do |_|
    lambda do |bound_to|
      if !bound_predicate || bound_predicate.call(bound_to)
        lambda do |*args, &block|
          if !args_predicate || args_predicate.call(args)
            if !block_predicate || block_predicate.call(block)
              passed_block.call(bound_to, *args, &block)
            end
          end || [args, block]
        end
      else
        lambda do |*args, &block|
          [args, block]
        end.freeze
      end
    end
  end.freeze
end