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 raise "Internal error, it shouldn't be loaded twice!" if defined? ESCAPE_METHOD_SYMBOLS # some tricky error when runing spec with rake ESCAPE_METHOD_SYMBOLS = [ ['==', 'assign'], ['>', 'gt'], ['<', 'lt'], ['>=', 'gte'], ['<=', 'lte'], ['?', 'qst'], ['!', 'imp'], ['<=>', 'lorg'], ['*', 'mp'], ['+', 'add'], ['-', 'sub'], ['=', 'assign'], ['**', 'pw'], ['=~', 'sim'], ['[]', 'sb'], ] def escape_method method m = method.to_s.clone ESCAPE_METHOD_SYMBOLS.each{|from, to| m.gsub! from, to} raise "Invalid method name '#{method}'!" unless m =~ /^[_a-zA-Z0-9]+$/ m end end