Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduling/intersection.rb

Defined Under Namespace

Classes: RangeDecreasingError

Instance Method Summary collapse

Instance Method Details

#decreasing?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'lib/scheduling/intersection.rb', line 4

def decreasing?
  if exclude_end?
    last < first
  else
    last <= first
  end
end

#intersect?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scheduling/intersection.rb', line 12

def intersect? other
  raise RangeDecreasingError if other.decreasing?

  if other.first > last
    return false
  elsif other.first == last && exclude_end?
    return false
  elsif other.last < first
    return false
  elsif other.last == first && other.exclude_end?
    return false
  else
    return true
  end
end

#intersection(other) ⇒ Object Also known as: &, intersect



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
61
62
# File 'lib/scheduling/intersection.rb', line 28

def intersection(other)
  raise RangeDecreasingError if other.decreasing?

  if intersect?(other)
    if include? other.first
      start = other.first
    else  # other.first < first
      start = first
    end

    if exclude_end?
      if other.exclude_end?
        return start...(last < other.last ? last : other.last)
      else # other is inclusive
        if other.last <= last
          return start..other.last
        else
          return start...last
        end
      end
    else # self is inclusize
      if other.exclude_end?
        if other.last >= last
          return start..last
        else other.last < last
          return start...other.last
        end
      else # other is inclusive as well
        return start..(last < other.last ? last : other.last)
      end
    end
  end
  
  return nil
end