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
# File 'lib/tlog/storage/disk.rb', line 10

def initialize(git_dir) 
  @git = Git.open(find_repo(git_dir))
  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



177
178
179
# File 'lib/tlog/storage/disk.rb', line 177

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

#change_log_owner(log, new_owner) ⇒ Object



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

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



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

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



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

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



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

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



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

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

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



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

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

#cur_entry_descriptionObject



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

def cur_entry_description
  current_entry_description
end

#cur_start_timeObject



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

def cur_start_time
  Time.parse(current_start_time) if current_start_path
end

#cur_userObject



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

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

#current_log_nameObject



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

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



55
56
57
58
59
60
61
62
63
64
# File 'lib/tlog/storage/disk.rb', line 55

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



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

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



173
174
175
# File 'lib/tlog/storage/disk.rb', line 173

def get_current_start_time 
  current_start_time
end

#in_branch(branch_exists = true) ⇒ Object

Code from ‘ticgit’, temporarily switches to tlog branch



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/tlog/storage/disk.rb', line 182

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



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

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

#pull_logsObject



51
52
53
# File 'lib/tlog/storage/disk.rb', line 51

def pull_logs
  git.pull('origin', 'origin/tlog')
end

#push_logsObject



47
48
49
# File 'lib/tlog/storage/disk.rb', line 47

def push_logs
  git.push('origin', 'tlog:tlog')
end

#require_log(log_name) ⇒ Object



66
67
68
# File 'lib/tlog/storage/disk.rb', line 66

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

#start_log(log, entry_description) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tlog/storage/disk.rb', line 70

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



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

def start_time_string
  current_start_time
end

#stop_log(log) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tlog/storage/disk.rb', line 82

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



151
152
153
154
155
156
157
158
# File 'lib/tlog/storage/disk.rb', line 151

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