Class: Portera::TimeslotEnum

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/portera/event.rb

Instance Method Summary collapse

Instance Method Details

#<<(timeslot) ⇒ Object



93
94
95
96
# File 'lib/portera/event.rb', line 93

def <<(timeslot)
  timeslots << timeslot
  self
end

#bestObject

sort timeslot availability by

  1. most participants available

  2. timeslot date/time



139
140
141
142
143
144
# File 'lib/portera/event.rb', line 139

def best
  sort do |a, b|
    comp = b.participants.count <=> a.participants.count
    comp.zero? ? (a.range.begin <=> b.range.begin) : comp
  end
end

#coalescedObject

join intersecting timeslots with identical participants note that this returns a new TimeslotEnum, so that you can call

[+enum.best+]            all timeslots sorted by highest participation
[+enum.coalesced.best+]  joined timeslots sorted by highest participation

Note assumes timeslots are appended in ascending order by range.begin TODO could use some refactoring



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/portera/event.rb', line 109

def coalesced
  last_slot = nil
  coales = inject(self.class.new) do |accum, this_slot|
    if last_slot
      rng       = this_slot.range
      last_rng  = last_slot.range
      if (rng.intersects?(last_rng) || rng.succeeds?(last_rng)) &&
         (this_slot.participants ==  last_slot.participants)
        last_slot = Timeslot.new(
                      (last_rng.begin...rng.end), 
                      this_slot.participants
                    )
      else
        accum << last_slot
        last_slot = this_slot
      end
    else
      last_slot = this_slot
    end
    accum
  end
  if last_slot 
    coales << last_slot
  end
  coales
end

#each(&b) ⇒ Object



98
99
100
# File 'lib/portera/event.rb', line 98

def each(&b)
  timeslots.each(&b)
end