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.



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

def initialize(filename)
  @filename = filename
  @comment_index = 0
end

Instance Attribute Details

#comment_indexObject

Returns the value of attribute comment_index.



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

def comment_index
  @comment_index
end

#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



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

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

Instance Method Details

#getObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/getcomments/read.rb', line 15

def get
  last_comment = []
  result = {}

  lines.each do |line|
    line.strip!

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

  unless last_comment.empty?
    result[get_key] = last_comment.join("\n")
  end

  result
end