Class: IceCube::Validations::WeeklyInterval::Validation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, week_start) ⇒ Validation

Returns a new instance of Validation.



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

def initialize(interval, week_start)
  @interval = interval
  @week_start = week_start
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



15
16
17
# File 'lib/ice_cube/validations/weekly_interval.rb', line 15

def interval
  @interval
end

#week_startObject (readonly)

Returns the value of attribute week_start.



15
16
17
# File 'lib/ice_cube/validations/weekly_interval.rb', line 15

def week_start
  @week_start
end

Instance Method Details

#build_hash(builder) ⇒ Object



33
34
35
# File 'lib/ice_cube/validations/weekly_interval.rb', line 33

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

#build_ical(builder) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ice_cube/validations/weekly_interval.rb', line 25

def build_ical(builder)
  builder['FREQ'] << 'WEEKLY'
  unless interval == 1
    builder['INTERVAL'] << interval
    builder['WKST'] << TimeUtil.week_start(week_start)
  end
end

#build_s(builder) ⇒ Object



21
22
23
# File 'lib/ice_cube/validations/weekly_interval.rb', line 21

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

#typeObject



17
18
19
# File 'lib/ice_cube/validations/weekly_interval.rb', line 17

def type
  :day
end

#validate(time, schedule) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ice_cube/validations/weekly_interval.rb', line 42

def validate(time, schedule)
  raise ZeroInterval if interval == 0
  date = Date.new(time.year, time.month, time.day)
  st = schedule.start_time
  start_date = Date.new(st.year, st.month, st.day)
  weeks = (
    (date - TimeUtil.normalize_weekday(date.wday, week_start)) - 
    (start_date - TimeUtil.normalize_weekday(start_date.wday, week_start))
  ) / 7
  unless weeks % interval == 0
    (interval - (weeks % interval)) * 7
  end
end