Class: Daterval::Set

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daterval/set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*pairs) ⇒ Set

Returns a new instance of Set.



5
6
7
# File 'lib/daterval/set.rb', line 5

def initialize(*pairs)
  @list = pairs
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



34
35
36
# File 'lib/daterval/set.rb', line 34

def list
  @list
end

Instance Method Details

#eachObject



9
10
11
# File 'lib/daterval/set.rb', line 9

def each
  list.each { |pair| yield pair }
end

#merged!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/daterval/set.rb', line 13

def merged!
  sorted = list.sort_by(&:begin)
  new_list = []

  sorted.each do |pair|
    merged = false

    new_list.each_with_index do |pair_x, index_x|
      if pair.overlaps?(pair_x)
        new_list[index_x] = pair.overlap(pair_x)
        merged = true
      end
    end

    new_list << pair unless merged
  end

  @list = new_list
  self
end