Class: UpdateDraftRelease::Content
- Inherits:
-
Object
- Object
- UpdateDraftRelease::Content
- Defined in:
- lib/content.rb
Constant Summary collapse
- HEADING_REGEX =
/^\s*#+\s+.+/
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#line_separator ⇒ Object
readonly
Returns the value of attribute line_separator.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
- #append(new_lines) ⇒ Object
- #find_heading(heading) ⇒ Object
- #heading_indexes ⇒ Object
- #headings ⇒ Object
- #include?(sha) ⇒ Boolean
-
#initialize(body) ⇒ Content
constructor
A new instance of Content.
- #insert(line_num, new_lines) ⇒ Object
- #title ⇒ Object
- #to_s ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/content.rb', line 5 def body @body end |
#line_separator ⇒ Object (readonly)
Returns the value of attribute line_separator.
5 6 7 |
# File 'lib/content.rb', line 5 def line_separator @line_separator end |
#lines ⇒ Object (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_indexes ⇒ Object
21 22 23 |
# File 'lib/content.rb', line 21 def heading_indexes @lines.each_index.select { |i| @lines[i].match(HEADING_REGEX) } end |
#headings ⇒ Object
17 18 19 |
# File 'lib/content.rb', line 17 def headings @lines.select { |line| line.match(HEADING_REGEX) } end |
#include?(sha) ⇒ 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 |
#title ⇒ Object
13 14 15 |
# File 'lib/content.rb', line 13 def title @lines.first[0].upcase + @lines.first[1..-1] end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/content.rb', line 45 def to_s @lines.join(@line_separator) end |