Class: Punch
- Inherits:
-
Object
- Object
- Punch
- Defined in:
- lib/punch.rb,
lib/punch/version.rb,
lib/punch/instance.rb
Defined Under Namespace
Modules: VERSION
Instance Attribute Summary collapse
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Class Method Summary collapse
- .age(project, options = {}) ⇒ Object
- .clock ⇒ Object
- .delete(project) ⇒ Object
- .entry(project, options = {}) ⇒ Object
- .in(project, options = {}) ⇒ Object
- .in?(project) ⇒ Boolean
- .list(*args) ⇒ Object
- .load ⇒ Object
- .log(project, message, options = {}) ⇒ Object
- .out(*args) ⇒ Object
- .out?(project) ⇒ Boolean
- .reset ⇒ Object
- .status(project = nil, options = {}) ⇒ Object
- .summary(project, options = {}) ⇒ Object
- .total(*args) ⇒ Object
- .write ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #age(options = {}) ⇒ Object
- #child_projects ⇒ Object (also: #children)
- #entry(options = {}) ⇒ Object (also: #clock)
- #in(options = {}) ⇒ Object
- #in? ⇒ Boolean
-
#initialize(project) ⇒ Punch
constructor
A new instance of Punch.
- #list(options = {}) ⇒ Object
- #log(message, options = {}) ⇒ Object
- #out(options = {}) ⇒ Object
- #out? ⇒ Boolean
- #status(options = {}) ⇒ Object
- #summary(options = {}) ⇒ Object
- #total(options = {}) ⇒ Object
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
#project ⇒ Object (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, = {}) raise ":after option makes no sense for aging" if [: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 [:before] data[aged_project], data[project] = data[project].partition { |d| d['out'] < [:before] } [project, aged_project].each { |proj| data.delete(proj) if data[proj].empty? } else data[aged_project] = data.delete(project) end true end |
.clock ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/punch.rb', line 121 def entry(project, = {}) raise ArgumentError, 'both :from and :to time are needed' unless [:from] and [:to] = { :time => [:from] } [:message] = [:message] if [:message] result = self.in(project, ) return result unless result out(project, :time => [: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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/punch.rb', line 111 def entry(project, = {}) raise ArgumentError, 'both :from and :to time are needed' unless [:from] and [:to] = { :time => [:from] } [:message] = [:message] if [:message] result = self.in(project, ) return result unless result out(project, :time => [:to]) end |
.in(project, options = {}) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/punch.rb', line 90 def in(project, = {}) return false if in?(project) data[project] ||= [] time = () data[project].push({'in' => time}) log(project, 'punch in', :time => time) log(project, [:message], :time => time) if [:message] true end |
.in?(project) ⇒ 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) = 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, ) else list_projects.inject({}) { |hash, project| hash.merge(project => do_list_single(project, )) } end end |
.load ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/punch.rb', line 24 def load begin raw = File.read(File.('~/.punch.yml')) @data = YAML.load(raw) || {} rescue Errno::ENOENT @data = {} end true end |
.log(project, message, options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/punch.rb', line 156 def log(project, , = {}) raise ArgumentError, "Message is not an optional argument" if .is_a?(Hash) return false unless in?(project) project_data = data[project].last project_data['log'] ||= [] time = () project_data['log'].push "#{} @ #{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) = args.last.is_a?(Hash) ? args.pop : {} project = args.first if project return false unless do_out_single(project, ) else return false unless projects.collect { |project| do_out_single(project, ) }.any? end true end |
.out?(project) ⇒ Boolean
82 83 84 |
# File 'lib/punch.rb', line 82 def out?(project) status(project) != 'in' end |
.reset ⇒ Object
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, = {}) if project.is_a?(Hash) = project project = nil end unless project stats = {} projects.each { |project| stats[project] = status(project, ) } if [: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 [:full] return status if status.nil? if status == 'in' = (time_data['log'] || []).last end result = { :status => status, :time => time_data[status] } result[:message] = if 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, = {}) return unless list_data = list(project, ) 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 [: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) = 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, )) } else return nil unless list_data do_total_time(list_data, ) end end |
.write ⇒ Object
39 40 41 42 43 |
# File 'lib/punch.rb', line 39 def write File.open(File.('~/.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( = {}) self.class.age(project, ) end |
#child_projects ⇒ Object 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( = {}) self.class.entry(project, ) end |
#in(options = {}) ⇒ Object
20 21 22 |
# File 'lib/punch/instance.rb', line 20 def in( = {}) self.class.in(project, ) end |
#in? ⇒ 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( = {}) self.class.list(project, ) end |
#log(message, options = {}) ⇒ Object
41 42 43 |
# File 'lib/punch/instance.rb', line 41 def log(, = {}) self.class.log(project, , ) end |
#out(options = {}) ⇒ Object
24 25 26 |
# File 'lib/punch/instance.rb', line 24 def out( = {}) self.class.out(project, ) end |
#out? ⇒ 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( = {}) self.class.status(project, ) end |
#summary(options = {}) ⇒ Object
45 46 47 |
# File 'lib/punch/instance.rb', line 45 def summary( = {}) self.class.summary(project, ) end |
#total(options = {}) ⇒ Object
37 38 39 |
# File 'lib/punch/instance.rb', line 37 def total( = {}) self.class.total(project, ) end |