Class: IceCube::Validations::DayOfWeek::Validation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(day, occ) ⇒ Validation

Returns a new instance of Validation.



45
46
47
48
# File 'lib/ice_cube/validations/day_of_week.rb', line 45

def initialize(day, occ)
  @day = day
  @occ = occ
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



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

def day
  @day
end

#occObject (readonly)

Returns the value of attribute occ.



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

def occ
  @occ
end

Instance Method Details

#build_hash(builder) ⇒ Object



39
40
41
42
43
# File 'lib/ice_cube/validations/day_of_week.rb', line 39

def build_hash(builder)
  builder.validations[:day_of_week] ||= {}
  arr = (builder.validations[:day_of_week][day] ||= [])
  arr << occ
end

#build_ical(builder) ⇒ Object



32
33
34
35
36
37
# File 'lib/ice_cube/validations/day_of_week.rb', line 32

def build_ical(builder)
  ical_day = IcalBuilder.fixnum_to_ical_day(day)
  # Delete any with this day and no occ first
  builder['BYDAY'].delete_if { |d| d == ical_day }
  builder['BYDAY'] << "#{occ}#{ical_day}"
end

#build_s(builder) ⇒ Object



28
29
30
# File 'lib/ice_cube/validations/day_of_week.rb', line 28

def build_s(builder)
  builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}"
end

#typeObject



24
25
26
# File 'lib/ice_cube/validations/day_of_week.rb', line 24

def type
  :day
end

#validate(time, schedule) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ice_cube/validations/day_of_week.rb', line 50

def validate(time, schedule)
  # count the days to the weekday
  sum = day >= time.wday ? day - time.wday : 7 - time.wday + day
  wrapper = TimeUtil::TimeWrapper.new(time)
  wrapper.add :day, sum
  # and then count the week until a viable occ
  loop do
    which_occ, num_occ = TimeUtil.which_occurrence_in_month(wrapper.to_time, day)
    this_occ = occ < 0 ? num_occ + occ + 1 : occ
    break if which_occ == this_occ
    sum += 7
    wrapper.add :day, 7 # one week
  end
  sum
end