Class: GetComments::Read

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Read

Returns a new instance of Read.



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

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/getcomments/read.rb', line 3

def filename
  @filename
end

Class Method Details

.from(filename) ⇒ Object



5
6
7
# File 'lib/getcomments/read.rb', line 5

def self.from(filename)
  new(filename).get
end

Instance Method Details

#getObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/getcomments/read.rb', line 13

def get
  last_comment = []
  result = {}

  lines.each do |line|
    line.strip!

    if line[0] == '#'
      last_comment << line.gsub(/^# ?/, '')
    elsif !last_comment.empty?
      key = get_key line
      result[key] = last_comment.join("\n") if key
      last_comment = []
    end
  end

  result
end