Class: Punch

Inherits:
Object
  • Object
show all
Defined in:
lib/punch.rb,
lib/punch/version.rb,
lib/punch/instance.rb

Defined Under Namespace

Modules: VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Punch

Returns a new instance of Punch.



4
5
6
# File 'lib/punch/instance.rb', line 4

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



2
3
4
# File 'lib/punch/instance.rb', line 2

def project
  @project
end

Class Method Details

.age(project, options = {}) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/punch.rb', line 195

def age(project, options = {})
  raise ":after option makes no sense for aging" if options[:after]
  return nil unless projects.include?(project)
  
  if project.match(%r{_old/\d+$})
    aged_project = project.succ
  else
    aged_project = "#{project}_old/1"
  end
  
  age(aged_project)
  if options[:before]
    data[aged_project], data[project] = data[project].partition { |d|  d['out'] < options[:before] }
    [project, aged_project].each { |proj|  data.delete(proj) if data[proj].empty? }
  else
    data[aged_project] = data.delete(project)
  end
  
  true
end

.clockObject

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
# File 'lib/punch.rb', line 121

def entry(project, options = {})
  raise ArgumentError, 'both :from and :to time are needed' unless options[:from] and options[:to]

  in_options = { :time => options[:from] }
  in_options[:message] = options[:message] if options[:message]
  result = self.in(project, in_options)
  return result unless result

  out(project, :time => options[:to])
end

.delete(project) ⇒ Object



123
124
125
126
# File 'lib/punch.rb', line 123

def delete(project)
  return nil unless data.delete(project)
  true
end

