Class: Worklog::Decorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/worklog/decorator.rb

Defined Under Namespace

Classes: Person

Instance Method Summary collapse

Constructor Details

#initialize(sheet) ⇒ Decorator

Returns a new instance of Decorator.



8
9
10
11
# File 'lib/worklog/decorator.rb', line 8

def initialize(sheet)
  @sheet = sheet
  super
end

Instance Method Details

#prev_monthObject



38
39
40
41
42
# File 'lib/worklog/decorator.rb', line 38

def prev_month
  till = start_of_month(Date.today) - 1
  from = start_of_month(till)
  tracks(-> (x,_) { x >= from && x <= till })
end

#prev_weekObject

FIXME: incorrect for sunday 1



52
53
54
55
56
57
# File 'lib/worklog/decorator.rb', line 52

def prev_week
  today = Date.today
  from = today - 7 - (today.wday - 1) % 7
  till = from + 6
  tracks(-> (x,_) { x >= from && x <= till })
end

#this_monthObject

TODO: this year, previous year?



34
35
36
# File 'lib/worklog/decorator.rb', line 34

def this_month
  tracks(-> (x,_) { x >= start_of_month(Date.today) })
end

#this_weekObject

FIXME: incorrect for sunday 1



45
46
47
48
49
# File 'lib/worklog/decorator.rb', line 45

def this_week
  today = Date.today
  from = today - (today.wday - 1) % 7
  tracks(-> (x,_) { x >= from })
end

#tracks(filter = ->(x,_){true}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/worklog/decorator.rb', line 15

def tracks(filter = ->(x,_){true})
  items = super().select(&filter)
    .each_with_object([]) do |(date, tary), out|
      out << Person.new(date,
        tary.map(&:spent).inject(:+),
        tary.map(&:task).join(?\n),
        tary.map(&:reward).inject(:+)
      )
    end.sort!{|a, b| a.date <=> b.date}

  {}.tap do |out|
    out[:title] = title
    out[:items] = items
    out[:spent] = items.map(&:spent).inject(:+)
    out[:total] = items.map(&:reward).inject(:+)
  end
end