Class: Bio::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-vcf/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ Template

Returns a new instance of Template.



7
8
9
10
# File 'lib/bio-vcf/template.rb', line 7

def initialize fn
  raise "Can not find template #{fn}!" if not File.exist?(fn)
  parse(File.read(fn))
end

Instance Method Details

#body(env) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/bio-vcf/template.rb', line 59

def body env
  if @erb_body
    @erb_body.result(env)
  else
    ""
  end
end


67
68
69
70
71
72
73
# File 'lib/bio-vcf/template.rb', line 67

def footer env
  if @erb_footer
    @erb_footer.result(env)
  else
    ""
  end
end

#header(env) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bio-vcf/template.rb', line 51

def header env
  if @erb_header
    @erb_header.result(env)
  else
    ""
  end
end

#parse(buf) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bio-vcf/template.rb', line 12

def parse buf
  header = []
  body = []
  footer = []
  where = :header
  buf.split("\n").each do | line |
    case where
      when :header
        next if line =~ /=HEADER/
        if line =~ /=BODY/
          body = []
          where = :body
          next
        end
        header << line
      when :body
        if line =~ /=FOOTER/
          footer = []
          where = :footer
          next
        end
        body << line
      else
        footer << line
    end
  end
  if body == []
    body = header
    header = []
  end
  @erb_header = ERB.new(header.join("\n")) if header.size
  @erb_body   = ERB.new(body.join("\n")) if body.size
  @erb_footer = ERB.new(footer.join("\n")) if footer.size
end

#result(env) ⇒ Object



47
48
49
# File 'lib/bio-vcf/template.rb', line 47

def result env
  @erb.result(env)
end