Class: Walrus::WalrusGrammar::DefDirective

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

Instance Method Summary collapse

Instance Method Details

#compile(options = {}) ⇒ Object

Returns a string containing the compiled (Ruby) version of receiver.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/walrus/walrus_grammar/def_directive.rb', line 23

def compile(options = {})
  internal = ''
  
  if @params == []
    external = "def #{@identifier.to_s}\n"
  else
    # this will work for the simple case where params are plain identifiers
    params = (@params.kind_of? Array) ? @params : [@params]
    param_list  = params.collect { |param| param.compile }.join(', ')
    external = "def #{@identifier.to_s}(#{param_list})\n"
  end
  
  nested  = nil
  
  if @content.respond_to? :each
    content = @content
  else
    content = [@content]
  end
  
  content.each do |element|
    if element.kind_of? WalrusGrammar::DefDirective # must handle nested def blocks here
      inner, outer = element.compile(options)
      nested = ['', ''] if nested.nil?
      external << inner if inner
      nested[1] << "\n" + outer
    else
      # again, may wish to forget the per-line indenting here if it breaks sensitive directive types
      # (#ruby blocks for example, which might have here documents)
      element.compile(options).each do |lines| # may return a single line or an array of lines
        lines.each { |line| external << '  ' + line }
      end
    end
  end
  
  external << "end\n\n"
  
  if nested
    external << nested[1]
  end
  
  internal = nil if internal == '' # better to return nil than an empty string here (which would get indented needlessly)
  [internal, external]
end