class Dhallish::Ast::Import

Attributes

buf[RW]
import_as_text[RW]
prefix[RW]
src[RW]

Public Class Methods

new(prefix, src, import_as_text) click to toggle source
# File lib/ast.rb, line 615
def initialize(prefix, src, import_as_text)
        @prefix = prefix
        @src = src
        @import_as_text = import_as_text
        @buf = nil
end

Public Instance Methods

compute_type(ctx) click to toggle source
# File lib/ast.rb, line 622
def compute_type(ctx)
        new_path = nil
        text = nil
        if prefix == "env:"
                text = ENV[@src]
                if text.nil?
                        raise NameError, "#{@src}: no such environment variable set"
                end
        else
                src = @src
                if prefix == "./" or prefix == "../"
                        basedir = ctx["<#DIR#>"]
                        if basedir.is_a? String
                                src = File.join basedir , (@prefix + src)
                                new_path = File.dirname src
                        else
                                opath = basedir.path
                                basedir.path = File.join basedir.path,  (@prefix + src)
                                src = basedir.to_s
                                basedir.path = File.dirname basedir.path
                                new_path = URI basedir.to_s
                                basedir.path = opath
                        end
                elsif prefix == "~/"
                        # ruby does not expand '~'
                        src = File.join ENV['HOME'], src
                        new_path = ENV['HOME']
                else
                        src = @prefix + src
                        new_path = URI src
                        new_path.path = File.dirname new_path.path
                end

                file = open(src)
                text = file.read

                if text.nil?
                        raise IOError, "url or file not found"
                end
        end

        if @import_as_text
                @buf = text
                Types::Text
        else
                # treat as dhallish expression
                @buf, type = Dhallish::evaluate(text, Dhallish::empty_context(new_path))
                type
        end
end
evaluate(ctx) click to toggle source
# File lib/ast.rb, line 673
def evaluate(ctx) @buf end