Skip to content

Commit

Permalink
Made some validation methods class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
John Crepezzi committed Aug 25, 2010
1 parent c2633a6 commit 5f97398
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/ice_cube/validation.rb
Expand Up @@ -5,13 +5,13 @@ class Validation
NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }

def adjust(goal, date)
def self.adjust(goal, date)
return goal if goal.utc_offset == date.utc_offset
goal - goal.utc_offset + date.utc_offset
end

# influences by ActiveSupport's to_sentence
def sentence(array)
def self.sentence(array)
case array.length
when 0 ; ''
when 1 ; array[0].to_s
Expand All @@ -20,14 +20,14 @@ def sentence(array)
end
end

def nice_numbers(array)
def self.nice_numbers(array)
array.sort!
sentence array.map { |d| nice_number(d) }
self.sentence array.map { |d| nice_number(d) }
end

private

def nice_number(number)
def self.nice_number(number)
if number == -1
'last'
elsif number < -1
Expand Down
6 changes: 3 additions & 3 deletions lib/ice_cube/validations/day.rb
Expand Up @@ -21,12 +21,12 @@ def closest(date)
days.compact!
# go to the closest distance away, the start of that day
goal = date + days.min * IceCube::ONE_DAY
adjust(goal, date)
self.class.adjust(goal, date)
end

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?
'on ' << self.class.sentence(days_dup.map { |d| Date::DAYNAMES[d] + 's' }) unless days_dup.empty?
end

def to_ical
Expand All @@ -36,4 +36,4 @@ def to_ical

end

end
end
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/day_of_month.rb
Expand Up @@ -34,11 +34,11 @@ def closest(date)
return nil if distances.empty?
# return the start of the proper day
goal = date + distances.min * IceCube::ONE_DAY
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'on the ' << nice_numbers(@days_of_month) << (@days_of_month.size == 1 ? ' day' : ' days') << ' of the month' unless @days_of_month.empty?
'on the ' << self.class.nice_numbers(@days_of_month) << (@days_of_month.size == 1 ? ' day' : ' days') << ' of the month' unless @days_of_month.empty?
end

def to_ical
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/day_of_week.rb
Expand Up @@ -26,15 +26,15 @@ def closest(date)
return nil if !@days_of_week || @days_of_week.empty?
goal = date
while goal += IceCube::ONE_DAY
return adjust(goal, date) if validate(goal)
return self.class.adjust(goal, date) if validate(goal)
end
end

def to_s
representation = ''
representation << 'on the '
representation << @days_of_week.map do |day, occ|
nice_numbers(occ) << ' ' << Date::DAYNAMES[day] << (occ.size != 1 ? 's' : '') unless @days_of_week.empty?
self.class.nice_numbers(occ) << ' ' << Date::DAYNAMES[day] << (occ.size != 1 ? 's' : '') unless @days_of_week.empty?
end.join(' and the ')
representation
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/day_of_year.rb
Expand Up @@ -33,11 +33,11 @@ def closest(date)
return nil if distances.empty?
# return the start of the proper day
goal = date + distances.min * IceCube::ONE_DAY
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'on the ' << nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
'on the ' << self.class.nice_numbers(@days_of_year) << (@days_of_year.size == 1 ? ' day' : ' days') << ' of the year' unless @days_of_year.empty?
end

def to_ical
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/hour_of_day.rb
Expand Up @@ -22,11 +22,11 @@ def closest(date)
# go to the closest distance away, the start of that hour
closest_hour = hours.min
goal = date + IceCube::ONE_HOUR * closest_hour
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'on the ' << nice_numbers(@hours_of_day) << (@hours_of_day.size == 1 ? ' hour' : ' hours') << ' of the day' unless @hours_of_day.empty?
'on the ' << self.class.nice_numbers(@hours_of_day) << (@hours_of_day.size == 1 ? ' hour' : ' hours') << ' of the day' unless @hours_of_day.empty?
end

def to_ical
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/minute_of_hour.rb
Expand Up @@ -22,11 +22,11 @@ def closest(date)
# go to the closest distance away, the beginning of that minute
closest_minute = minutes.min
goal = date + closest_minute * IceCube::ONE_MINUTE
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'on the ' << nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
'on the ' << self.class.nice_numbers(@minutes_of_hour) << (@minutes_of_hour.size == 1 ? ' minute' : ' minutes') << ' of the hour' unless @minutes_of_hour.empty?
end

def to_ical
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/month_of_year.rb
Expand Up @@ -22,11 +22,11 @@ def closest(date)
# go to the closest distance away
goal = date
months.min.times { goal += TimeUtil.days_in_month(goal) * IceCube::ONE_DAY }
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'in ' << sentence(@months_of_year.map { |m| Date::MONTHNAMES[m] }) unless @months_of_year.empty?
'in ' << self.class.sentence(@months_of_year.map { |m| Date::MONTHNAMES[m] }) unless @months_of_year.empty?
end

def to_ical
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/validations/second_of_minute.rb
Expand Up @@ -22,11 +22,11 @@ def closest(date)
# go to the closest distance away
closest_second = seconds.min
goal = date + closest_second
adjust(goal, date)
self.class.adjust(goal, date)
end

def to_s
'on the ' << nice_numbers(@seconds_of_minute) << (@seconds_of_minute.size == 1 ? ' second' : ' seconds') << ' of the minute' unless @seconds_of_minute.empty?
'on the ' << self.class.nice_numbers(@seconds_of_minute) << (@seconds_of_minute.size == 1 ? ' second' : ' seconds') << ' of the minute' unless @seconds_of_minute.empty?
end

def to_ical
Expand Down

0 comments on commit 5f97398

Please sign in to comment.