Sha256: 33d33ca23a8de703fb14c28f6400591e2b63257c48f52d9e2b16fb17c666f076

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

# encoding: utf-8

module Chronicles

  # Reloads object method
  #
  # @private
  class Updater

    def initialize(object, name, code = nil)
      @object = object
      @name   = name
      @code   = code
      type
    end

    attr_reader :object, :name, :code

    def type
      @type ||= %w(public private protected).detect do |item|
        object.public_send("#{ item }_methods").include? name
      end
    end

    def klass
      object.singleton_class
    end

    def run
      redefine_method
      protect_method
    end

    private

    def redefine_method
      code = code()
      klass.__send__ :define_method, name do |*args|
        instance_eval(code) if code
        super(*args)
      end
    end

    def protect_method
      klass.instance_eval "#{ type } :#{ name }"
    end

  end # class Updater

end # module Chronicles

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicles-0.0.1 lib/chronicles/updater.rb