Sha256: 7681a260f32363d99eff9028387be5526373c52cc08e52a54461743c4413121a

Contents?: true

Size: 1.19 KB

Versions: 35

Compression:

Stored size: 1.19 KB

Contents

module FakeFS
  # Kernel Module
  module Kernel
    @captives = { original: {}, hijacked: {} }

    class << self
      attr_accessor :captives
    end

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

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

    # NOTE: maybe private
    def self.hijack(name, &block)
      captives[:original][name] = ::Kernel.method(name.to_sym)
      captives[:hijacked][name] = block || proc { |_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

35 entries across 35 versions & 1 rubygems

Version Path
fakefs-2.3.0 lib/fakefs/kernel.rb
fakefs-2.2.0 lib/fakefs/kernel.rb
fakefs-2.1.0 lib/fakefs/kernel.rb
fakefs-2.0.0 lib/fakefs/kernel.rb
fakefs-1.9.0 lib/fakefs/kernel.rb
fakefs-1.8.0 lib/fakefs/kernel.rb
fakefs-1.7.0 lib/fakefs/kernel.rb
fakefs-1.6.0 lib/fakefs/kernel.rb
fakefs-1.5.1 lib/fakefs/kernel.rb
fakefs-1.5.0 lib/fakefs/kernel.rb
fakefs-1.4.1 lib/fakefs/kernel.rb
fakefs-1.4.0 lib/fakefs/kernel.rb
fakefs-1.3.2 lib/fakefs/kernel.rb
fakefs-1.3.1 lib/fakefs/kernel.rb
fakefs-1.3.0 lib/fakefs/kernel.rb
fakefs-1.2.3 lib/fakefs/kernel.rb
fakefs-1.2.2 lib/fakefs/kernel.rb
fakefs-1.2.1 lib/fakefs/kernel.rb
fakefs-1.2.0 lib/fakefs/kernel.rb
fakefs-1.1.0 lib/fakefs/kernel.rb