Class: CalendarAssistant::EventSet::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/calendar_assistant/event_set.rb

Instance Attribute Summary

Attributes inherited from Base

#event_repository, #events

Instance Method Summary collapse

Methods inherited from Base

#==, #empty?, #initialize, #new

Constructor Details

This class inherits a constructor from CalendarAssistant::EventSet::Base

Instance Method Details

#[](key) ⇒ Object



61
62
63
# File 'lib/calendar_assistant/event_set.rb', line 61

def [](key)
  events[key] ||= []
end

#[]=(key, value) ⇒ Object



65
66
67
# File 'lib/calendar_assistant/event_set.rb', line 65

def []=(key, value)
  events[key] = value
end

#available_blocks(length: 1) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
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
# File 'lib/calendar_assistant/event_set.rb', line 69

def available_blocks(length: 1)
  event_repository.in_tz do
    dates = events.keys.sort

    # iterate over the days finding free chunks of time
    _avail_time = dates.inject({}) do |avail_time, date|
      avail_time[date] ||= []
      date_events = events[date]

      start_time = date.to_time.to_datetime +
                   BusinessTime::Config.beginning_of_workday.hour.hours +
                   BusinessTime::Config.beginning_of_workday.min.minutes
      end_time = date.to_time.to_datetime +
                 BusinessTime::Config.end_of_workday.hour.hours +
                 BusinessTime::Config.end_of_workday.min.minutes

      date_events.each do |e|
        # ignore events that are outside my business day
        next if Time.before_business_hours?(e.end_time.to_time)
        next if Time.after_business_hours?(e.start_time.to_time)

        if HasDuration.duration_in_seconds(start_time, e.start_time) >= length
          avail_time[date] << AvailableBlock.new(start: start_time, end: e.start_time)
        end
        start_time = [e.end_time, start_time].max
        break if !start_time.during_business_hours?
      end

      if HasDuration.duration_in_seconds(start_time, end_time) >= length
        avail_time[date] << AvailableBlock.new(start: start_time, end: end_time)
      end

      avail_time
    end

    new _avail_time
  end
end

#ensure_keys(keys, only: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/calendar_assistant/event_set.rb', line 48

def ensure_keys(keys, only: false)
  keys.each do |key|
    events[key] = [] unless events.has_key?(key)
  end
  if only
    events.keys.each do |key|
      if !keys.include? key
        events.delete(key)
      end
    end
  end
end

#intersection(other, length: 1) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/calendar_assistant/event_set.rb', line 108

def intersection(other, length: 1)
  set = new({})
  set.ensure_keys(events.keys + other.events.keys)
  set.events.keys.each do |date|
    events[date].each do |event_a|
      other.events[date].each do |event_b|
        if event_a.contains?(event_b.start_time) ||
           event_a.contains?(event_b.end_time - 1) ||
           event_b.contains?(event_a.start_time) ||
           event_b.contains?(event_a.end_time - 1)
          start_time = [event_a.start_time, event_b.start_time].max
          end_time = [event_a.end_time, event_b.end_time].min
          if HasDuration.duration_in_seconds(start_time, end_time) >= length
            set.events[date] << AvailableBlock.new(start: start_time, end: end_time)
          end
        end
      end
    end
  end
  set
end