1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
module IceCube
module Validations::DayOfYear
def day_of_year(*days)
days.flatten.each do |day|
unless day.is_a?(Integer)
raise ArgumentError, "expecting Integer value for day, got #{day.inspect}"
end
validations_for(:day_of_year) << Validation.new(day)
end
clobber_base_validations(:month, :day, :wday)
self
end
class Validation
attr_reader :day
def initialize(day)
@day = day
end
def type
:day
end
def dst_adjust?
true
end
def validate(step_time, start_time)
days_in_year = TimeUtil.days_in_year(step_time)
yday = day < 0 ? day + days_in_year + 1 : day
offset = yday - step_time.yday
offset >= 0 ? offset : offset + days_in_year
end
def build_s(builder)
builder.piece(:day_of_year) << StringBuilder.nice_number(day)
end
def build_hash(builder)
builder.validations_array(:day_of_year) << day
end
def build_ical(builder)
builder['BYYEARDAY'] << day
end
StringBuilder.register_formatter(:day_of_year) do |entries|
str = StringBuilder.sentence(entries)
sentence = IceCube::I18n.t('ice_cube.days_of_year', count: entries.size, segments: str)
IceCube::I18n.t('ice_cube.on', sentence: sentence)
end
end
end
end
|