Class: TimeLayout

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

Overview

assert interval is constant assert start_at of one TimeSpan can equal end_at of another

Constant Summary collapse

DEFAULT_INTERVAL =
1.day

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TimeLayout

:time_spans or :start_at required



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

def initialize(args)
  @key = args[:key]
  if args[:time_spans] and args[:time_spans].is_a?(Array)
    @time_spans = args[:time_spans]
  else
    @time_spans = []
    if args[:end_at].nil? || args[:end_at].is_a?(Array) && args[:end_at].compact.empty?
      @interval = calculate_interval_from_times(args[:start_at]) 
      args[:end_at] = nil
    end
    args[:start_at].each_with_index do |start_at,i|
      if args[:end_at]
        @time_spans << TimeSpan.new(:start_at => start_at, :end_at => args[:end_at][i])
      else
        @time_spans << TimeSpan.new(:start_at => start_at, :duration => self.interval)
      end
    end
  end
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/time_layout.rb', line 7

def key
  @key
end

#time_spansObject

Returns the value of attribute time_spans.



8
9
10
# File 'lib/time_layout.rb', line 8

def time_spans
  @time_spans
end

Instance Method Details

#[](i) ⇒ Object



56
57
58
# File 'lib/time_layout.rb', line 56

def [](i)
  @time_spans[i] if (@time_spans && @time_spans.is_a?(Array) && i >= 0 && i < @time_spans.length)
end

#durationObject



83
84
85
# File 'lib/time_layout.rb', line 83

def duration
  self.end_at - self.start_at
end

#end_atObject

of entire TimeLayout



64
65
66
# File 'lib/time_layout.rb', line 64

def end_at # of entire TimeLayout
  @end_at ||= self.time_spans.inject(nil){|memo,time_span| (memo && memo > time_span.end_at) ? memo : time_span.end_at }
end

#index_at(t) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/time_layout.rb', line 33

def index_at(t)
  @time_spans.each_with_index do |time_span,i|
    return i if time_span.envelopes?(t, false)
  end
  # if t falls on the end of a time_span and not the start of any others
  @time_spans.each_with_index do |time_span,i|
    return i if time_span.envelopes?(t, true)
  end
  nil
end

#indices_enveloping(time_span) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/time_layout.rb', line 44

def indices_enveloping(time_span)
  indices = []
  weights = []
  @time_spans.each_with_index do |ts,i|
    if ts.overlaps?(time_span, false)
      indices << i
      weights << ts.overlap_by(time_span, false)
    end
  end
  return indices, weights
end

#intervalObject



68
69
70
# File 'lib/time_layout.rb', line 68

def interval
  @interval ||= (@time_spans.first.duration || calculate_interval_from_time_spans)
end

#lengthObject



72
73
74
# File 'lib/time_layout.rb', line 72

def length
  @length ||= @time_spans.length
end

#start_atObject

of entire TimeLayout



60
61
62
# File 'lib/time_layout.rb', line 60

def start_at # of entire TimeLayout
  @start_at ||= self.time_spans.inject(nil){|memo,time_span| (memo && memo < time_span.start_at) ? memo : time_span.start_at }
end