Class: Logbook::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/logbook/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePage

Returns a new instance of Page.



5
6
7
8
9
# File 'lib/logbook/page.rb', line 5

def initialize
  @clock = Clock.new
  @entries = []
  @logged_work = {}
end

Instance Attribute Details

#clockObject

Returns the value of attribute clock.



3
4
5
# File 'lib/logbook/page.rb', line 3

def clock
  @clock
end

#entriesObject

Returns the value of attribute entries.



3
4
5
# File 'lib/logbook/page.rb', line 3

def entries
  @entries
end

#logged_workObject

Returns the value of attribute logged_work.



3
4
5
# File 'lib/logbook/page.rb', line 3

def logged_work
  @logged_work
end

#propertiesObject

Returns the value of attribute properties.



3
4
5
# File 'lib/logbook/page.rb', line 3

def properties
  @properties
end

Instance Method Details

#add(entry) ⇒ Object



11
12
13
14
# File 'lib/logbook/page.rb', line 11

def add(entry)
  entries << entry
  clock.tick(entry) { |tracked_entry, duration| logged_work.store(tracked_entry, duration) }
end

#entry_at(line_number) ⇒ Object



16
17
18
# File 'lib/logbook/page.rb', line 16

def entry_at(line_number)
  entries.reverse.find { |entry| entry.line_number <= line_number }
end

#logged_timeObject



20
21
22
# File 'lib/logbook/page.rb', line 20

def logged_time
  logged_work.map { |entry, duration| duration }.reduce(Duration.new(0), &:+)
end

#tasksObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/logbook/page.rb', line 24

def tasks
  entries.reduce({}) do |tasks, entry|
    case entry
    when TaskEntry, TaskDefinition
      if entry.belongs_to_task?
        task = tasks[entry.task_id] ||= Task.new(entry.task_id)

        if logged_work.has_key?(entry)
          task.log_work(entry, logged_work[entry])
        else
          task.add_entry(entry)
        end
      end
    else
    end

    tasks
  end
end