Class: Syctask::Scanner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeScanner

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

#scannedObject (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

#separatorObject (readonly)

The separator that separates task values



26
27
28
# File 'lib/syctask/scanner.rb', line 26

def separator
  @separator
end

#task_countObject (readonly)

The task counter holding tasks scanned



30
31
32
# File 'lib/syctask/scanner.rb', line 30

def task_count
  @task_count
end

#task_fieldsObject (readonly)

The task fields



28
29
30
# File 'lib/syctask/scanner.rb', line 28

def task_fields
  @task_fields
end

#tasksObject (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’.

Returns:

  • (Boolean)


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 options_of(task_values)
  task_values = task_values - [task_values[@task_fields.index(:title)]]
  task_fields = @task_fields - [:title]
  options = {}
  task_fields.each_with_index do |field, index|
    if Syctask::Task::FIELDS.include?(field.to_s) 
      options[field] = task_values[index].strip
    end
  end
  options
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)] = options_of(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