Class: Temporality::TimeSpanCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/temporality/time_span_collection.rb

Direct Known Subclasses

SliceCollection

Instance Method Summary collapse

Constructor Details

#initialize(collection = []) ⇒ TimeSpanCollection

Returns a new instance of TimeSpanCollection.



5
6
7
8
9
10
# File 'lib/temporality/time_span_collection.rb', line 5

def initialize(collection = [])
  super()
  collection = [collection].flatten # Support pour ne passer qu'un seul TS
  collection.map { |ts| self << (ts.is_a?(TimeSpan) ? ts : raise(TypeError, "Element is not a Temporality::TimeSpan")) }
  sort!
end

Instance Method Details

#inspectObject



16
17
18
# File 'lib/temporality/time_span_collection.rb', line 16

def inspect
  to_s
end

#intersect(tsc) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/temporality/time_span_collection.rb', line 39

def intersect(tsc)
  res = tsc.inject(TimeSpanCollection.new) do |r, ts|
    r << ts.intersect(self)
  end.compact.sort

  TimeSpanCollection.new(res)
end

#intersect!(time_span) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/temporality/time_span_collection.rb', line 27

def intersect!(time_span)
  if time_span.is_a? TimeSpan
    map! { |ts| ts.intersect(time_span) }.compact!
  elsif time_span.is_a? TimeSpanCollection
    raise 'Not handling time_span collections yet'
  else
    raise TypeError, "Element is not a Temporality::TimeSpan or a Temporality::TimeSpanCollection"
  end

  self
end

#intersects?(time_span) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/temporality/time_span_collection.rb', line 47

def intersects?(time_span)
  any? { |ts| ts.intersects? time_span }
end

#limit_to!(time_span) ⇒ Object



20
21
22
23
24
25
# File 'lib/temporality/time_span_collection.rb', line 20

def limit_to!(time_span)
  reject! { |ts| !ts.intersects?(time_span) }
  first.starts_on = [first.starts_on, time_span.starts_on].max
  last.ends_on = [last.ends_on, time_span.ends_on].min
  self
end

#substract(time_span) ⇒ Object

Raises:

  • (TypeError)


51
52
53
54
55
56
57
58
59
# File 'lib/temporality/time_span_collection.rb', line 51

def substract(time_span)
  raise TypeError, "Not a TimeSpan" unless time_span.is_a? TimeSpan

  TimeSpanCollection.new(
    map do |ts|
      ts.substract(time_span)
    end
  )
end

#to_sObject



12
13
14
# File 'lib/temporality/time_span_collection.rb', line 12

def to_s
  "{TSC : #{map { |ts| ts.to_s }.join(", ")}}"
end