Class: Erector::Erected

Inherits:
Object show all
Defined in:
lib/erector/erected.rb

Instance Method Summary collapse

Constructor Details

#initialize(in_file) ⇒ Erected

Returns a new instance of Erected.



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

def initialize(in_file)
  @in_file = in_file
end

Instance Method Details

#classnameObject



17
18
19
20
21
22
23
24
25
# File 'lib/erector/erected.rb', line 17

def classname
  base = classize(basename)
  parent = File.dirname(@in_file)
  grandparent = File.dirname(parent)
  if File.basename(grandparent) == "views"
    base = "Views::" + classize(File.basename(parent)) + "::" + base
  end
  base
end

#convertObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/erector/erected.rb', line 31

def convert
  parser = RhtmlParser.new
  parsed = parser.parse(File.read(@in_file))
  if parsed.nil?
    raise "Could not parse #{@in_file}\n" +
      parser.failure_reason
  else
    File.open(filename, "w") do |f|
      f.puts("class #{classname} < Erector::Widget")
      f.puts("  def render")
      f.puts(parsed.set_indent(2).convert)
      f.puts("  end")
      f.puts("end")
    end
  end
end

#filenameObject



13
14
15
# File 'lib/erector/erected.rb', line 13

def filename
  dir + basename + ".rb"
end

#textObject



27
28
29
# File 'lib/erector/erected.rb', line 27

def text
  File.read(@in_file)
end