Class: TimeDistribution::WorkDay

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, *tasks) ⇒ WorkDay

Returns a new instance of WorkDay.

Parameters:

  • date (#to_s)

    Date of this work day in Chronic compatible format.

  • tasks (Array<Task>)

    List of tasks done in this work day. Defaults to an empty list.



20
21
22
23
24
25
26
27
# File 'lib/time_distribution/work_day.rb', line 20

def initialize(date, *tasks)
  @date = Chronic.parse(date)
  @tasks = if tasks.length == 1 && !tasks.first.is_a?(Task)
    TaskList.new(*tasks)
  else
    TaskList.new(tasks)
  end
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/time_distribution/work_day.rb', line 9

def date
  @date
end

#tasksObject (readonly)

Returns the value of attribute tasks.



9
10
11
# File 'lib/time_distribution/work_day.rb', line 9

def tasks
  @tasks
end

Class Method Details

.from_map(map_data) ⇒ Object



11
12
13
14
15
16
# File 'lib/time_distribution/work_day.rb', line 11

def self.from_map(map_data)
  self.new(
    map_data['date'],
    *(map_data['tasks'].map { |t| Task.from_map t })
  )
end

Instance Method Details

#==(other) ⇒ Object



29
# File 'lib/time_distribution/work_day.rb', line 29

def ==(other) (@date == other.date && @tasks == other.tasks) end

#add_task!(task) ⇒ Object

Parameters:

  • task (Task)

    Adds task to the list of tasks completed on this work day.



39
40
41
42
# File 'lib/time_distribution/work_day.rb', line 39

def add_task!(task)
  @tasks << task
  self
end

#time_worked(*subjects) ⇒ Object



44
# File 'lib/time_distribution/work_day.rb', line 44

def time_worked(*subjects) @tasks.time_worked *subjects end

#to_hObject



31
32
33
34
35
36
# File 'lib/time_distribution/work_day.rb', line 31

def to_h
  {
    'date' => @date.strftime('%B %-d, %Y'),
    'tasks' => @tasks.map { |e| e.to_h }
  }
end

#to_hours(*subjects) ⇒ Object



46
# File 'lib/time_distribution/work_day.rb', line 46

def to_hours(*subjects) @tasks.to_hours *subjects end

#to_mdObject



48
49
50
51
52
53
54
# File 'lib/time_distribution/work_day.rb', line 48

def to_md
  (
    @date.strftime('%b %-d, %Y') +
    "\n============================\n" +
    @tasks.to_md
  )
end

#to_ssvObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/time_distribution/work_day.rb', line 56

def to_ssv
  (
    "# #{@date.strftime('%b %-d, %Y')}\n" + (
      if block_given?
        @tasks.to_ssv { |key| yield(key) }
      else
        @tasks.to_ssv
      end
    )
  )
end