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.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/ice_cube/validations/day_of_week.rb', line 17

def initialize(day, occ)
  raise ArgumentError, "Integer occurrence value required" unless occ.is_a?(Integer)
  raise ArgumentError, "Invalid day_of_week occurrence: #{occ.inspect}" if occ.zero? || occ.abs > 5

  @day = day
  @occ = occ
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



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

def day
  @day
end

#occObject (readonly)

Returns the value of attribute occ.



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

def occ
  @occ
end

Instance Method Details

#build_hash(builder) ⇒ Object



55
56
57
58
59
# File 'lib/ice_cube/validations/day_of_week.rb', line 55

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

#build_ical(builder) ⇒ Object



61
62
63
64
65
66
# File 'lib/ice_cube/validations/day_of_week.rb', line 61

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



47
48
49
50
51
52
53
# File 'lib/ice_cube/validations/day_of_week.rb', line 47

def build_s(builder)
  builder.piece(:day_of_week) << IceCube::I18n.t(
    "ice_cube.days_of_week",
    segments: StringBuilder.nice_number(occ),
    day: IceCube::I18n.t("date.day_names")[day]
  )
end

#dst_adjust?Boolean

Returns:

  • (Boolean)


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

def dst_adjust?
  true
end

#typeObject



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

def type
  :day
end

#validate(step_time, start_time) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ice_cube/validations/day_of_week.rb', line 33

def validate(step_time, start_time)
  wday = step_time.wday
  offset = (day < wday) ? (7 - wday + day) : (day - wday)
  wrapper = TimeUtil::TimeWrapper.new(step_time)
  wrapper.add :day, offset
  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 offset if which_occ == this_occ
    wrapper.add :day, 7
    offset += 7
  end
end