Class: Dhallish::Ast::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, src, import_as_text) ⇒ Import

Returns a new instance of Import.



615
616
617
618
619
620
# 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

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



613
614
615
# File 'lib/ast.rb', line 613

def buf
  @buf
end

#import_as_textObject

Returns the value of attribute import_as_text.



612
613
614
# File 'lib/ast.rb', line 612

def import_as_text
  @import_as_text
end

#prefixObject

Returns the value of attribute prefix.



610
611
612
# File 'lib/ast.rb', line 610

def prefix
  @prefix
end

#srcObject

Returns the value of attribute src.



611
612
613
# File 'lib/ast.rb', line 611

def src
  @src
end

Instance Method Details

#compute_type(ctx) ⇒ Object



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# 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) ⇒ Object



673
# File 'lib/ast.rb', line 673

def evaluate(ctx) @buf end