Class: IceCube::DayValidation

Inherits:
Validation show all
Defined in:
lib/ice_cube/validations/day.rb

Constant Summary

Constants inherited from Validation

Validation::NUMBER_SUFFIX

Instance Method Summary collapse

Methods inherited from Validation

#adjust, #nice_numbers, #sentence

Constructor Details

#initialize(rule) ⇒ DayValidation

Returns a new instance of DayValidation.



5
6
7
8
# File 'lib/ice_cube/validations/day.rb', line 5

def initialize(rule)
  @days = rule.validations[:day]
  @rule = rule
end

Instance Method Details

#closest(date) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ice_cube/validations/day.rb', line 15

def closest(date)
  return nil if !@days || @days.empty?
  # turn days into distances
  days = @days.map do |d| 
    d > date.wday ? (d - date.wday) : (7 - date.wday + d)
  end
  days.compact!
  # go to the closest distance away, the start of that day
  goal = date + days.min * IceCube::ONE_DAY
  adjust(goal, date)
end

#to_icalObject



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

def to_ical
  days_dup = (@days - @rule.validations[:day_of_week].keys if @rule.validations[:day_of_week]) || @days # don't list twice
  'BYDAY=' << days_dup.map { |d| IceCube::ICAL_DAYS[d] }.join(',') unless days_dup.empty?
end

#to_sObject



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

def to_s
  days_dup = (@days - @rule.validations[:day_of_week].keys if @rule.validations[:day_of_week]) || @days # don't list twice
  'on ' << sentence(days_dup.map { |d| Date::DAYNAMES[d] + 's' }) unless days_dup.empty?
end

#validate(date) ⇒ Object



10
11
12
13
# File 'lib/ice_cube/validations/day.rb', line 10

def validate(date)
  return true if !@days || @days.empty?
  @days.include?(date.wday)
end