-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4t.rb
192 lines (150 loc) · 4.23 KB
/
4t.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/ruby
require 'time'
def get_new_id
case RUBY_PLATFORM
when /linux/
return `uuidgen`.strip
else
raise 'UUID generation is not yet supported on #{RUBY_PLATFORM}'
end
end
class Config
class Category
attr_accessor :id, :name, :abbreviation, :options
def initialize name, abbreviation, options, *id
@name = name
@abbreviation = abbreviation
@options = options
if ! id.empty?
@id = id[0]
else
@id = get_new_id()
end
end
end
def initialize
@categories = [
Category.new( 'Start time', 's', nil ),
Category.new( 'End time', 'e', nil ),
Category.new( 'Project', 'p', [] ),
Category.new( 'Task', 't', [] ),
Category.new( 'Class', 'c', [] )
]
end
def reinitialize categories
@categories = categories
end
def add_category name, abbrev=''
return if ! get_cats( :name, name ).empty?
@categories.push == Category.new( cat, abbrev, [] )
end
def get_abbreviation name
if ( ! ( cats = get_cats( :name, name ) ).empty? ) then return cats[0].abbreviation end
end
def set_abbreviation name, abbrev
if ! ( cats = get_cats( :name, name ) ).empty? then cats[0].abbreviation = abbrev end
end
def get_full_name abbrev
if ! ( cats = get_cats( :abbreviation, abbrev ) ).empty? then return cats[0].name end
end
def get_options name
if ! ( cats = get_cats( :name, name ) ).empty? then return cats[0].options end
end
private
def get_cats attribute, value
@categories.select { |v| v.method( attribute ).call == value }
end
end
$config = Config.new
class Record
attr_reader :description, :start_time, :end_time, :categories
def initialize text
self.reinitialize text
end
def reinitialize text
description = []
@start_time = nil
@end_time = nil
@categories = Hash.new( [] )
( text.split( /\s/ ) ).each do | word |
case word
when /:/
abbrev, value = word.split( ':', 2 )
full_name = $config.get_full_name abbrev
case full_name
when nil
description.push word
when 'Start time'
@start_time = value=='' ? Time.now : Time.parse( value )
when 'End time'
@end_time = value=='' ? Time.now : Time.parse( value )
else
@categories[ full_name ] = @categories[ full_name ].push value
end
else
description.push word
end
end
@description = description.join ' '
end
# Return a string that could be parsed by Record#reinitialise to create an
# identical Record.
def to_text
result = ''
if ! @start_time.nil?
result += $config.get_abbreviation( 'Start time' ) + ':'
result += @start_time.strftime( '%Y-%m-%d-%H:%M' ) + " "
end
if ! @end_time.nil?
result += $config.get_abbreviation( 'End time' ) + ':'
result += @end_time.strftime( '%Y-%m-%d-%H:%M' ) + " "
end
@categories.each { |k,v| result += $config.get_abbreviation(k) + ":#{v} " }
result += @description
end
def to_s indent=''
result = ''
result += "#{indent}Start time; #{@start_time}\n"
result += "#{indent}End time; #{@end_time}\n"
result += "#{indent}Description; #{@description}\n"
result += "#{indent}Categories;\n"
@categories.each { |k,v| result += "#{indent} #{k} => #{v}\n" }
result
end
end
class RecordList
def initialize
@records = {}
end
def set_all records
@records = records
end
def update_record id, record
@records[id] = record
end
def add_record record
@records[ get_new_id ] = record
end
def get_records_in_period start_time, end_time
@records.select do |k,v|
( !v.start_time.nil? && v.start_time < end_time ) || ( !v.end_time.nil? && v.end_time > start_time )
end
end
def to_s
result = ''
@records.each { |k,v| result += "#{k} =>\n#{v.to_s( ' ' )}" }
result
end
end
w = Record.new 's: e:2011-01-01-12:45:37.072 p:crap stuf and more stuff'
x = Record.new 's: p:ploppy stoopid stuff'
y = RecordList.new
y.add_record x
y.add_record w
puts
puts y
puts
puts x.to_text
puts
puts y.get_records_in_period Time.parse('2011-01-01-00:00:00'), Time.parse('2011-12-31-23:59:59')
puts