Sha256: 1f110665345568e86a84f3e76e739fd637f749258d02ebf12287f3c899c3359f

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module IceCubeEx
  module Validations
    module DayCycleInterval
      attr_accessor :rule

      def cycle(cycle, repeat)
        @interval = 1
        @repeat   = normalize(repeat)
        @cycle    = normalize(cycle)

        unless @repeat < @cycle
          raise ArgumentError, 'cycle has to be a value higher than repeat'
        end

        @acceptable_cycle_percentage = ((@repeat.to_f / @cycle.to_f) * 100).to_i
        replace_validations_for \
          :interval, [Validation.new(@interval, @cycle, @repeat)]
        clobber_base_validations(:wday, :day)
        self
      end

      class Validation < IceCube::Validations::DailyInterval::Validation
        attr_reader :cycle, :repeat

        def initialize(interval, cycle, repeat)
          super(interval)
          @cycle    = cycle
          @repeat   = repeat
        end

        def build_s(builder)
          builder.base = "Every #{cycle} days, repeat #{repeat} times"
        end

        def build_hash(builder)
          builder[:interval] = interval
          builder[:cycle]    = cycle
          builder[:repeat]   = repeat
        end

        def build_ical(builder)
          builder['FREQ']   << 'DAY_CYCLE (CUSTOM RULE)'
          builder['CYCLE']  << cycle
          builder['REPEAT'] << repeat
        end
      end

    private

      def normalize(arg)
        arg.to_i.tap do |val|
          unless val > 1
            raise ArgumentError, "'#{arg}' is not a valid argument. " \
                                 "Please pass an integer higher than 1."
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ice_cube_ex-0.1.0 lib/ice_cube_ex/validations/day_cycle_interval.rb