Class: Walrus::WalrusGrammar::ImportDirective

Inherits:
Object
  • Object
show all
Defined in:
lib/walrus/walrus_grammar/import_directive.rb

Instance Method Summary collapse

Instance Method Details

#compile(options = {}) ⇒ Object

Returns an OpenStruct encapsulating information about the receiver for use by the compiler



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/walrus/walrus_grammar/import_directive.rb', line 24

def compile(options = {})
  info = OpenStruct.new
  path = Pathname.new @class_name.lexeme.to_s
  
  if path.absolute?
    # it will work just fine as it is
    info.class_name   = path.basename.to_s.to_class_name
    info.require_line = "require '#{path.to_s}'"
  else
    dir, base       = path.split
    info.class_name = base.to_s.to_class_name
    if dir.to_s == '.'
      # desired template is in the same directory
      info.require_line = "require File.join(File.dirname(__FILE__), '#{base.to_s}').to_s"
    else
      # desired template is in a relative directory
      info.require_line = "require File.join(File.dirname(__FILE__), '#{dir.to_s}', '#{base.to_s}').to_s"
    end
  end  
  info
end