Sha256: 84abf5e6ee016c912fa644bf9b3dc970d01ef2633800f17b3ed27124725cca65

Contents?: true

Size: 1.78 KB

Versions: 10

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

10 entries across 10 versions & 1 rubygems

Version Path
ruby-ext-0.2.16 lib/ruby_ext/module.rb
ruby-ext-0.2.15 lib/ruby_ext/module.rb
ruby-ext-0.2.13 lib/ruby_ext/module.rb
ruby-ext-0.2.12 lib/ruby_ext/module.rb
ruby-ext-0.2.11 lib/ruby_ext/module.rb
ruby-ext-0.2.10 lib/ruby_ext/module.rb
ruby-ext-0.2.9 lib/ruby_ext/module.rb
ruby-ext-0.2.8 lib/ruby_ext/module.rb
ruby-ext-0.2.7 lib/ruby_ext/module.rb
ruby-ext-0.2.6 lib/ruby_ext/module.rb