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

Class methods ‘create_repo’ ‘all_logs’, also ‘create’ command



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

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



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

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

#create_log(log) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/tlog/storage/disk.rb', line 28

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

#cur_entry_descriptionObject



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

def cur_entry_description
  current_entry_description
end

#cur_entry_ownerObject



121
122
123
# File 'lib/tlog/storage/disk.rb', line 121

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

#cur_start_timeObject



117
118
119
# File 'lib/tlog/storage/disk.rb', line 117

def cur_start_time
  Time.parse(current_start_time) if current_start_path
end

#current_log_nameObject



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

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tlog/storage/disk.rb', line 39

def delete_log(log)
  log.path = log_path(log.name)
  if log.delete
    delete_current(log.name)
    git.remove(log.path, {:recursive => "-r"})
    git.commit("Deleted log #{log.name}")
    true
  else
    false
  end
end

#find_repo(dir) ⇒ Object



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

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



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

def get_current_start_time 
  current_start_time
end

#in_branch(branch_exists = true) ⇒ Object

Code from ‘ticgit’, temporarily switches to tlog branch



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tlog/storage/disk.rb', line 143

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



85
86
87
88
89
90
91
92
93
94
# File 'lib/tlog/storage/disk.rb', line 85

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



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

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

#start_log(log, entry_description) ⇒ Object



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

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



104
105
106
# File 'lib/tlog/storage/disk.rb', line 104

def start_time_string
  current_start_time
end

#stop_log(log) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tlog/storage/disk.rb', line 67

def stop_log(log)
  if Dir.exists?(current_path)
    current_hash = { 
      :name => current_log_name,
      :start_time => current_start_time,
      :description => current_entry_description,
      :owner => cur_entry_owner
    }
    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



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

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