Class: Mo2tex::DaySchedule

Inherits:
Object
  • Object
show all
Includes:
DateTimeHelper
Defined in:
lib/mo2tex/day_schedule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DateTimeHelper

#datetime_create

Constructor Details

#initialize(day, ln, wd, conf) ⇒ DaySchedule

Returns a new instance of DaySchedule.

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mo2tex/day_schedule.rb', line 76

def initialize(day, ln, wd, conf)
  raise DayIsNotDateTime, "#{day}" unless day.is_a?(DateTime)
  raise ArgumentError, "#{conf}" unless conf.is_a?(Hash)
  @day     = day # should be a DateTime with the d/m/y
  @wday    = wd
  @tstart  = conf[self.wday]['orario'][0]
  @tend    = conf[self.wday]['orario'][1]
  @lbtstart = conf[self.wday]['pausa'][0]
  @lbtend   = conf[self.wday]['pausa'][1]
  @dtstart = datetime_create(self.day, self.tstart)
  @dtend   = datetime_create(self.day, self.tend)
  @lbdtstart = datetime_create(self.day, self.lbtstart)
  @lbdtend   = datetime_create(self.day, self.lbtend)
  @lnumber = ln
  @slots = []
  self.dirty  = false
  calculate_free_slots
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def day
  @day
end

#dirtyObject

Returns the value of attribute dirty.



74
75
76
# File 'lib/mo2tex/day_schedule.rb', line 74

def dirty
  @dirty
end

#dtendObject (readonly)

Returns the value of attribute dtend.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def dtend
  @dtend
end

#dtstartObject (readonly)

Returns the value of attribute dtstart.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def dtstart
  @dtstart
end

#lbdtendObject (readonly)

Returns the value of attribute lbdtend.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def lbdtend
  @lbdtend
end

#lbdtstartObject (readonly)

Returns the value of attribute lbdtstart.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def lbdtstart
  @lbdtstart
end

#lbtendObject (readonly)

Returns the value of attribute lbtend.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def lbtend
  @lbtend
end

#lbtstartObject (readonly)

Returns the value of attribute lbtstart.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def lbtstart
  @lbtstart
end

#lnumberObject (readonly)

Returns the value of attribute lnumber.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def lnumber
  @lnumber
end

#slotsObject (readonly)

Returns the value of attribute slots.



73
74
75
# File 'lib/mo2tex/day_schedule.rb', line 73

def slots
  @slots
end

#tendObject (readonly)

Returns the value of attribute tend.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def tend
  @tend
end

#tstartObject (readonly)

Returns the value of attribute tstart.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def tstart
  @tstart
end

#wdayObject (readonly)

Returns the value of attribute wday.



72
73
74
# File 'lib/mo2tex/day_schedule.rb', line 72

def wday
  @wday
end

Instance Method Details

#<<(event) ⇒ Object



99
100
101
102
103
# File 'lib/mo2tex/day_schedule.rb', line 99

def <<(event)
  return unless this_slot?(event)
  self.slots << event
  self.dirty = true
end

#calculate_free_slotsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mo2tex/day_schedule.rb', line 105

def calculate_free_slots
  if self.slots.empty?
    self.slots << LunchBreak.new(self.day, self.lbdtstart, self.lbdtend)
  else
    bsorted = self.slots.sort { |a, b| a.dtstart <=> b.dtstart }
    tstart = self.dtstart
    bsorted.each do
      |ev|
      self.slots << FreeSlot.new(self.day, tstart, ev.dtstart) if (ev.dtstart.round_to_ymdhm > tstart.round_to_ymdhm)
      tstart = ev.dtend
    end
    self.slots << FreeSlot.new(self.day, tstart, self.dtend) if (tstart.round_to_ymdhm < self.dtend.round_to_ymdhm)
  end
  self.slots.sort! { |a, b| a.dtstart <=> b.dtstart }
  self.dirty = false
  return self.slots
end

#dirty?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/mo2tex/day_schedule.rb', line 95

def dirty?
  return self.dirty
end

#each_slot(&block) ⇒ Object



123
124
125
126
127
# File 'lib/mo2tex/day_schedule.rb', line 123

def each_slot(&block)
  self.calculate_free_slots if self.dirty?
  self.slots.sort! { |a, b| a.dtstart <=> b.dtstart }
  self.slots.each { |slot| yield(slot) } if block_given?
end

#this_slot?(ev) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/mo2tex/day_schedule.rb', line 129

def this_slot?(ev)
  return (ev.dtstart.day_only >= self.dtstart.day_only && ev.dtend.day_only <= self.dtend.day_only)
end