Module: Eventuale::Calendar

Extended by:
ActiveSupport::Concern
Defined in:
lib/eventuale.rb

Overview

Daily scheduling behavior added to Active Record models. rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#advance(slots = 1) ⇒ Object



28
29
30
# File 'lib/eventuale.rb', line 28

def advance(slots = 1)
  advance_by(slots)
end

#advance_by(slots) ⇒ Object



32
33
34
# File 'lib/eventuale.rb', line 32

def advance_by(slots)
  move_to(position - integer_slots!(slots))
end

#append(attributes = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/eventuale.rb', line 71

def append(attributes = {})
  values = attributes.merge(start: start + duration_in_seconds, position: position + 1).compact
  self.class.create!(values).tap do |record|
    record.move_to(record.position)
  end
end

#move_to(destination) ⇒ Object

Moves to a one-based position or inserts at a time on a day.



60
61
62
63
64
65
66
67
68
69
# File 'lib/eventuale.rb', line 60

def move_to(destination)
  case destination
  when Integer
    move_to_position(destination, calendar_day)
  when Time, DateTime, ActiveSupport::TimeWithZone
    move_to_time(destination)
  else
    raise ArgumentError, "destination must be an Integer or a datetime"
  end
end

#postpone(slots = 1) ⇒ Object



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

def postpone(slots = 1)
  postpone_by(slots)
end

#postpone_by(slots) ⇒ Object



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

def postpone_by(slots)
  move_to(position + integer_slots!(slots))
end

#prepend(attributes = {}) ⇒ Object



78
79
80
81
82
# File 'lib/eventuale.rb', line 78

def prepend(attributes = {})
  self.class.create!(attributes.merge(start: start, position: position).compact).tap do |record|
    record.move_to(record.position)
  end
end

#reschedule(start: self.start, duration: self.duration) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eventuale.rb', line 44

def reschedule(start: self.start, duration: self.duration)
  ensure_persisted!
  self.duration = duration

  if start == self.start
    self.class.transaction do
      save!
      normalize_current_day!
    end
    reload
  else
    move_to(start)
  end
end