Class: Transform

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

Instance Method Summary collapse

Constructor Details

#initialize(inFd, outFd = $stdout) ⇒ Transform

Returns a new instance of Transform.



11
12
13
14
15
16
17
18
# File 'lib/terrascript.rb', line 11

def initialize(inFd, outFd = $stdout)
  @in = inFd
  @out = outFd
  @mode = :read
  @argName = "block"
  @arg = ""
  @body = ""
end

Instance Method Details

#processObject



20
21
22
23
24
# File 'lib/terrascript.rb', line 20

def process
  @in.each do |line|
    processLine line
  end
end

#processLine(line) ⇒ Object



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
# File 'lib/terrascript.rb', line 26

def processLine(line)
  case @mode

  when :read
    if line.lstrip.start_with?("@inline")
      args = line.split(' ')
      if !args[1].nil?
        @argName = args[1]
      end
      @mode = :body
    else
      @out.puts line
    end

  when :arg
    if line.lstrip.start_with?("@end")
      transform
      @arg = ""
      @body = ""
      @mode = :read
    else
      @arg << line
    end

  when :body
    @body << line
    if line.lstrip.start_with?("return")
      @mode = :arg
    end

  end
end

#transformObject



4
5
6
7
8
9
# File 'lib/terrascript.rb', line 4

def transform
  s = $stdout
  $stdout = @out
  eval("lambda { |#{@argName}|\n#{@body}\n}").call(@arg)
  $stdout = s
end