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



169
170
171
# File 'lib/tlog/storage/disk.rb', line 169

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

#change_log_owner(log, new_owner) ⇒ Object



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

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

	git.add
	git.commit("Changed owner for time log #{log.name}")
end

#change_log_points(log, new_points_value) ⇒ Object



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

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

	git.add
	git.commit("Changed points value for time log #{log.name}")
end

#change_log_state(log, new_state) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/tlog/storage/disk.rb', line 92

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

	git.add
	git.commit("Changed state for time log #{log.name}")
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



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

def cur_entry_description
	current_entry_description
end

#cur_start_timeObject



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

def cur_start_time
	Time.parse(current_start_time) if current_start_path
end

#cur_userObject



139
140
141
# File 'lib/tlog/storage/disk.rb', line 139

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

#current_log_nameObject



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

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



127
128
129
130
131
132
133
# File 'lib/tlog/storage/disk.rb', line 127

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



165
166
167
# File 'lib/tlog/storage/disk.rb', line 165

def get_current_start_time 
	current_start_time
end

#in_branch(branch_exists = true) ⇒ Object

Code from ‘ticgit’, temporarily switches to tlog branch



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/tlog/storage/disk.rb', line 174

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



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

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



135
136
137
# File 'lib/tlog/storage/disk.rb', line 135

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
90
# 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



143
144
145
146
147
148
149
150
# File 'lib/tlog/storage/disk.rb', line 143

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