Class: RworkTracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yamlfile = nil) ⇒ RworkTracker

Returns a new instance of RworkTracker.



96
97
98
99
100
# File 'lib/rworktracker.rb', line 96

def initialize(yamlfile = nil)
  @yamlfile = yamlfile
  @wdata = Hash.new
  @time = Time.now
end

Instance Attribute Details

#wdataObject (readonly)

Returns the value of attribute wdata.



103
104
105
# File 'lib/rworktracker.rb', line 103

def wdata
  @wdata
end

#yamlfileObject

Returns the value of attribute yamlfile.



102
103
104
# File 'lib/rworktracker.rb', line 102

def yamlfile
  @yamlfile
end

Instance Method Details

#addProject(pro) ⇒ Object



129
130
131
# File 'lib/rworktracker.rb', line 129

def addProject(pro)
  @wdata[pro] ||= []
end

#elapsed(pro) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rworktracker.rb', line 161

def elapsed(pro)
  return false unless @wdata[pro]
  total = 0
  @wdata[pro].each  do |e|
    if e['stop']
      total += Time.parse(e['stop']) - Time.parse(e['start'])  
    elsif started?(pro)
      total += Time.now - Time.parse(e['start'])
    end
  end
  return total
end

#loadYamlObject



109
110
111
112
113
114
115
116
117
# File 'lib/rworktracker.rb', line 109

def loadYaml
  begin
  f = YAML.load(File.open(@yamlfile))
  rescue
  f = false
  end
  @wdata = ( f == false) ? Hash.new : f
  #check_status
end

#projectsObject



125
126
127
# File 'lib/rworktracker.rb', line 125

def projects
  @wdata.keys
end

#renewTimeObject



105
106
107
# File 'lib/rworktracker.rb', line 105

def renewTime
  @time = Time.now
end

#start(pro) ⇒ Object



145
146
147
148
149
# File 'lib/rworktracker.rb', line 145

def start(pro)
  return false unless @wdata[pro]
  @wdata[pro] << { 'start' => @time.to_s }
  return true
end

#started?(pro) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rworktracker.rb', line 133

def started?(pro)
  begin
  if @wdata[pro].last['stop'] == nil
    return true
  else
    return false
  end
  rescue 
    return false
  end
end

#stop(pro) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/rworktracker.rb', line 151

def stop(pro)
  return false unless @wdata[pro]
  if @wdata[pro].last.has_key?('start') and !@wdata[pro].last.has_key?('stop') 
    @wdata[pro].last.merge!({ 'stop' => @time.to_s })
    return true
  else
    return false
  end
end

#writeYamlObject



119
120
121
122
123
# File 'lib/rworktracker.rb', line 119

def writeYaml
  File.open(@yamlfile, File::CREAT|File::TRUNC|File::RDWR) do |f|
    f << YAML::dump(@wdata)
  end
end