Class: UpdateDraftRelease::Content

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

Constant Summary collapse

HEADING_REGEX =
/^\s*#+\s+.+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Content

Returns a new instance of Content.



7
8
9
10
11
# File 'lib/content.rb', line 7

def initialize(body)
  @body = body
  @line_separator = if body =~ /(\r\n|\n)/ then $1 else %(\r\n) end
  @lines = body.split(@line_separator)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#line_separatorObject (readonly)

Returns the value of attribute line_separator.



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

def line_separator
  @line_separator
end

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

Instance Method Details

#append(new_lines) ⇒ Object



29
30
31
# File 'lib/content.rb', line 29

def append(new_lines)
  insert(@lines.size, new_lines)
end

#find_heading(heading) ⇒ Object



25
26
27
# File 'lib/content.rb', line 25

def find_heading(heading)
  @lines.index { |line| line.match(/^\s*#+\s+.*#{heading}.*/i)  }
end

#heading_indexesObject



21
22
23
# File 'lib/content.rb', line 21

def heading_indexes
  @lines.each_index.select { |i| @lines[i].match(HEADING_REGEX) }
end

#headingsObject



17
18
19
# File 'lib/content.rb', line 17

def headings
  @lines.select { |line| line.match(HEADING_REGEX) }
end

#include?(sha) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/content.rb', line 41

def include?(sha)
  @body.include?(sha)
end

#insert(line_num, new_lines) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/content.rb', line 33

def insert(line_num, new_lines)
  if line_num == 0 || @lines[line_num - 1].strip.empty?
    @lines[line_num,0] = Array(new_lines).flat_map { |line| [line, ''] }
  else
    @lines[line_num,0] = Array(new_lines).flat_map { |line| ['', line] }
  end
end

#titleObject



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

def title
  @lines.first[0].upcase + @lines.first[1..-1]
end

#to_sObject



45
46
47
# File 'lib/content.rb', line 45

def to_s
  @lines.join(@line_separator)
end