Sha256: e62c65fd269698d0abde834fff994663b8764dc61a469c35b18c502c5100a0e3

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'active_support/core_ext/string/inflections'

module Repeatable
  class Schedule
    def initialize(args)
      @expression = build_expression(args)
    end

    def occurrences(start_date, end_date)
      start_date = Date(start_date)
      end_date = Date(end_date)
      (start_date..end_date).select { |date| include?(date) }
    end

    def next_occurrence(start_date = Date.today)
      date = Date(start_date)
      until include?(date)
        date = date.next_day
      end
      date
    end

    def include?(date = Date.today)
      date = Date(date)
      expression.include?(date)
    end

    private

    attr_reader :expression

    def build_expression(hash)
      if hash.length != 1
        fail "Invalid expression: '#{hash}' should have single key and value"
      else
        expression_for(*hash.first)
      end
    end

    def expression_for(key, value)
      klass = "repeatable/expression/#{key}".classify.safe_constantize
      case klass
      when nil
        fail "Unknown mapping: Can't map key '#{key.inspect}' to an expression class"
      when Repeatable::Expression::Set
        args = value.map { |hash| build_expression(hash) }
        klass.new(*args)
      else
        klass.new(value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
repeatable-0.1.0 lib/repeatable/schedule.rb