Class: TaskList::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/task-list/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments: [], options: {}) ⇒ Parser

Returns a new instance of Parser.



8
9
10
11
12
13
14
15
16
# File 'lib/task-list/parser.rb', line 8

def initialize(arguments: [], options: {})
  self.search_path = options[:search_path]
  @github = options[:github] if options[:github]
  @plain = options[:plain] if options[:plain]
  @type = options[:type].upcase if options[:type]
  @files = fuzzy_find_files queries: arguments
  @tasks = {}
  VALID_TASKS.each { |t| @tasks[t.to_sym] = [] }
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/task-list/parser.rb', line 6

def files
  @files
end

#githubObject (readonly)

Returns the value of attribute github.



6
7
8
# File 'lib/task-list/parser.rb', line 6

def github
  @github
end

#search_pathObject

Returns the value of attribute search_path.



6
7
8
# File 'lib/task-list/parser.rb', line 6

def search_path
  @search_path
end

#tasksObject (readonly)

Returns the value of attribute tasks.



6
7
8
# File 'lib/task-list/parser.rb', line 6

def tasks
  @tasks
end

Instance Method Details

#git_repo_rootObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/task-list/parser.rb', line 74

def git_repo_root
  full_search_path = File.expand_path(self.search_path)
  root_path = full_search_path.dup
  repo_found = false

  begin
    if File.directory?(File.join(root_path, ".git"))
      repo_found = true
      break
    end

    directories = root_path.split(File::SEPARATOR)
    directories.pop
    root_path = File.join(*directories)
  end until repo_found

  unless repo_found
    # FIXME: Show an error message instead of raising an exception
    raise "No git repo found."
  end

  root_path
end

#github?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/task-list/parser.rb', line 98

def github?
  !!@github
end

#parse!Object

Parse all the collected files to find tasks and populate the tasks hash



28
29
30
31
32
33
34
# File 'lib/task-list/parser.rb', line 28

def parse!
  unless @type.nil? || VALID_TASKS.include?(@type)
    raise TaskList::Exceptions::InvalidTaskTypeError.new type: @type
  end

  @files.each { |f| parsef! file: f }
end

#plain?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/task-list/parser.rb', line 102

def plain?
  !!@plain
end

#print!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/task-list/parser.rb', line 36

def print!
  @tasks.each do |type, tasks|
    unless tasks.empty?
      puts "#{type}:\n#{'-' * (type.length + 1)}\n"

      if self.github? || self.plain?
        puts
      end

      tasks.each do |task|
        if self.github?
          if Pathname.new(self.search_path).absolute?
            reference = "#{task[:file].sub(/#{Regexp.escape(self.git_repo_root)}\//, "")}#L#{task[:line_number]}"
          else
            search_path_components = File.expand_path(self.search_path).split(File::SEPARATOR)
            search_path_components.pop
            parent = File.join(*search_path_components)
            file = task[:file].sub(/#{Regexp.escape(parent)}\//, "")
            reference = "#{file}#L#{task[:line_number]}"
          end

          puts "- #{task[:task]} — [#{reference}](#{reference})"
        else
          puts task[:task]

          if self.plain?
            puts "  line #{task[:line_number]} in #{task[:file]}"
          else
            puts "  \e[30m\e[1mline #{task[:line_number]} in #{task[:file]}\e[0m"
          end
        end
      end

      puts
    end
  end
end