Sha256: d28b980a93fcc898cb15997a79f853cc4556d5bc7208b64ba22adfe917e4e7e4

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'set'

Module.class_eval do
  def is?(base)
    ancestors.include?(base)
  end
  
  def wrap_method( sym, prefix = "old_", &blk )
    old_method = "#{prefix}_#{sym}".to_sym
    alias_method old_method, sym
    define_method(sym) do |*args|
      instance_exec(old_method, *args, &blk)
    end
  end
  
  def inherited_instance_methods
  	im = Set.new
  	ancestors.each do |a| 
  		next if a == self
  		im.merge a.instance_methods
  	end
  	return im.to_a
  end
  
  def namespace
    if @module_namespace_defined
      @module_namespace
    else
      @module_namespace_defined = true
      @module_namespace = Module.namespace_for name
    end
  end
  
  def each_namespace &block
    current = namespace
    while current do
      block.call current
      current = current.namespace
    end
  end
  
  def each_ancestor include_standard = false, &block
    if include_standard
      ancestors.each{|a| block.call a unless a == self}
    else
      exclude = [self, Object, Kernel]
      ancestors.each do |a|
        block.call a unless exclude.include? a
      end
    end
  end
  
  def self_ancestors_and_namespaces &b
    b.call self
    each_ancestor &b
    each_namespace &b
  end
  
  def self.namespace_for class_name
    list = class_name.split("::")
    if list.size > 1
      list.pop
      return eval list.join("::"), TOPLEVEL_BINDING, __FILE__, __LINE__			
    else
      return nil
    end
  end
  
  def inherit *modules
    modules.each do |amodule|
      include amodule
      
      processed = []
      amodule.ancestors.each do |a|			
        if a.const_defined? :ClassMethods				
          class_methods = a.const_get :ClassMethods
          next if processed.include? class_methods
          processed << class_methods
          extend class_methods
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-ext-0.2.5 lib/ruby_ext/module.rb
ruby-ext-0.2.4 lib/ruby_ext/module.rb