Class: IceCube::Validations::DailyInterval::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_cube/validations/daily_interval.rb

Overview

A validation for checking to make sure that a time is inside of a certain DailyInterval

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval) ⇒ Validation

Returns a new instance of Validation.



18
19
20
# File 'lib/ice_cube/validations/daily_interval.rb', line 18

def initialize(interval)
  @interval = interval
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



16
17
18
# File 'lib/ice_cube/validations/daily_interval.rb', line 16

def interval
  @interval
end

Instance Method Details

#build_hash(builder) ⇒ Object



26
27
28
# File 'lib/ice_cube/validations/daily_interval.rb', line 26

def build_hash(builder)
  builder[:interval] = interval
end

#build_ical(builder) ⇒ Object



30
31
32
33
34
35
# File 'lib/ice_cube/validations/daily_interval.rb', line 30

def build_ical(builder)
  builder['FREQ'] << 'DAILY'
  unless interval == 1
    builder['INTERVAL'] << interval
  end
end

#build_s(builder) ⇒ Object



22
23
24
# File 'lib/ice_cube/validations/daily_interval.rb', line 22

def build_s(builder)
  builder.base = interval == 1 ? 'Daily' : "Every #{interval} days"
end

#typeObject



37
38
39
# File 'lib/ice_cube/validations/daily_interval.rb', line 37

def type
  :day
end

#validate(time, schedule) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
# File 'lib/ice_cube/validations/daily_interval.rb', line 41

def validate(time, schedule)
  raise ZeroInterval if interval == 0
  time_date = Date.new(time.year, time.month, time.day)
  start_date = Date.new(schedule.start_time.year, schedule.start_time.month, schedule.start_time.day)
  days = time_date - start_date
  unless days % interval === 0
    interval - (days % interval)
  end
end