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
|
module IceCube
module Validations::Count
# Value reader for limit
def occurrence_count
(arr = @validations[:count]) && (val = arr[0]) && val.count
end
def count(max)
unless max.nil? || max.is_a?(Integer)
raise ArgumentError, "Expecting Integer or nil value for count, got #{max.inspect}"
end
replace_validations_for(:count, max && [Validation.new(max, self)])
self
end
class Validation
attr_reader :rule, :count
def initialize(count, rule)
@count = count
@rule = rule
end
def type
:limit
end
def dst_adjust?
false
end
def validate(time, start_time)
raise CountExceeded if rule.uses && rule.uses >= count
end
def build_s(builder)
builder.piece(:count) << count
end
def build_hash(builder)
builder[:count] = count
end
def build_ical(builder)
builder['COUNT'] << count
end
StringBuilder.register_formatter(:count) do |segments|
count = segments.first
IceCube::I18n.t('ice_cube.times', count: count)
end
end
end
end
|