Sha256: ad4d7e4d92021abcc90cfd93b4ec177c4ff1200b5eef74426e3ac1e7549c61b8

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

module Mutant
  class Zombifier
    # File containing source being zombified
    class File
      include Adamantium::Flat, Concord::Public.new(:path), AST::Sexp

      # Zombify contents of file
      #
      # @return [self]
      #
      # @api private
      #
      def zombify(namespace)
        $stderr.puts("Zombifying #{path}")
        eval(
          Unparser.unparse(namespaced_node(namespace)),
          TOPLEVEL_BINDING,
          path.to_s
        )
        self
      end

      # Find file by logical path
      #
      # @param [String] logical_name
      #
      # @return [File]
      #   if found
      #
      # @return [nil]
      #   otherwise
      #
      # @api private
      #
      def self.find(logical_name)
        file_name =
          case ::File.extname(logical_name)
          when '.so'
            return
          when '.rb'
            logical_name
          else
            "#{logical_name}.rb"
          end

        $LOAD_PATH.each do |path|
          path = Pathname.new(path).join(file_name)
          return new(path) if path.file?
        end

        $stderr.puts "Cannot find file #{file_name} in $LOAD_PATH"
        nil
      end

    private

      # Return node
      #
      # @return [Parser::AST::Node]
      #
      # @api private
      #
      def node
        Parser::CurrentRuby.parse(path.read, path.to_s)
      end

      # Return namespaced root
      #
      # @param [Symbol] namespace
      #
      # @return [Parser::AST::Node]
      #
      # @api private
      #
      def namespaced_node(namespace)
        s(:module, s(:const, nil, namespace), node)
      end

    end # File
  end # Zombifier
end # Mutant

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mutant-0.7.3 lib/mutant/zombifier/file.rb
mutant-0.7.2 lib/mutant/zombifier/file.rb
mutant-0.7.1 lib/mutant/zombifier/file.rb
mutant-0.6.7 lib/mutant/zombifier/file.rb
mutant-0.6.6 lib/mutant/zombifier/file.rb
mutant-0.6.5 lib/mutant/zombifier/file.rb
mutant-0.6.4 lib/mutant/zombifier/file.rb
mutant-0.6.3 lib/mutant/zombifier/file.rb