Class: TodosExport::Main

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Main

Returns a new instance of Main.



11
12
13
14
# File 'lib/todos_export.rb', line 11

def initialize(target)
  self.target = target
  self.exportables = []
end

Instance Attribute Details

#exportablesObject

Returns the value of attribute exportables.



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

def exportables
  @exportables
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#delete_exportable(file, line) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/todos_export.rb', line 63

def delete_exportable(file, line)
  lines = File.readlines(file)
  lines.delete_at(line - 1)

  File.open(file, "w") do |f|
    lines.each { |l| f.puts(l) }
  end

  self.find_exportables
end

#delete_exportablesObject



74
75
76
77
78
79
# File 'lib/todos_export.rb', line 74

def delete_exportables
  while !self.exportables.empty? do
    self.delete_exportable(self.exportables[0][:file], self.exportables[0][:line])
    self.find_exportables
  end
end

#executeObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/todos_export.rb', line 81

def execute
  self.find_exportables

  say("Found #{self.exportable_todos.size} TODO's, " \
    "#{self.exportable_fixmes.size} FIXME's and " \
    "#{self.exportable_bugs.size} BUG's" \
    " in #{self.exportables.map { |x| x[:file] }.uniq.size } of #{exportables.map { |x| x[:file] }.size } files searched.")
  say("\n")

  choose do |menu|
    menu.prompt = "Export to: "
    menu.choice('Github Issues') { TodosExport::GithubIssues.new(self).run }
  end
end

#exportable_bugsObject



59
60
61
# File 'lib/todos_export.rb', line 59

def exportable_bugs
  self.exportables.select { |x| x[:type] == 'BUG' }
end

#exportable_fixmesObject



55
56
57
# File 'lib/todos_export.rb', line 55

def exportable_fixmes
  self.exportables.select { |x| x[:type] == 'FIXME' }
end

#exportable_todosObject



51
52
53
# File 'lib/todos_export.rb', line 51

def exportable_todos
  self.exportables.select { |x| x[:type] == 'TODO' }
end

#find_exportablesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/todos_export.rb', line 28

def find_exportables
  self.find_files
  @exportables = []

  self.files.each do |file|
    File.open(file) do |f|
      f.each_with_index do |line, number|
        search = line.scan(/((?:#)(?:| )(TODO|FIXME|BUG):?(.*)$)/i)
        if !search.empty?
          self.exportables << {
            :type => search[0][1].upcase,
            :content => search[0][2].strip,
            :original_content => search[0][0],
            :file => file.gsub(/^(.\/)/, ''),
            :original_file => file,
            :line => number + 1
          }
        end
      end
    end
  end
end

#find_filesObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/todos_export.rb', line 16

def find_files
  if File.file?(self.target)
    self.files = [self.target]
  elsif File.directory?(self.target)
    self.files = Dir.glob(File.join(self.target, "**", "*.rb"))
  else
    abort "#{target} does not exist."
  end

  return self.files
end