.entry(project, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
# File 'lib/punch.rb', line 111

def entry(project, options = {})
  raise ArgumentError, 'both :from and :to time are needed' unless options[:from] and options[:to]

  in_options = { :time => options[:from] }
  in_options[:message] = options[:message] if options[:message]
  result = self.in(project, in_options)
  return result unless result

  out(project, :time => options[:to])
end

.in(project, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/punch.rb', line 90

def in(project, options = {})
  return false if in?(project)
  data[project] ||= []
  time = time_from_options(options)
  data[project].push({'in' => time})
  log(project, 'punch in', :time => time)
  log(project, options[:message], :time => time) if options[:message]
  true
end

.in?(project) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/punch.rb', line 86

def in?(project)
  status(project) == 'in'
end

.list(*args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/punch.rb', line 128

def list(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  project = args.first
  
  if project
    list_projects = child_projects(project) + [project]
  else
    list_projects = projects
  end
  
  if list_projects.length == 1
    do_list_single(list_projects.first, options)
  else
    list_projects.inject({}) { |hash, project|  hash.merge(project => do_list_single(project, options)) }
  end
end

.loadObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/punch.rb', line 24

def load
  begin
    raw = File.read(File.expand_path('~/.punch.yml'))
    @data = YAML.load(raw) || {}
  rescue Errno::ENOENT
    @data = {}
  end
  
  true
end

.log(project, message, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
163
164
# File 'lib/punch.rb', line 156

def log(project, message, options = {})
  raise ArgumentError, "Message is not an optional argument" if message.is_a?(Hash)
  return false unless in?(project)
  project_data = data[project].last
  project_data['log'] ||= []
  time = time_from_options(options)
  project_data['log'].push "#{message} @ #{time.strftime('%Y-%m-%dT%H:%M:%S%z')}"
  true
end

.out(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/punch.rb', line 100

def out(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  project = args.first
  if project
    return false unless do_out_single(project, options)
  else
    return false unless projects.collect { |project|  do_out_single(project, options) }.any?
  end
  true
end

.out?(project) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/punch.rb', line 82

def out?(project)
  status(project) != 'in'
end

.resetObject



35
36
37
# File 'lib/punch.rb', line 35

def reset
  @data = nil
end

.status(project = nil, options = {}) ⇒ Object



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
# File 'lib/punch.rb', line 45

def status(project = nil, options = {})
  if project.is_a?(Hash)
    options = project
    project = nil
  end
  
  unless project
    stats = {}
    projects.each { |project|  stats[project] = status(project, options) }
    if options[:short]
      stats.reject! { |k, v|  !in?(k) }
      stats = 'out' if stats.empty?
    end
    return stats
  end
  
  project_data = data[project]
  time_data = (project_data || []).last
  
  if time_data
    status = time_data['out'] ? 'out' : 'in'
  end
  
  status, time_data = check_child_status(project, status, time_data)
  
  return status unless options[:full]
  return status if status.nil?
  
  if status == 'in'
    message = (time_data['log'] || []).last
  end
  
  result = { :status => status, :time => time_data[status] }
  result[:message] = message if message
  result
end

.summary(project, options = {}) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/punch.rb', line 166

def summary(project, options = {})
  return unless list_data = list(project, options)
  summary = Hash.new(0)
  
  list_data.each do |time_data|
    if (time_data['log'] || []).empty?
      summary['unspecified'] += ((time_data['out'] || Time.now) - time_data['in']).to_i
      next
    end
    
    log = time_data['log'].collect do |l|
      msg, time = l.split('@')
      msg = msg.strip
      msg = 'unspecified' if msg == 'punch in'
      { :msg => msg, :time => Time.parse(time) }
    end
    
    log << { :msg => 'punch out', :time => Time.now } unless log.last[:msg] == 'punch out'
    
    log.each_cons(2) do |a, b|
      summary[a[:msg]] += (b[:time] - a[:time]).to_i
    end
  end
  
  summary.reject! { |k, v|  v == 0 }
  summary.each { |k, v|  summary[k] = v.elapsed_time } if options[:format]
  summary
end

.total(*args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/punch.rb', line 145

def total(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  list_data = list(*args)
  if list_data.is_a?(Hash)
    list_data.inject({}) { |hash, (project, project_data)|  hash.merge(project => do_total_time(project_data, options)) }
  else
    return nil unless list_data
    do_total_time(list_data, options)
  end
end

.writeObject



39
40
41
42
43
# File 'lib/punch.rb', line 39

def write
  File.open(File.expand_path('~/.punch.yml'), 'w') do |file|
    file.puts data.to_yaml
  end
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
# File 'lib/punch/instance.rb', line 53

def ==(other)
  project == other.project
end

#age(options = {}) ⇒ Object



49
50
51
# File 'lib/punch/instance.rb', line 49

def age(options = {})
  self.class.age(project, options)
end

#child_projectsObject Also known as: children



57
58
59
# File 'lib/punch/instance.rb', line 57

def child_projects
  Punch.send(:child_projects, project).collect { |proj|  Punch.new(proj) }
end

#entry(options = {}) ⇒ Object Also known as: clock



28
29
30
# File 'lib/punch/instance.rb', line 28

def entry(options = {})
  self.class.entry(project, options)
end

#in(options = {}) ⇒ Object



20
21
22
# File 'lib/punch/instance.rb', line 20

def in(options = {})
  self.class.in(project, options)
end

#in?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/punch/instance.rb', line 16

def in?
  self.class.in?(project)
end

#list(options = {}) ⇒ Object



33
34
35
# File 'lib/punch/instance.rb', line 33

def list(options = {})
  self.class.list(project, options)
end

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



41
42
43
# File 'lib/punch/instance.rb', line 41

def log(message, options = {})
  self.class.log(project, message, options)
end

#out(options = {}) ⇒ Object



24
25
26
# File 'lib/punch/instance.rb', line 24

def out(options = {})
  self.class.out(project, options)
end

#out?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/punch/instance.rb', line 12

def out?
  self.class.out?(project)
end

#status(options = {}) ⇒ Object



8
9
10
# File 'lib/punch/instance.rb', line 8

def status(options = {})
  self.class.status(project, options)
end

#summary(options = {}) ⇒ Object



45
46
47
# File 'lib/punch/instance.rb', line 45

def summary(options = {})
  self.class.summary(project, options)
end

#total(options = {}) ⇒ Object



37
38
39
# File 'lib/punch/instance.rb', line 37

def total(options = {})
  self.class.total(project, options)
end