Class: Presser::LocalFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LocalFile

Returns a new instance of LocalFile.



5
6
7
8
9
10
11
12
# File 'lib/local_file.rb', line 5

def initialize filename
  @in_header = true
  @body = ""
  @filename = filename
  @params = {}
  parse_all_lines
  # puts "The title: #{@title}"
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/local_file.rb', line 4

def body
  @body
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/local_file.rb', line 4

def params
  @params
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/local_file.rb', line 4

def title
  @title
end

Instance Method Details

#parse_all_linesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/local_file.rb', line 14

def parse_all_lines
  File.readlines(@filename).each do |line|
    line = line.strip
    if @in_header
      parse_line line
    else
      # puts "Body: '#{line}'"
      body << line << "\n"
    end
  end
  # puts "params: #{@params}"
  @title = params["title"] || "Default title"
end

#parse_line(line) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/local_file.rb', line 28

def parse_line line
  if line == "# End of header"
    @in_header = false
  else
    if line =~ /(.*):(.*)/
      # puts "match: #{line}"
      key = $1.strip
      val = $2.strip
      @params[key] = val
    else
      # puts "No match: #{line}"
    end
  end
end