Class: Octokom::Editor

Inherits:
Object show all
Defined in:
lib/octokom/editor.rb

Overview

The ‘Octokom::Editor` class opens and parses files where you can insert a title and description for pull-requests or GitHub issues. This class is inspired by the `Pry::Editor` class (pry.github.io).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, title, desc) ⇒ Editor

Returns a new instance of Editor.



13
14
15
16
17
# File 'lib/octokom/editor.rb', line 13

def initialize(repo, title, desc)
  @repo  = repo
  @title = title
  @desc  = desc
end

Class Method Details

.open(repo, title, desc) ⇒ Object



6
7
8
9
10
11
# File 'lib/octokom/editor.rb', line 6

def self.open(repo, title, desc)
  editor = Editor.new(repo, title, desc)
  editor.prepare_tempfile
  editor.open_editor
  editor.parse_user_input
end

Instance Method Details

#open_editorObject

Execute the command that is used to start the editor. This includes the waiting flag as well as the file and line number.



39
40
41
# File 'lib/octokom/editor.rb', line 39

def open_editor
  system("#{editor_name} #{waiting_flag} #{file_inc_line(file_name, 6)}")
end

#parse_user_inputObject

Opens the tempfile once again to parse the content and return the ‘title` and `description`.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octokom/editor.rb', line 45

def parse_user_input
  input = {title: [], desc: []}
  parsing = false

  read_tempfile.each do |line|
    match = /^\s*#\s*(title|desc)/i.match(line)
    parsing = match[1].downcase.to_sym if match

    unless line =~ /^\s*#/
      input[parsing] << line if parsing
    end
  end

  input.update(input) { |_, lines| lines.join("\n") }.values
end

#prepare_tempfileObject

Save a tempfile that includes the input from the command line or the last commit message. TODO Remove #Title and #Description section and use first line; following lines. TODO use last commit message for title



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/octokom/editor.rb', line 23

def prepare_tempfile
  File.open(file_name, 'w') do |f|
    f.puts '# Please enter a title and optional a description below'
    f.puts '# the comments. To continue, save end close the file.'
    f.puts "# Comment lines starting with '#' will be ignored."
    f.puts
    f.puts '# Title'
    f.puts @title
    f.puts
    f.puts '# Description'
    f.puts @desc
  end
end