Sha256: d3ec89106dd8ed1bdc9b387ab26d25adb18b09ef9162ec970c7e6accb1ae92fd

Contents?: true

Size: 1.85 KB

Versions: 16

Compression:

Stored size: 1.85 KB

Contents

module Comee
  module Core
    class Period
      attr_accessor :start, :finish

      def initialize(start, finish)
        @start = start
        @finish = finish
      end

      def ==(other)
        self.class == other.class && @start == other.start && @finish == other.finish
      end

      def <(other)
        instance_of?(other.class) && @start < other.start && @finish < other.finish
      end

      def >(other)
        instance_of?(other.class) && @start > other.start && @finish > other.finish
      end

      def valid?
        finish > start
      end

      def past?
        raise(StandardError, "Period must be valid.") unless valid?

        finish < Date.current
      end

      def current?
        raise(StandardError, "Period must be valid.") unless valid?

        start <= Date.current && finish > Date.current
      end

      def future?
        raise(StandardError, "Period must be valid.") unless valid?

        start > Date.current
      end

      def contains?(period)
        return true if start <= period.start && finish >= period.finish

        false
      end

      def contained_by?(period)
        return true if start >= period.start && finish <= period.finish

        false
      end

      def disjoint?(period)
        @finish < period.start || period.finish < @start
      end

      def overlaps?(period)
        return true if period.start <= @start && @start <= period.finish

        return true if @start <= period.start && period.start <= @finish

        false
      end

      def self.compute_next_valid_period(period1, period2)
        return period2 if period1.disjoint?(period2) && period1 < period2

        return nil if period1 > period2 || period1.contains?(period2)

        period = Period.new(period1.finish.advance(days: 1), period2.finish)
        return nil unless period.valid?

        period
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/utils/comee/core/period.rb
comee_core-0.3.61 app/utils/comee/core/period.rb
comee_core-0.3.60 app/utils/comee/core/period.rb
comee_core-0.3.59 app/utils/comee/core/period.rb
comee_core-0.3.58 app/utils/comee/core/period.rb
comee_core-0.3.57 app/utils/comee/core/period.rb
comee_core-0.3.56 app/utils/comee/core/period.rb
comee_core-0.3.55 app/utils/comee/core/period.rb
comee_core-0.3.54 app/utils/comee/core/period.rb
comee_core-0.3.53 app/utils/comee/core/period.rb
comee_core-0.3.51 app/utils/comee/core/period.rb
comee_core-0.3.50 app/utils/comee/core/period.rb
comee_core-0.3.49 app/utils/comee/core/period.rb
comee_core-0.3.48 app/utils/comee/core/period.rb
comee_core-0.3.47 app/utils/comee/core/period.rb
comee_core-0.3.46 app/utils/comee/core/period.rb