Sha256: 82f71f09813baf5deb481afc535fd3ab7eea34617dcc6610814221d82cbefdf9

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 Bytes

Contents

module FakeFS
  module Kernel

    @captives = { :original => {}, :hijacked => {}}
    class << self
      attr_accessor :captives
    end

    def self.hijack!
      captives[:hijacked].each do |name,prc|
        ::Kernel.send(:define_method, name.to_sym, &prc)
      end
    end

    def self.unhijack!
      captives[:original].each do |name,prc|
        ::Kernel.send(:define_method, name.to_sym, Proc.new do |*args, &block|
          ::FakeFS::Kernel.captives[:original][name].call(*args, &block)
        end)
      end
    end

    private
    def self.hijack name, &block
      captives[:original][name] = ::Kernel.method(name.to_sym)
      captives[:hijacked][name] = block || Proc.new { |args| }
    end

    hijack :open do |*args, &block|
      if args.first.start_with? '|'
        # This is a system command
        ::FakeFS::Kernel.captives[:original][:open].call(*args, &block)
      else
        name = args.shift
        FakeFS::File.open(name, *args, &block)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fakefs-0.5.4 lib/fakefs/kernel.rb
fakefs-0.5.3 lib/fakefs/kernel.rb