Class: TodoLint::Todo
- Inherits:
-
Object
- Object
- TodoLint::Todo
- Defined in:
- lib/todo_lint/todo.rb
Overview
Todo represents a todo/fixme/etc comment within your source code
Constant Summary collapse
- PATTERN =
The regular expression that identifies todo comments
/ (?<flag> TODO ){0} (?<due_date> \(\d{4}-\d{2}-\d{2}\)){0} (?<tag>\#\w+){0} (?<task>.+){0} \g<flag>(?:\g<due_date>|(?:\(\g<tag>\)))?: \g<task> /x
Instance Attribute Summary collapse
-
#line ⇒ String
readonly
The content of the line of source code containing the todo comment.
-
#line_number ⇒ Fixnum
readonly
The 1-indexed line on which the todo was discovered.
-
#path ⇒ String
readonly
The absolute path to the file where we found this todo.
Class Method Summary collapse
-
.present_in?(line) ⇒ Boolean
Does this line contain a todo?.
-
.within(file, config: RequiredArg.new(:config)) ⇒ Array<Todo>
Search a file for all of the todo/fixme/etc comments within it.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Which todo is due sooner?.
-
#annotated? ⇒ Boolean
Was this todo annotated with a due date?.
-
#character_number ⇒ Fixnum
The 1-indexed character at which the todo comment is found.
-
#due_date ⇒ DueDate, NilClass
When this todo is due.
-
#flag ⇒ String
What did the developer write to get our attention?.
-
#initialize(line, line_number: RequiredArg.new(:line_number), path: RequiredArg.new(:path), config: RequiredArg.new(:config)) ⇒ Todo
constructor
A new Todo must know a few things.
-
#relative_path ⇒ String
The relative path to the file where this todo was found.
-
#tag ⇒ String, NilClass
What tag does this todo use?.
-
#tag? ⇒ Boolean
Was this todo using a tag (as opposed to a direct due date)?.
-
#task ⇒ String
What is the actual task associated with this todo?.
Constructor Details
#initialize(line, line_number: RequiredArg.new(:line_number), path: RequiredArg.new(:path), config: RequiredArg.new(:config)) ⇒ Todo
A new Todo must know a few things
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/todo_lint/todo.rb', line 66 def initialize(line, line_number: RequiredArg.new(:line_number), path: RequiredArg.new(:path), config: RequiredArg.new(:config)) absent_todo!(line) unless self.class.present_in?(line) @line = line @line_number = line_number @path = path @config = config end |
Instance Attribute Details
#line ⇒ String (readonly)
The content of the line of source code containing the todo comment
45 46 47 |
# File 'lib/todo_lint/todo.rb', line 45 def line @line end |
#line_number ⇒ Fixnum (readonly)
The 1-indexed line on which the todo was discovered
52 53 54 |
# File 'lib/todo_lint/todo.rb', line 52 def line_number @line_number end |
#path ⇒ String (readonly)
The absolute path to the file where we found this todo
60 61 62 |
# File 'lib/todo_lint/todo.rb', line 60 def path @path end |
Class Method Details
.present_in?(line) ⇒ Boolean
Does this line contain a todo?
36 37 38 |
# File 'lib/todo_lint/todo.rb', line 36 def self.present_in?(line) line =~ PATTERN end |
.within(file, config: RequiredArg.new(:config)) ⇒ Array<Todo>
Search a file for all of the todo/fixme/etc comments within it
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/todo_lint/todo.rb', line 18 def self.within(file, config: RequiredArg.new(:config)) file.each_line.with_index.map do |line, line_number| next unless present_in?(line) new( line, :line_number => line_number + 1, :path => file.path, :config => config ) end.compact end |
Instance Method Details
#<=>(other) ⇒ Fixnum
Which todo is due sooner?
168 169 170 |
# File 'lib/todo_lint/todo.rb', line 168 def <=>(other) due_date_for_sorting <=> other.due_date_for_sorting end |
#annotated? ⇒ Boolean
Was this todo annotated with a due date?
86 87 88 |
# File 'lib/todo_lint/todo.rb', line 86 def annotated? !match[:due_date].nil? || !match[:tag].nil? end |
#character_number ⇒ Fixnum
The 1-indexed character at which the todo comment is found
134 135 136 |
# File 'lib/todo_lint/todo.rb', line 134 def character_number (line =~ PATTERN) + 1 end |
#due_date ⇒ DueDate, NilClass
When this todo is due
110 111 112 113 114 115 116 117 118 |
# File 'lib/todo_lint/todo.rb', line 110 def due_date return unless annotated? return @due_date if defined?(@due_date) @due_date = if match[:due_date] DueDate.from_annotation(match[:due_date]) elsif match[:tag] lookup_tag_due_date end end |
#flag ⇒ String
What did the developer write to get our attention?
125 126 127 |
# File 'lib/todo_lint/todo.rb', line 125 def flag match[:flag] end |
#relative_path ⇒ String
The relative path to the file where this todo was found
179 180 181 182 |
# File 'lib/todo_lint/todo.rb', line 179 def relative_path current_dir = Pathname.new(File.("./")) Pathname.new(path).relative_path_from(current_dir).to_s end |
#tag ⇒ String, NilClass
What tag does this todo use?
157 158 159 |
# File 'lib/todo_lint/todo.rb', line 157 def tag match[:tag] end |
#tag? ⇒ Boolean
Was this todo using a tag (as opposed to a direct due date)?
144 145 146 |
# File 'lib/todo_lint/todo.rb', line 144 def tag? !match[:tag].nil? end |
#task ⇒ String
What is the actual task associated with this todo?
97 98 99 |
# File 'lib/todo_lint/todo.rb', line 97 def task match[:task].lstrip end |