Class: TimeTap::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/time_tap/project.rb

Defined Under Namespace

Classes: Pinch

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mid_path, name) ⇒ Project

Returns a new instance of Project.



88
89
90
91
92
# File 'lib/time_tap/project.rb', line 88

def initialize mid_path, name
  @name = name
  @path = File.expand_path("~/#{mid_path}/#{name}/")
  @pinches = []
end

Class Attribute Details

.pause_limitObject

Returns the value of attribute pause_limit.



5
6
7
# File 'lib/time_tap/project.rb', line 5

def pause_limit
  @pause_limit
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



87
88
89
# File 'lib/time_tap/project.rb', line 87

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



87
88
89
# File 'lib/time_tap/project.rb', line 87

def path
  @path
end

#pinchesObject (readonly)

Returns the value of attribute pinches.



3
4
5
# File 'lib/time_tap/project.rb', line 3

def pinches
  @pinches
end

Class Method Details

.[](path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/time_tap/project.rb', line 40

def [] path
  path = File.expand_path(path)
  how_nested = 1

  regex_suffix = "([^/]+)"
  if TimeTap.config["nested_project_layers"] && TimeTap.config["nested_project_layers"].to_i > 0
    how_nested =  TimeTap.config["nested_project_layers"].to_i

    # nested project layers works "how many folders inside your code folder
    # do you keep projects.
    #
    # For example, if your directory structure looks like:
    # ~/Code/
    #    Clients/
    #        AcmeCorp/
    #            website/
    #            intranet
    #        BetaCorp/
    #           skunkworks/
    #    OpenSource/
    #        project_one/
    #        timetap/
    #
    # A nested_project_layers setting of 2 would mean we track "AcmeCorp", "BetaCorp", and everything
    # under OpenSource, as their own projects
    regex_suffix = [regex_suffix] * how_nested
    regex_suffix = regex_suffix.join("/")
  end

  res = path.scan(%r{(#{TimeTap.config[:code] || "Code"})/#{regex_suffix}}).flatten
  mid_path = res[0] # not in a MatchObj group any more, so it's 0 based
  name = res[how_nested]
  mid_path, name = path.scan(%r{#{File.expand_path("~")}/([^/]+)/([^/]+)}).flatten if name.nil?
  if name
    name.chomp!
    key = name.underscore.downcase
    if projects[key].nil?
      project = Project.new mid_path, name
      projects[key] = project
    end
    projects[key]
  else
    nil
  end
end

.allObject



30
31
32
33
# File 'lib/time_tap/project.rb', line 30

def all
  load_file(history_file) if projects.empty?
  projects.values
end

.find(name) ⇒ Object



35
36
37
38
# File 'lib/time_tap/project.rb', line 35

def find name
  load_file(history_file) if projects.empty?
  projects[name.underscore.downcase]
end

.history_fileObject



10
11
12
# File 'lib/time_tap/project.rb', line 10

def history_file
  "#{TimeTap.config[:root]}/.tap_history"
end

.load_file(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/time_tap/project.rb', line 14

def load_file path

  File.open(File.expand_path(path), 'r', RUBY19 ? {:external_encoding => 'utf-8'} : nil) do |file|
    file.each_line do |line|
      time, path = line.split(": ")

      project = self[path]
      project << time.to_i if project
    end
  end
end

.projectsObject



26
27
28
# File 'lib/time_tap/project.rb', line 26

def projects
  @projects ||= HashWithIndifferentAccess.new
end

Instance Method Details

#<<(time) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/time_tap/project.rb', line 121

def << time
  time = Time.at time
  last_pinch = pinches.last
  if pinches.empty?
    pinches << Pinch.new(time)
  else
    last_time = last_pinch.end_time
    return unless time > last_time

    if (time - last_time) < self.class.pause_limit
      last_pinch.end_time = time
    else
      pinches << Pinch.new(time)
    end
  end
end

#daysObject



143
144
145
146
147
# File 'lib/time_tap/project.rb', line 143

def days
  pinches.group_by do |pinch|
    pinch.start_time.to_date
  end
end

#work_timeObject



138
139
140
# File 'lib/time_tap/project.rb', line 138

def work_time
  pinches.map(&:duration).inject(0.seconds, &:+)
end