Class: Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Tracker

Returns a new instance of Tracker.



19
20
21
22
# File 'lib/tracker.rb', line 19

def initialize(root)
	@root = root
	raise "#{root} is not a directory" if !File.directory?root
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



17
18
19
# File 'lib/tracker.rb', line 17

def root
  @root
end

Instance Method Details

#add(type, status, title) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tracker.rb', line 34

def add(type, status, title)
	directory = File.join(@root, type)
	ensure_directory_exists directory
	directory = File.join(directory, status)
	ensure_directory_exists directory
	filename = filename_from(title)
	filepath = File.join(directory, filename)
	begin
		file = File.new(filepath, 'w')
		file.puts title
	ensure
		file.close
	end

	tracked = Tracked.new()
	tracked.type = type
	tracked.status = status
	tracked.title = title
	tracked.filepath = filepath
	tracked.filename = filename

	tracked
end

#allObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tracker.rb', line 75

def all()
	if File.directory?@root
		Find.find(@root) do |path|
			if FileTest.directory?(path)
				if File.basename(path)[0] == '.'
					Find.prune
				else
					next
				end
			else
				if File.file?(path)
					# split into parts to extract status
					parts = path.split(File::SEPARATOR).reverse
					trackedfilename = parts.shift
					trackedstatus = parts.shift
					trackedtype = parts.shift
					yield get(trackedtype, trackedstatus, trackedfilename)
				else
					next
				end
			end
		end
	end
end

#ensure_directory_exists(directory) ⇒ Object



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

def ensure_directory_exists(directory)
	Dir.mkdir(directory) if !File.directory?(directory)
end

#filename_from(title) ⇒ Object



24
25
26
27
28
# File 'lib/tracker.rb', line 24

def filename_from(title)
	nowpart = Format::compact_datetime(DateTime.now)
	titlepart = Format::safe_for_filename(title)
	filename = "#{titlepart}_#{nowpart}.txt"
end

#find(filename) ⇒ Object



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
# File 'lib/tracker.rb', line 100

def find(filename)
	return nil if !File.directory?@root

	Find.find(@root) do |path|
		if FileTest.directory?(path)
			if File.basename(path)[0] == '.'
				Find.prune
			else
				next
			end
		else
			if File.basename(path) == filename
				# split into parts to extract status
				parts = path.split(File::SEPARATOR).reverse
				parts.shift # pops filename
				status = parts.shift
				type = parts.shift
				return get type, status, filename
			else
				next
			end
		end
	end
	nil
end

#get(type, status, filename) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tracker.rb', line 59

def get(type, status, filename)
	filepath = File.join(@root, type, status, filename)
	title = nil
	File.open(filepath) {|f| title = f.readline }
	title = title.gsub(/\n/, '')

	tracked = Tracked.new()
	tracked.type = type
	tracked.status = status
	tracked.filepath = filepath
	tracked.filename = filename
	tracked.title = title

	tracked
end