Class: TimeTree::FileParser
- Inherits:
-
Object
- Object
- TimeTree::FileParser
show all
- Includes:
- DateCalculator
- Defined in:
- lib/time_tree/file_parser.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#date_between?, #in_prev_month?, #in_prev_week?, #next_weekday, #prev_weekday
Constructor Details
#initialize(tree, options) ⇒ FileParser
Returns a new instance of FileParser.
9
10
11
12
13
|
# File 'lib/time_tree/file_parser.rb', line 9
def initialize(tree, options)
@errors = []
@activity_tree = tree
@options = options
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
7
8
9
|
# File 'lib/time_tree/file_parser.rb', line 7
def errors
@errors
end
|
Instance Method Details
#add_error(line, message) ⇒ Object
98
99
100
|
# File 'lib/time_tree/file_parser.rb', line 98
def add_error(line, message)
@errors << "%s:%d: %s - %s" % [File.basename(@path), @line_number, line.chomp, message]
end
|
#find_path(paths) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/time_tree/file_parser.rb', line 15
def find_path(paths)
paths.each do |path|
return path if File.exist?(path)
end
@errors << "File not found in: #{paths.join(', ')}"
false
end
|
#parse_line(line) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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/time_tree/file_parser.rb', line 58
def parse_line(line)
@line_number += 1
case line.chomp.sub(/#.*/, '').strip
when ''
true
when /^(\d\d\d\d\/\d\d\/\d\d) *.*$/
set_date($1)
true
when /^(\d\d\d\d) +([-&\w\/]+) *(.*)$/
if minutes = mins($1)
unless @prev_mins.nil?
if minutes > @prev_mins
if @prev_activity != '-' && selected?(@date, @prev_context)
process_line(minutes, @prev_context, @prev_comment)
end
else
add_error(line, 'time does not advance')
return false
end
end
@prev_mins = minutes
@prev_activity = $2
@prev_context = $2 unless %w{- &}.include?($2)
@prev_comment = $3.size > 0 ? $3 : nil
true
else
false
end
else
add_error(line, 'line not understood')
false
end
end
|
#process_file(path) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/time_tree/file_parser.rb', line 37
def process_file(path)
if File.exist?(path)
if File.directory?(path)
process_folder(path)
else
set_file(path)
File.read(path).each_line {|line| parse_line(line) }
true
end
else
@errors << "File not found: #{path}"
false
end
end
|
#process_folder(path) ⇒ Object
52
53
54
55
56
|
# File 'lib/time_tree/file_parser.rb', line 52
def process_folder(path)
Dir.new(path).each do |file|
process_file(File.join(path, file)) unless file =~ /^\./
end
end
|
#selected?(date = @date, activities = '', options = @options) ⇒ Boolean
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/time_tree/file_parser.rb', line 106
def selected?(date = @date, activities = '', options = @options)
date_options = [:today, :yesterday, :week, :month, :date, :range]
parsed_date = Date.parse(date)
(
options.detect { |key, val| date_options.include?(key) }.nil? ||
options[:all] ||
options[:today] && parsed_date == Date.today ||
options[:yesterday] && parsed_date == Date.today-1 ||
options[:week] && in_prev_week?(parsed_date, options[:week]) ||
options[:month] && in_prev_month?(parsed_date, options[:month]) ||
options[:date] && parsed_date == Date.parse(options[:date]) ||
options[:range] && parsed_date >= Date.parse(options[:range].split(':').first) &&
parsed_date <= Date.parse(options[:range].split(':').last)
) &&
(
options[:filter] && options[:filter].detect { |f| activities =~ Regexp.new(f) } ||
options[:filter].nil?
)
end
|
#set_date(date) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/time_tree/file_parser.rb', line 29
def set_date(date)
@date = date
@prev_mins = nil
@prev_activity = nil
@prev_context = nil
@prev_comment = nil
end
|
#set_file(path) ⇒ Object
24
25
26
27
|
# File 'lib/time_tree/file_parser.rb', line 24
def set_file(path)
@path = path
@line_number = 0
end
|
#valid? ⇒ Boolean
102
103
104
|
# File 'lib/time_tree/file_parser.rb', line 102
def valid?
errors.empty?
end
|