Class: AsciiTracker::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/asciitracker/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



6
7
8
9
10
# File 'lib/asciitracker/model.rb', line 6

def initialize 
  @records = [ ]
  @by_date = { }
  @projects = Hash.new { |hash, project_id| hash[project_id] = [] }
end

Instance Attribute Details

#projectsObject (readonly)

Returns the value of attribute projects.



4
5
6
# File 'lib/asciitracker/model.rb', line 4

def projects
  @projects
end

#recordsObject (readonly)

Returns the value of attribute records.



4
5
6
# File 'lib/asciitracker/model.rb', line 4

def records
  @records
end

Instance Method Details

#add_record(rec, date = Date.today) ⇒ Object



16
17
18
19
20
# File 'lib/asciitracker/model.rb', line 16

def add_record rec, date = Date.today
  @records.push(rec)
  (@by_date[date] ||= []).push(rec)
  rec
end

#by_date(date) ⇒ Object



12
13
14
# File 'lib/asciitracker/model.rb', line 12

def by_date(date)
  (@by_date[date] ||= [])
end

#find_best_cover(rec, date = Date.today) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/asciitracker/model.rb', line 26

def find_best_cover rec, date = Date.today
  by_date(date).inject(nil) do |best, test| 
    if test.respond_to?(:covers?) # spans never cover
      if test.covers?(rec) && (best.nil? || (best.covers? test))
        best = test 
      end
    end
    best
  end
end

#find_overlaps(rec, date = Date.today) ⇒ Object



22
23
24
# File 'lib/asciitracker/model.rb', line 22

def find_overlaps rec, date = Date.today
  by_date(date).find_all { |a| a.overlaps?(rec) rescue false }
end