Class: Rogdl::Writer

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

Instance Method Summary collapse

Instance Method Details

#load_templates(dir) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/writer.rb', line 38

def load_templates(dir)
  excludes = ["CVS",".svn"]
  Find.find(dir) do |path|
    if FileTest.directory?(path)
      if excludes.include?(File.basename(path))
        Find.prune       # Don't look any further into this directory.
      else
        next
      end
    end

    if File.extname(path) == '.rhtml'
      file = File.new(path)
      s = ''
      file.each_line {|line| s << line }
      templates[File.basename(path).split('.').first] = s
    end
  end
  return self
end

#templatesObject



9
10
11
# File 'lib/writer.rb', line 9

def templates
  @templates ||= {}
end

#write(nodeArray) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/writer.rb', line 13

def write(nodeArray)
  nodes = to_node_array(nodeArray)
  a = []
  nodes.each do |node|
    raise "Writer::write: cannot find template" unless !templates[node.gname].nil?
    node.assign(self)
    a << node.render
  end
  return a
end

#write_to_files(nodeArray, namingProc) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/writer.rb', line 24

def write_to_files(nodeArray, namingProc)
  nodes = to_node_array(nodeArray)
  output_array = write(nodes)
  throw 'Illegal state: lengths do not match' if output_array.length != nodes.length
  
  0..output_array.length.times do |index|
    filename = namingProc.call(nodes[index])
    f = File.new(filename, 'w+')
    f << output_array[index]
    f.close
  end
  
end