Class: CommitRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/repo_timetracker/commit_record.rb,
lib/repo_timetracker/commit_record_class.rb

Constant Summary collapse

A_LONG_TIME =

30 minutes

30*60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_directory, first_event_string = nil) ⇒ CommitRecord

Returns a new instance of CommitRecord.



11
12
13
14
15
16
17
18
# File 'lib/repo_timetracker/commit_record.rb', line 11

def initialize(project_directory, first_event_string = nil)
  @project_name = project_directory.slice(/[^\/]*$/)
  @timeline_directory = "#{project_directory}/.repo_timeline"
  @file_path = generate_file_path(project_directory)

  @events = []
  @events << Event.new(first_event_string) if first_event_string
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



7
8
9
# File 'lib/repo_timetracker/commit_record.rb', line 7

def events
  @events
end

#file_pathObject

Returns the value of attribute file_path.



7
8
9
# File 'lib/repo_timetracker/commit_record.rb', line 7

def file_path
  @file_path
end

Class Method Details

.create(project_directory, first_event_string = nil) ⇒ Object



4
5
6
7
# File 'lib/repo_timetracker/commit_record_class.rb', line 4

def create(project_directory, first_event_string = nil)
  commit = new(project_directory, first_event_string)
  commit.save
end

.load(commit_file_path) ⇒ Object



9
10
11
# File 'lib/repo_timetracker/commit_record_class.rb', line 9

def load(commit_file_path)
  YAML::load(IO.read(commit_file_path).to_s)
end

Instance Method Details

#==(other_commit) ⇒ Object



54
55
56
57
# File 'lib/repo_timetracker/commit_record.rb', line 54

def ==(other_commit)
    all_events_equal(other_commit) and 
    @file_path == other_commit.file_path
end

#add_events(events) ⇒ Object



35
36
37
38
# File 'lib/repo_timetracker/commit_record.rb', line 35

def add_events(events)
  @events += events
  save
end

#clear_eventsObject



44
45
46
47
# File 'lib/repo_timetracker/commit_record.rb', line 44

def clear_events
  @events = []
  save
end

#generate_new_event(event_string, following_time_spent = :working) ⇒ Object



20
21
22
23
24
# File 'lib/repo_timetracker/commit_record.rb', line 20

def generate_new_event(event_string, following_time_spent = :working)
  @events << Event.new(event_string, following_time_spent)
  @events[-2].following_time_spent = :not_working if long_after_previous_event?(@events[-1])
  save
end

#get_tailObject



40
41
42
# File 'lib/repo_timetracker/commit_record.rb', line 40

def get_tail
  @events[1..-1]
end

#saveObject



49
50
51
52
# File 'lib/repo_timetracker/commit_record.rb', line 49

def save
  File.open(@file_path, "w") { |f| f.puts YAML::dump(self) }
  self
end

#total_timeObject



26
27
28
29
30
31
32
33
# File 'lib/repo_timetracker/commit_record.rb', line 26

def total_time
  time = 0
  @events.each_cons(2) do |pair|
    time += pair[1].time_recorded - pair[0].time_recorded if pair[0].following_time_spent_working?
  end

  time.round
end