Sha256: dc05d32799bd83eee3fc8fe910a9aad00ee1a33791e51e66ad005ce24efb6b62

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 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 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.new(period1.finish.advance(days: 1), period2.finish)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
comee_core-0.1.31 app/utils/comee/core/period.rb
comee_core-0.1.30 app/utils/comee/core/period.rb
comee_core-0.1.29 app/utils/comee/core/period.rb
comee_core-0.1.28 app/utils/comee/core/period.rb
comee_core-0.1.27 app/utils/comee/core/period.rb
comee_core-0.1.26 app/utils/comee/core/period.rb
comee_core-0.1.25 app/utils/comee/core/period.rb
comee_core-0.1.24 app/utils/comee/core/period.rb