Class: Syctask::Scanner
- Inherits:
-
Object
- Object
- Syctask::Scanner
- Defined in:
- lib/syctask/scanner.rb
Overview
A Scanner scans text or files for tasks. The task is identified by an annotation
Example
@tasks; title;description;prio;follow_up Title 1;Description 1;;2016-09-10 Title 2;Description 2;1;2016-09-20
@tasks; is indicating that tasks are following separated by ‘;’. The next line describes to which fields the values belong to. The next lines are the actual field values.
Instance Attribute Summary collapse
-
#scan(content) ⇒ Object
readonly
Scans the content in regard to tasks.
-
#scanned ⇒ Object
readonly
Indicates whether task has been scanned since last @task(s) annotation.
-
#separator ⇒ Object
readonly
The separator that separates task values.
-
#task_count ⇒ Object
readonly
The task counter holding tasks scanned.
-
#task_fields ⇒ Object
readonly
The task fields.
-
#tasks ⇒ Object
readonly
The scanned tasks.
Instance Method Summary collapse
-
#initialize ⇒ Scanner
constructor
Creates a new scanner.
-
#load_task_fields(line) ⇒ Object
Checks if the ‘line’ contains task fields.
-
#multiple_scan? ⇒ Boolean
Checks whether multiple tasks should be scanned which is indicated by ‘@scan == ’@tasks’.
-
#options_of(task_values) ⇒ Object
Retrieves the task values except for the ‘title’ value and returns a hash of the task values.
-
#scan_file(file) ⇒ Object
Scans a file for tasks.
-
#scan_line(line) ⇒ Object
Scans a string (line) for tasks.
-
#scan_task_line(line) ⇒ Object
Scans the ‘line’ for task values.
-
#scan_text(text) ⇒ Object
Scans a text for tasks.
-
#title_of(task_values) ⇒ Object
Retrieves the ‘title’ value from the task_values.
Constructor Details
#initialize ⇒ Scanner
Creates a new scanner
35 36 37 |
# File 'lib/syctask/scanner.rb', line 35 def initialize @tasks = {} end |
Instance Attribute Details
#scan(content) ⇒ Object (readonly)
Scans the content in regard to tasks. ‘content’ may be a file or a string. It checks if ‘content’ is a file and if it exists scans the file otherwise it asumes the content to be text and scans accordingly.
22 23 24 |
# File 'lib/syctask/scanner.rb', line 22 def scan @scan end |
#scanned ⇒ Object (readonly)
Indicates whether task has been scanned since last @task(s) annotation
24 25 26 |
# File 'lib/syctask/scanner.rb', line 24 def scanned @scanned end |
#separator ⇒ Object (readonly)
The separator that separates task values
26 27 28 |
# File 'lib/syctask/scanner.rb', line 26 def separator @separator end |
#task_count ⇒ Object (readonly)
The task counter holding tasks scanned
30 31 32 |
# File 'lib/syctask/scanner.rb', line 30 def task_count @task_count end |
#task_fields ⇒ Object (readonly)
The task fields
28 29 30 |
# File 'lib/syctask/scanner.rb', line 28 def task_fields @task_fields end |
#tasks ⇒ Object (readonly)
The scanned tasks
32 33 34 |
# File 'lib/syctask/scanner.rb', line 32 def tasks @tasks end |
Instance Method Details
#load_task_fields(line) ⇒ Object
Checks if the ‘line’ contains task fields. If it contains task fields it sets the @task_fields and returns true, otherwise returns false
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/syctask/scanner.rb', line 84 def load_task_fields(line) task_fields = line.split(@separator).map do |field| field.strip.downcase.gsub(/(?<=.)-(?=.)/, "_") end if (Syctask::Task::FIELDS & task_fields).empty? false else @task_fields = task_fields.map { |f| f.strip.downcase.to_sym } true end end |
#multiple_scan? ⇒ Boolean
Checks whether multiple tasks should be scanned which is indicated by ‘@scan == ’@tasks’.
134 135 136 |
# File 'lib/syctask/scanner.rb', line 134 def multiple_scan? @scan =~ /@tasks/i end |
#options_of(task_values) ⇒ Object
Retrieves the task values except for the ‘title’ value and returns a hash of the task values
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/syctask/scanner.rb', line 120 def (task_values) task_values = task_values - [task_values[@task_fields.index(:title)]] task_fields = @task_fields - [:title] = {} task_fields.each_with_index do |field, index| if Syctask::Task::FIELDS.include?(field.to_s) [field] = task_values[index].strip end end end |
#scan_file(file) ⇒ Object
Scans a file for tasks
52 53 54 55 56 |
# File 'lib/syctask/scanner.rb', line 52 def scan_file(file) File.foreach(file) do |line| scan_line(line.strip) end end |
#scan_line(line) ⇒ Object
Scans a string (line) for tasks
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/syctask/scanner.rb', line 66 def scan_line(line) if line =~ /^@tasks./i @scan, @separator = line.scan(/(@tasks)(.)/i).flatten @scanned = false elsif line =~ /^@task./i @scan, @separator = line.scan(/(@task)(.)/i).flatten @scanned = false else if not @scanned load_task_fields(line) || scan_task_line(line) elsif multiple_scan? scan_task_line(line) end end end |
#scan_task_line(line) ⇒ Object
Scans the ‘line’ for task values
98 99 100 101 102 103 104 105 |
# File 'lib/syctask/scanner.rb', line 98 def scan_task_line(line) return if line.start_with? '-' task_values = line.split(@separator) if @task_fields && (@task_fields.size == task_values.size) @tasks[title_of(task_values)] = (task_values) @scanned = true end end |
#scan_text(text) ⇒ Object
Scans a text for tasks
59 60 61 62 63 |
# File 'lib/syctask/scanner.rb', line 59 def scan_text(text) text.each_line do |line| scan_line(line.strip) end end |
#title_of(task_values) ⇒ Object
Retrieves the ‘title’ value from the task_values. If title column value is missing an ArgumentError is trown
109 110 111 112 113 114 115 |
# File 'lib/syctask/scanner.rb', line 109 def title_of(task_values) if @task_fields.index(:title).nil? raise ArgumentError, "scan: No 'title' header column found" end task_values[@task_fields.index(:title)].strip end |