Class: Worklog::Entities::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/worklog/entities/sheet.rb

Overview

This class represent sheet of tracks collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSheet

Returns a new instance of Sheet.



20
21
22
23
24
25
26
# File 'lib/worklog/entities/sheet.rb', line 20

def initialize
  @tracks = {}
  @date_format = "%Y-%m-%d"
  @hourly_rate = 0
  @title =  "Unspecified"
  @author = "Unspecified"
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



14
15
16
# File 'lib/worklog/entities/sheet.rb', line 14

def author
  @author
end

#date_formatObject

Returns the value of attribute date_format.



16
17
18
# File 'lib/worklog/entities/sheet.rb', line 16

def date_format
  @date_format
end

#hourly_rateObject

Returns the value of attribute hourly_rate.



17
18
19
# File 'lib/worklog/entities/sheet.rb', line 17

def hourly_rate
  @hourly_rate
end

#sourceObject

Returns the value of attribute source.



15
16
17
# File 'lib/worklog/entities/sheet.rb', line 15

def source
  @source
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/worklog/entities/sheet.rb', line 13

def title
  @title
end

#tracksObject (readonly)

Returns the value of attribute tracks.



12
13
14
# File 'lib/worklog/entities/sheet.rb', line 12

def tracks
  @tracks
end

Instance Method Details

#merge!(sheet) ⇒ Object

The method merges tracks from sheet passed as param



57
58
59
60
61
62
63
# File 'lib/worklog/entities/sheet.rb', line 57

def merge!(sheet)
  @tracks.merge!(sheet.tracks) do |date, these_tracks, merge_tracks|
    puts "Warning! Date duplication detected in '#{@source}' and '#{sheet.source}'"
    puts "Please, check these files manually for '#{date.to_s}'"
    these_tracks + merge_tracks
  end
end

#rewardObject



71
72
73
74
75
# File 'lib/worklog/entities/sheet.rb', line 71

def reward
  @tracks.inject(0) {|sum, (d, t)|
    sum + t.map(&:reward).inject(:+)          
  }
end

#spentObject



65
66
67
68
69
# File 'lib/worklog/entities/sheet.rb', line 65

def spent
  @tracks.inject(0) {|sum, (d, t)|
    sum + t.map(&:spent).inject(:+)
  }
end

#spent_for(date) ⇒ Object

The method calculates sum(spent) for provided date

Parameters:

  • date (Date)

    the date for calculation



52
53
54
# File 'lib/worklog/entities/sheet.rb', line 52

def spent_for(date)
  @tracks[date].map(&:spent).inject(:+)
end

#track(date, spent:, task:, rate: nil) ⇒ Track

This method adds new track to the sheet It will pring warning, when total spent for one date exceeds 24h

Parameters:

  • date (Date)

    the track date

  • spent (Number)

    the track spent time in minutes

  • task (String)

    the tracking task description

  • rate (Number) (defaults to: nil)

    the track hourly rate, optional; provide it when the track hourly rate differs from specified for whole sheet (weekends, overtime, or sth)

Returns:

  • (Track)

    the track registered



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/worklog/entities/sheet.rb', line 35

def track(date, spent:, task:, rate: nil)
  @tracks[date] ||= []
  @tracks[date] << Track.new(
    date,
    spent: spent,
    task: task,
    rate: rate || @hourly_rate
  )
  if @tracks[date].size > 1 && spent_for(date) > Track::MAX_MINUTES
    puts "Warning! Total spent for #{date} greater than 24 hours"
  end
  @tracks[date].last
end