Class: AsciiTracker::Slot

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

Constant Summary

Constants inherited from Record

Record::Defaults

Instance Attribute Summary collapse

Attributes inherited from Record

#date, #desc, #span

Instance Method Summary collapse

Methods inherited from Record

hours_to_dhm

Constructor Details

#initialize(values = {}) ⇒ Slot

supports Record values keys plus: :start and :end for interval definition



11
12
13
14
15
16
17
18
19
20
# File 'lib/asciitracker/slot.rb', line 11

def initialize values = {}
  values = Defaults.merge(values)
  @t_start = HHMM.new(values[:start])
  @t_end = HHMM.new(values[:end])

  @duration = (@t_end - @t_start)
  super values.merge(:span => @duration.to_f)

  @interrupts = []
end

Instance Attribute Details

#t_endObject (readonly)

Returns the value of attribute t_end.



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

def t_end
  @t_end
end

#t_startObject (readonly)

Returns the value of attribute t_start.



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

def t_start
  @t_start
end

Instance Method Details

#_24(a, b) ⇒ Object

—–

-+

—————-

|

| <– overlaping

——

|

——

|

———-

-+

self: [———]

[---]                       -+
   [---]                     | <-- not overlaping
                 [----]      |
                   [-----]  -+


44
45
46
# File 'lib/asciitracker/slot.rb', line 44

def _24(a,b)
  [a.to_f, b < a ? b.to_f + 24 : b.to_f]
end

#add_interrupt(slot_or_span) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/asciitracker/slot.rb', line 80

def add_interrupt slot_or_span
  slr = slot_or_span # shortcut
  unless covers? slr
    raise Exception, "interrupt not covered! #{slr}"
  end

  unless slr.span <= span
    raise Exception, "'#{self}' overload(#{span}): #{slr}"
  end
  #raise Exception, "overload: #{slr}" unless slr.span <= span

  # new interrupts may not overlap with existing ones!
  if slr.respond_to?(:t_start) 
    #raise Exception, "overlap: #{slr}" if @interrupts.any? do |i| 
    #    slr.overlaps? i
    #end
    if @interrupts.any? { |rec| slr.overlaps? rec }
      raise Exception, "overlap: #{slr}"
    end
  end

  @interrupts.push(slr)

  # subtract interrupts from span time
  #dt_lost = @interrupts.inject(0.0) { |sum, rec| sum + rec.span }
  dt_lost = @interrupts.inject(0.0) { |sum, rec| sum + (rec.gross_length rescue rec.span) }
  @span = gross_length - dt_lost
end

#covers?(slot_or_span) ⇒ Boolean

checks for a pure technical fit which does not take into account the already existing interruptions!

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/asciitracker/slot.rb', line 65

def covers? slot_or_span
  slr = slot_or_span # shortcut for shorter lines below
  begin
    a, b = _24(t_start, t_end)
    c, d = _24(slr.t_start, slr.t_end)
    if d < a
      #puts "..upgrade: #{a}, #{b} -> #{c}, #{d}"
      c = c + 24; d = d + 24
    end
    a <= c && d <= b
  rescue NoMethodError # not a slot?
    slr.kind_of?(Record) and slr.span <= gross_length
  end
end

#gross_lengthObject

gross length is full slot length without interruptions being subtracted



26
27
28
# File 'lib/asciitracker/slot.rb', line 26

def gross_length
  @duration.to_f
end

#interruptsObject

returns copy only, not suited to add/delelte interrupts



23
# File 'lib/asciitracker/slot.rb', line 23

def interrupts; @interrupts.clone end

#overlaps?(slr) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/asciitracker/slot.rb', line 48

def overlaps? slr
  return false unless slr.respond_to?(:t_end)
  a, b = _24(t_start, t_end)
  c, d = _24(slr.t_start, slr.t_end)
  #puts "..a, b, c, d: #{a}, #{b} -> #{c}, #{d}"
  if d < a
    #puts "..upgrade: #{a}, #{b} -> #{c}, #{d}"
    c = c + 24; d = d + 24
  end
  #puts "..a, b, c, d: #{a}, #{b} -> #{c}, #{d} | #{c < b && a < d}"
  c < b && a < d
  #!slr.nil? && !(slr.t_end <= t_start || t_end <= slr.t_start)
  #not(slr.t_end <= t_start || t_end <= slr.t_start)
end

#to_sObject



6
# File 'lib/asciitracker/slot.rb', line 6

def to_s; "#{date} #{t_start}-#{t_end}  #{desc}" end