ptf 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ module Ptf
2
+ class Group
3
+
4
+ def initialize(name, abbrev)
5
+ @name = name
6
+ @abbreviation = abbrev
7
+ end
8
+
9
+ def name
10
+ @name
11
+ end
12
+
13
+ def abbreviation
14
+ @abbreviation
15
+ end
16
+
17
+ def to_s
18
+ "#{name}:#{abbreviation}"
19
+ end
20
+
21
+ def is_default?
22
+ Group.default_group.to_s == to_s
23
+ end
24
+
25
+
26
+ class << self
27
+ def config
28
+ @config = Ptf::Config.get_config if @config.nil?
29
+
30
+ @config
31
+ end
32
+
33
+ def from_name(group_name)
34
+ group_file = Ptf::FileSystem.group_list_file
35
+ File.open(group_file, "r") do |f|
36
+ f.each_line do |l|
37
+ name, abbrev = l.gsub(/\s+/, "").split(":")
38
+
39
+ if group_name == name
40
+ return Group.new(name, abbrev)
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ nil
47
+ end
48
+
49
+ def from_abbrev(group_abbrev)
50
+ group_file = Ptf::FileSystem.group_list_file
51
+ File.open(group_file, "r") do |f|
52
+ f.each_line do |l|
53
+ name, abbrev = l.gsub(/\s+/, "").split(":")
54
+
55
+ if group_abbrev == abbrev
56
+ return Group.new(name, abbrev)
57
+ end
58
+
59
+ end
60
+ end
61
+
62
+ nil
63
+ end
64
+
65
+ def get_group(group)
66
+ if(group.length <= 4 && group == group.upcase)
67
+ from_abbrev(group)
68
+ else
69
+ from_name(group)
70
+ end
71
+ end
72
+
73
+ def group_exist?(g)
74
+ group_file = Ptf::FileSystem.group_list_file
75
+
76
+ File.open(group_file, "r") do |f|
77
+ f.each_line do |l|
78
+ name, abbrev = l.gsub(/\s+/, "").split(":")
79
+
80
+ if name == g || abbrev == g
81
+ return true
82
+ end
83
+
84
+ end
85
+ end
86
+
87
+ false
88
+ end
89
+
90
+ def all_groups
91
+ group_arr = []
92
+
93
+ File.open(Ptf::FileSystem.group_list_file, "r") do |f|
94
+ f.each_line do |l|
95
+ name, abbrev = l.gsub(/\s+/, "").split(":")
96
+
97
+ group_arr.push (Ptf::Group.new(name, abbrev))
98
+ end
99
+ end
100
+
101
+ group_arr
102
+ end
103
+
104
+ def default_group
105
+ default_group = config[:default_group]
106
+
107
+ name, abbrev = default_group.gsub(/\s+/, "").split(":")
108
+ Ptf::Group.new(name, abbrev)
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+ end
115
+
@@ -0,0 +1,301 @@
1
+ module Ptf
2
+ # Represents a single Metadata file.
3
+ #
4
+ # @author Austin Blatt
5
+ # @since 0.1.0
6
+ class MetadataFile
7
+
8
+ # Create a MetadataFile object from the given input.
9
+ #
10
+ # @param title [String] the task's title.
11
+ # @param group [Ptf::Group] the Group the task is associated with.
12
+ # @param due_date [DateTime, nil] the due date given for the task.
13
+ # @param estimate [Integer, nil] the estimated time to complete the task.
14
+ # @param id [Integer] the unique ID number for this task.
15
+ #
16
+ # @return [Ptf::MetadataFile] the metadata file for the task.
17
+ def self.create_from_input(title, group, due_date, estimate, id)
18
+ data = {
19
+ :title => title,
20
+ :group => group,
21
+ :due_date => due_date,
22
+ :estimate => estimate,
23
+ :id => id,
24
+ :created_at => DateTime.now,
25
+ :completed_at => nil
26
+ }
27
+
28
+ # Get the filepath for the infofile
29
+ open_dir = Ptf::FileSystem.metadata_open_dir
30
+ group_dir = File.join(open_dir, group.name)
31
+ filepath = File.join(group_dir, id.to_s)
32
+
33
+ self.new(filepath, data)
34
+ end
35
+
36
+ # Create a MetadataFile object from the given filepath.
37
+ #
38
+ # @param filepath [String] the full file path to the metadata file.
39
+ #
40
+ # @raise [ArgumentError] if the given filepath does not exist.
41
+ # @raise [ArgumentError] if the given file is not a metadata file.
42
+ #
43
+ # @return [Ptf::MetadataFile] the object representation of the metadata file.
44
+ def self.create_from_file(filepath)
45
+ raise ArgumentError, "File #{filepath} does not exist." unless Ptf::FileSystem.file_exist?(filepath)
46
+
47
+ data = {}
48
+ # parse file
49
+ File.open(filepath, "r") do |f|
50
+ f.each_line do |l|
51
+ key, val = l.gsub(/\s+/, "").split(":")
52
+
53
+ if key.nil? || !(key.is_a? String)
54
+ raise ArgumentError, "Error parsing file."
55
+ end
56
+
57
+ # Convert values to appropriate types
58
+ case key
59
+ when "group"
60
+ val = Ptf::Group.from_name(val)
61
+ when "due_date", "created_at", "completed_at"
62
+ val = (val.nil? ? val : Ptf::Date.str_to_datetime(val))
63
+ when "id"
64
+ val = val.to_i
65
+ when "estimate"
66
+ val = (val.nil? ? nil : val.to_i)
67
+ end
68
+
69
+ data[key.to_sym] = val
70
+ end
71
+ end
72
+
73
+ return self.new(filepath, data)
74
+ end
75
+
76
+ # Initialize a new metadata file.
77
+ #
78
+ # @param filepath [String] the full path to the file.
79
+ # @param data [Hash] the data in the file.
80
+ # @option data [String] :title the task's title.
81
+ # @option data [Ptf::Group] :group the Group the task is associated with.
82
+ # @option data [DateTime, nil] :due_date the due date given for the task.
83
+ # @option data [Integer, nil] :estimate the estimated time to complete the task.
84
+ # @option data [Integer] :id the unique ID number for this task.
85
+ # @option data [DateTime] :created_at the time the task was created.
86
+ # @option data [DateTime, nil] :completed_at the time the task was completed (nil if it has not been completed).
87
+ # @option data [String] :hash the file hash (the filepath of the data file).
88
+ def initialize(filepath, data)
89
+ @filepath = filepath
90
+ @data = data
91
+ end
92
+
93
+ # Add content to the metadata file.
94
+ #
95
+ # @param key [Symbol] the key for the new content.
96
+ # @param val [Object] the value for the new content.
97
+ #
98
+ # @raise [ArgumentError] if the key is not a symbol.
99
+ def add_content(key, val)
100
+ raise ArgumentError, "The key #{key} is not a symbol." unless key.is_a? Symbol
101
+ raise ArgumentError, "The key #{key} already exists." unless @data[key].nil?
102
+
103
+ @data[key] = val
104
+ end
105
+
106
+ # Returns the title of the task.
107
+ #
108
+ # @return [String] the title of the task.
109
+ def title
110
+ @data[:title]
111
+ end
112
+
113
+ # Set the task's title to a new title.
114
+ #
115
+ # @param new_title [String] the new title.
116
+ #
117
+ # @raise [ArgumentError] if the new_title is not a String.
118
+ def set_title(new_title)
119
+ raise ArgumentError, "The new title must be a string. Recieved a #{new_title.class.name}" unless new_title.is_a?(String)
120
+
121
+ @data[:title] = new_title
122
+ end
123
+
124
+ # Returns the group the task is associated with.
125
+ #
126
+ # @return [Ptf::Group] the group the task is associated with.
127
+ def group
128
+ @data[:group]
129
+ end
130
+
131
+ # Returns the due date of the task.
132
+ #
133
+ # @return [DateTime, nil] the due date of the task or nil if no due date exists.
134
+ def due_date
135
+ @data[:due_date]
136
+ end
137
+
138
+ # Returns the due date of the task as a String.
139
+ #
140
+ # @return [String] the due date of the task as a string.
141
+ def due_date_str
142
+ return "" if due_date.nil?
143
+
144
+ Ptf::Date.datetime_to_str(due_date)
145
+ end
146
+
147
+ # Set the task due's date.
148
+ #
149
+ # @param new_date [DateTime, nil] the new due date for the task
150
+ #
151
+ # @raise [ArgumentError] if new_date is not a DateTime or nil.
152
+ def set_due_date(new_date)
153
+ raise ArgumentError, "Invalid new due date." unless (new_date.nil? || new_date.is_a?(DateTime))
154
+
155
+ @data[:due_date] = new_date
156
+ end
157
+
158
+ # Returns the given datetime in list format
159
+ #
160
+ # @param datetime [DateTime] the datetime to convert to a String.
161
+ #
162
+ # @return [String] a String representing the datetime (ex. 'Wed Dec 21 - 11AM -')
163
+ def list_format(datetime)
164
+ datetime.strftime("%a %b %_d - %l%p -")
165
+ end
166
+
167
+ # Returns the due date of the task for the ptf list command.
168
+ #
169
+ # @return [String] the due date in the form 'Mon Dec 19 - 11AM -' or 'Mon Dec 9 - 2PM -'.
170
+ def due_date_list_format
171
+ return "" if due_date.nil?
172
+
173
+ list_format due_date
174
+ end
175
+
176
+ # Returns the estimated time to complete the task.
177
+ #
178
+ # @return [Integer] the estimated time to compelte the task.
179
+ def estimate
180
+ @data[:estimate]
181
+ end
182
+
183
+ # Set the task's estiamted time.
184
+ #
185
+ # @param new_estimate [Integer, nil] the new estimated time to complete the task.
186
+ #
187
+ # @raise [ArgumentError] if the new_estimate is not an Integer or nil.
188
+ def set_estimate(new_estimate)
189
+ raise ArgumentError, "The new estimate, #{new_estimate.to_s}, is not an integer." unless (new_estimate.nil? || new_estimate.is_a?(Integer))
190
+
191
+ @data[:estimate] = new_estimate
192
+ end
193
+
194
+ # Returns the date and time that the task was created at.
195
+ #
196
+ # @return [DateTime] the date and time that the task was created at.
197
+ def created_at
198
+ @data[:created_at]
199
+ end
200
+
201
+ # Returns the date and time that the task was created at as a String.
202
+ #
203
+ # @return [String] the date and time represented as a string.
204
+ def created_at_str
205
+ Ptf::Date.datetime_to_str create_at
206
+ end
207
+
208
+ # Returns the date and time that the task was completed at.
209
+ #
210
+ # @return [DateTime] the date and time that the task was completed at.
211
+ def completed_at
212
+ @data[:completed_at]
213
+ end
214
+
215
+ # Returns the date and time that the task was completed at as a String.
216
+ #
217
+ # @return [String] the date and time represented as a string.
218
+ def completed_at_str
219
+ Ptf::Date.datetime_to_str completed_at
220
+ end
221
+
222
+ # Returns the completed at time of the task for the ptf list command.
223
+ #
224
+ # @return [String] the completed at time in the form 'Mon Dec 19 - 11AM -' or 'Mon Dec 9 - 2PM -'.
225
+ def completed_at_list_format
226
+ return "" if completed_at.nil?
227
+
228
+ list_format completed_at
229
+ end
230
+
231
+ # Return the id of the task.
232
+ #
233
+ # @return [Integer] the ID number of the task.
234
+ def id
235
+ @data[:id]
236
+ end
237
+
238
+ # Return the hash of the task.
239
+ #
240
+ # @return [String] the hash of the metadata file (the name of the data file).
241
+ def hash
242
+ @data[:hash]
243
+ end
244
+
245
+ # Completes the task.
246
+ #
247
+ # Fills in the completed_at field with the current DateTime.
248
+ # Removes the metadata file from the in progress directory and writes it to the completed directory.
249
+ def complete_now
250
+ @data[:completed_at] = DateTime.now
251
+
252
+ FileUtils.rm @filepath
253
+ @filepath = File.join(File.join(Ptf::FileSystem.metadata_closed_dir, group.name), id.to_s)
254
+
255
+ write_to_file
256
+ end
257
+
258
+ def reopen
259
+ FileUtils.rm @filepath
260
+ @filepath = File.join(File.join(Ptf::FileSystem.metadata_open_dir, group.name), id.to_s)
261
+
262
+ write_to_file
263
+ end
264
+
265
+ def key_val_to_str(key, val)
266
+ str_val = val
267
+ case key
268
+ when :group
269
+ str_val = val.name
270
+ when :due_date, :created_at, :completed_at
271
+ str_val = (val.nil? ? "" : Ptf::Date.datetime_to_str(val))
272
+ end
273
+
274
+ "#{key}:#{str_val}"
275
+ end
276
+
277
+ # Returns the data in the file as a String.
278
+ #
279
+ # @return [String] the entire Metadata file as a String.
280
+ def file_string
281
+ return_str = ""
282
+ @data.each do |key, val|
283
+ return_str += "#{key_val_to_str(key, val)}\n"
284
+ end
285
+
286
+ return_str
287
+ end
288
+
289
+ # Writes the metadata file. Overwrites any previous metadata file for the same task.
290
+ def write_to_file
291
+ file = File.new(@filepath, "w")
292
+
293
+ @data.each do |key, val|
294
+ file.puts "#{key_val_to_str(key, val)}"
295
+ end
296
+ file.close
297
+ end
298
+
299
+ end
300
+ end
301
+
@@ -1,3 +1,3 @@
1
1
  module Ptf
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ptf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Blatt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-28 00:00:00.000000000 Z
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -96,19 +96,18 @@ files:
96
96
  - lib/ptf/commands.rb
97
97
  - lib/ptf/commands/group.rb
98
98
  - lib/ptf/commands/group/add.rb
99
+ - lib/ptf/commands/group/remove.rb
99
100
  - lib/ptf/commands/init.rb
100
101
  - lib/ptf/commands/list.rb
101
102
  - lib/ptf/commands/task.rb
102
103
  - lib/ptf/commands/task/create.rb
103
104
  - lib/ptf/commands/task/edit.rb
104
105
  - lib/ptf/config.rb
105
- - lib/ptf/data.rb
106
- - lib/ptf/data/data_file.rb
107
- - lib/ptf/data/group.rb
108
- - lib/ptf/data/metadata_file.rb
109
- - lib/ptf/utilities.rb
110
- - lib/ptf/utilities/date.rb
111
- - lib/ptf/utilities/file_system.rb
106
+ - lib/ptf/data_file.rb
107
+ - lib/ptf/date.rb
108
+ - lib/ptf/file_system.rb
109
+ - lib/ptf/group.rb
110
+ - lib/ptf/metadata_file.rb
112
111
  - lib/ptf/version.rb
113
112
  - ptf.gemspec
114
113
  homepage: https://github.com/austb/ptf