Class: Tlog::Storage::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/tlog/storage/disk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_dir) ⇒ Disk

Returns a new instance of Disk.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tlog/storage/disk.rb', line 10

def initialize(git_dir)	
	@git = Git.open(find_repo(git_dir))
	# Format class?
	proj_path = @git.dir.path.downcase.gsub(/[^a-z0-9]+/i, '-')

	@tlog_dir = '~/.tlog'
	@tlog_working = File.expand_path(File.join(@tlog_dir, proj_path, 'working'))
	@tlog_index = File.expand_path(File.join(@tlog_dir, proj_path, 'index'))

	bs = git.lib.branches_all.map{|b| b.first}

	unless(bs.include?('tlog') && File.directory?(@tlog_working))
		init_tlog_branch(bs.include?('tlog'))
	end
end

Instance Attribute Details

#gitObject

Returns the value of attribute git.



4
5
6
# File 'lib/tlog/storage/disk.rb', line 4

def git
  @git
end

#tlog_dirObject

Returns the value of attribute tlog_dir.



5
6
7
# File 'lib/tlog/storage/disk.rb', line 5

def tlog_dir
  @tlog_dir
end

#tlog_indexObject

Returns the value of attribute tlog_index.



7
8
9
# File 'lib/tlog/storage/disk.rb', line 7

def tlog_index
  @tlog_index
end

#tlog_workingObject

Returns the value of attribute tlog_working.



6
7
8
# File 'lib/tlog/storage/disk.rb', line 6

def tlog_working
  @tlog_working
end

#working_dirObject

Returns the value of attribute working_dir.



8
9
10
# File 'lib/tlog/storage/disk.rb', line 8

def working_dir
  @working_dir
end

Instance Method Details

#all_log_dirsObject



159
160
161
# File 'lib/tlog/storage/disk.rb', line 159

def all_log_dirs
	Pathname.new(logs_path).children.select { |c| c.directory? }
end

#change_log_owner(log, new_owner) ⇒ Object



101
102
103
104
# File 'lib/tlog/storage/disk.rb', line 101

def change_log_owner(log, new_owner)
	log.path = log_path(log.name)
	log.update_owner(new_owner)
end

#change_log_points(log, new_points_value) ⇒ Object



96
97
98
99
# File 'lib/tlog/storage/disk.rb', line 96

def change_log_points(log, new_points_value)
	log.path = log_path(log.name)
	log.update_points(new_points_value)
end

#change_log_state(log, new_state) ⇒ Object



91
92
93
94
# File 'lib/tlog/storage/disk.rb', line 91

def change_log_state(log, new_state)
	log.path = log_path(log.name)
	log.update_state(new_state)
end

#checkout_log(log) ⇒ Object



26
27
28
29
30
# File 'lib/tlog/storage/disk.rb', line 26

def checkout_log(log)
	File.open(checkout_path, 'w'){|f| f.write(log.name)}
	git.add
	git.commit("Checking out time log '#{log.name}'")
end

#checkout_valueObject



32
33
34
# File 'lib/tlog/storage/disk.rb', line 32

def checkout_value
	read_file(checkout_path) if File.exists?(checkout_path)
end

#create_log(log, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/tlog/storage/disk.rb', line 36

def create_log(log, options = {})
	log.path = log_path(log.name)
	if log.create(options)
		git.add
		git.commit("Created log '#{log.name}'")
		true
	else
		false
	end
end

#cur_entry_descriptionObject



146
147
148
# File 'lib/tlog/storage/disk.rb', line 146

def cur_entry_description
	current_entry_description
end

#cur_start_timeObject



142
143
144
# File 'lib/tlog/storage/disk.rb', line 142

def cur_start_time
	Time.parse(current_start_time) if current_start_path
end

#cur_userObject



129
130
131
# File 'lib/tlog/storage/disk.rb', line 129

def cur_user
	git.config["user.email"].split('@').first rescue ''
end

#current_log_nameObject



150
151
152
153
# File 'lib/tlog/storage/disk.rb', line 150

def current_log_name
	name_contents = File.read(current_name_path) if File.exists?(current_name_path)
	name_contents.strip if name_contents
end

#delete_log(log) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/tlog/storage/disk.rb', line 47

def delete_log(log)
	log.path = log_path(log.name)
	log.delete
	delete_current(log.name)
	delete_checkout(log.name)

	# Recursively removes the directory that stores the time log
	git.remove(log.path, {:recursive => "-r"})
	git.commit("Deleted log '#{log.name}'")
end

#find_repo(dir) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/tlog/storage/disk.rb', line 117

def find_repo(dir)
	full = File.expand_path(dir)
	ENV["GIT_WORKING_DIR"] || loop do
		return full if File.directory?(File.join(full, ".git"))
		raise "No Repo Found" if full == full=File.dirname(full)
	end
end

#get_current_start_timeObject



155
156
157
# File 'lib/tlog/storage/disk.rb', line 155

def get_current_start_time 
	current_start_time
end

#in_branch(branch_exists = true) ⇒ Object

Code from ‘ticgit’, temporarily switches to tlog branch



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tlog/storage/disk.rb', line 164

def in_branch(branch_exists = true)
	unless File.directory?(@tlog_working)
		FileUtils.mkdir_p(@tlog_working)
	end

	old_current = git.lib.branch_current
	begin
		git.lib.change_head_branch('tlog')
		git.with_index(@tlog_index) do 
			git.with_working(@tlog_working) do |wd|
				git.lib.checkout('tlog') if branch_exists
				yield wd
			end
		end
	ensure
		git.lib.change_head_branch(old_current)
	end
end

#log_duration(log_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/tlog/storage/disk.rb', line 106

def log_duration(log_name)
	duration = 0
	if current_log_name == log_name
		duration += time_since_start
	end
	log_entries(log_name).each do |entry| # should just be able to do log.entries.each
		duration += entry.length
	end
	duration
end

#require_log(log_name) ⇒ Object



58
59
60
# File 'lib/tlog/storage/disk.rb', line 58

def require_log(log_name)
	decode_log_path(Pathname.new(log_path(log_name)))
end

#start_log(log, entry_description) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tlog/storage/disk.rb', line 62

def start_log(log, entry_description)
	entry_description = '(no description)' unless entry_description
	if update_current(log.name, entry_description)
		create_log(log) # Creates directory if it has not already been created
		git.add
		git.commit("Started log '#{log.name}'")
		true
	else
		false
	end
end

#start_time_stringObject



125
126
127
# File 'lib/tlog/storage/disk.rb', line 125

def start_time_string
	current_start_time
end

#stop_log(log) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tlog/storage/disk.rb', line 74

def stop_log(log)
	if Dir.exists?(current_path) and log.name == checkout_value
		current_hash = { 
			:name => current_log_name,
			:start_time => current_start_time,
			:description => current_entry_description,
		}
		delete_current(current_hash[:name])
		log.add_entry(current_hash)
		git.add
		git.commit("Stopped log '#{log.name}'")
		true
	else
		false
	end
end

#time_since_startObject



133
134
135
136
137
138
139
140
# File 'lib/tlog/storage/disk.rb', line 133

def time_since_start
	if Dir.exists?(current_path)
		difference = Time.now - Time.parse(current_start_time)
		difference.to_i
	else
		nil
	end
end