Module: CalendarAssistant::HasDuration

Included in:
AvailableBlock, Event
Defined in:
lib/calendar_assistant/has_duration.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast_datetime(datetime, time_zone = Time.zone.name) ⇒ Object



7
8
9
10
# File 'lib/calendar_assistant/has_duration.rb', line 7

def self.cast_datetime(datetime, time_zone = Time.zone.name)
  return datetime if datetime.is_a?(Google::Apis::CalendarV3::EventDateTime)
  Google::Apis::CalendarV3::EventDateTime.new(date_time: datetime.in_time_zone(time_zone).to_datetime)
end

.duration_in_seconds(start_time, end_time) ⇒ Object



3
4
5
# File 'lib/calendar_assistant/has_duration.rb', line 3

def self.duration_in_seconds(start_time, end_time)
  (end_time.to_datetime - start_time.to_datetime).days.to_i
end

Instance Method Details

#all_day?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/calendar_assistant/has_duration.rb', line 12

def all_day?
  start.try(:date) || self.end.try(:date)
end

#contains?(time) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/calendar_assistant/has_duration.rb', line 97

def contains?(time)
  start_time <= time && time < end_time
end

#cover?(event) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/calendar_assistant/has_duration.rb', line 36

def cover?(event)
  event.start_date >= start_date && event.end_date <= end_date
end

#current?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/calendar_assistant/has_duration.rb', line 24

def current?
  !(past? || future?)
end

#durationObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/calendar_assistant/has_duration.rb', line 80

def duration
  if all_day?
    days = (end_date - start_date).to_i
    return "#{days}d"
  end

  p = ActiveSupport::Duration.build(duration_in_seconds).parts
  s = []
  s << "#{p[:hours]}h" if p.has_key?(:hours)
  s << "#{p[:minutes]}m" if p.has_key?(:minutes)
  s.join(" ")
end

#duration_in_secondsObject



93
94
95
# File 'lib/calendar_assistant/has_duration.rb', line 93

def duration_in_seconds
  HasDuration.duration_in_seconds start_time, end_time
end

#end_dateObject



72
73
74
75
76
77
78
# File 'lib/calendar_assistant/has_duration.rb', line 72

def end_date
  if all_day?
    self.end.to_date
  else
    end_time.to_date
  end
end

#end_timeObject



64
65
66
67
68
69
70
# File 'lib/calendar_assistant/has_duration.rb', line 64

def end_time
  if all_day?
    end_date.beginning_of_day
  else
    self.end.date_time
  end
end

#future?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/calendar_assistant/has_duration.rb', line 28

def future?
  if all_day?
    start_date > Date.today
  else
    start_time > Time.now
  end
end

#overlaps_end_of?(event) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/calendar_assistant/has_duration.rb', line 44

def overlaps_end_of?(event)
  event.start_date < start_date && event.end_date >= start_date
end

#overlaps_start_of?(event) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/calendar_assistant/has_duration.rb', line 40

def overlaps_start_of?(event)
  event.start_date <= end_date && event.end_date > end_date
end

#past?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/calendar_assistant/has_duration.rb', line 16

def past?
  if all_day?
    Date.today >= end_date
  else
    Time.now >= end_time
  end
end

#start_dateObject



56
57
58
59
60
61
62
# File 'lib/calendar_assistant/has_duration.rb', line 56

def start_date
  if all_day?
    start.to_date
  else
    start_time.to_date
  end
end

#start_timeObject



48
49
50
51
52
53
54
# File 'lib/calendar_assistant/has_duration.rb', line 48

def start_time
  if all_day?
    start_date.beginning_of_day
  else
    start.date_time
  end
end