Sha256: d1bfc509873df84d2ee47c6c42c68b08b3154df73f7b865a0473446c34e8e690

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

#==============================================================================#
# $Id: core.rb 27 2007-08-15 14:35:31Z yuyakato $
#==============================================================================#

class Kagemusha #:nodoc:
  def initialize(klass) #:nodoc:
    @klass            = klass
    @meta             = class << @klass; self; end
    @class_methods    = {}
    @instance_methods = {}
  end

  def define_class_method(name, &block) #:nodoc:
    @class_methods[name.to_s.intern] = block
  end
  alias :defs :define_class_method

  def undefine_class_method(name) #:nodoc:
    @class_methods.delete(name.to_s.intern)
  end
  alias :undefs :undefine_class_method

  def define_instance_method(name, &block) #:nodoc:
    @instance_methods[name.to_s.intern] = block
  end
  alias :def :define_instance_method

  def undefine_instance_method(name) #:nodoc:
    @instance_methods.delete(name.to_s.intern)
  end
  alias :undef :undefine_instance_method

  def swap #:nodoc:
    original_class_methods    = {}
    original_instance_methods = {}

    @class_methods.each { |name, proc|
      method = @meta.instance_method(name)
      @meta.instance_eval { define_method(name, proc) }
      original_class_methods[name] = method
    }

    @instance_methods.each { |name, proc|
      method = @klass.instance_method(name)
      @klass.instance_eval { define_method(name, proc) }
      original_instance_methods[name] = method
    }

    yield
  ensure
    original_class_methods.each { |name, method|
      @meta.instance_eval { define_method(name, method) }
    }
    original_instance_methods.each { |name, method|
      @klass.instance_eval { define_method(name, method) }
    }
  end
end

#==============================================================================#
#==============================================================================#

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kagemusha-0.0.1 lib/kagemusha/core.rb