Sha256: 7d7c111271560cdfbc76c33f7dc515a78598a4c09c3f06d0d440a6c84a20c338

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

module Finitio
  class Compilation

    def initialize(system = System.new, factory = TypeFactory.new, source = nil)
      @system  = system
      @factory = factory
      @proxies = []
      @source = source
    end
    attr_reader :system, :factory, :proxies, :source

    def self.coerce(arg, source = nil)
      case arg
      when NilClass    then new(System.new, TypeFactory.new, source)
      when System      then new(arg, arg.factory, source)
      when TypeFactory then new(System.new, arg, source)
      else
        raise ArgumentError, "Unable to coerce `#{arg}`"
      end
    end

    def imports
      @system.send(:imports)
    end

    def add_import(import)
      @system.add_import(import)
      self
    end

    def resolve_url(url)
      if url.to_s =~ /^\.\//
        resolve_relative_url(url)
      else
        STDLIB_PATHS.each do |path|
          file = File.expand_path("#{url}.fio", path)
          return Pathname.new(file) if File.file?(file)
        end
        raise Error, "No such import `#{url}`"
      end
    end

    def resolve_relative_url(url)
      raise Error, "Unable to resolve `#{url}`, missing path context"\
        unless source.respond_to?(:to_path)
      file = File.expand_path("../#{url}.fio", source.to_path)
      raise Error, "No such file `#{file}`" unless File.file?(file)
      Pathname.new(file)
    end

    def resolve_proxies!
      proxies.each do |p|
        p.resolve(system)
      end
      self
    end

    # Delegation to Factory

    TypeFactory::DSL_METHODS.each do |dsl_method|
      define_method(dsl_method){|*args, &bl|
        factory.public_send(dsl_method, *args, &bl)
      }
    end

    def proxy(*args, &bl)
      proxy = factory.proxy(*args, &bl)
      proxies << proxy
      proxy
    end

    # Delegation to System

    [
      :add_type,
      :fetch,
      :main,
    ].each do |meth|
      define_method(meth) do |*args, &bl|
        system.public_send(meth, *args, &bl)
      end
    end

  end # class Compilation
end # module Finitio

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
finitio-0.7.0 lib/finitio/support/compilation.rb
finitio-0.7.0.pre.rc4 lib/finitio/support/compilation.rb
finitio-0.7.0.pre.rc3 lib/finitio/support/compilation.rb
finitio-0.7.0.pre.rc2 lib/finitio/support/compilation.rb
finitio-0.7.0.pre.rc1 lib/finitio/support/compilation.rb