Class: TimeSpan::TimeSpan

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

Overview

this class’ objects have a starting and ending TimeSpan::RelativeTime, and both times must be on the same TimeSpan::TimeLine implements a large selection of comparators, both for start / end times (single time compartors), and also range comparators

Author:

  • Craig A. Cook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starting_at, ending_at, t_line, nom = "(unnamed)") ⇒ Boolean

Returns true if passed arguments are valid to create a TimeSpan.

Parameters:

  • starting_at (TimeSpan::RelativeTime)

    is when the span starts

  • ending_at (TimeSpan::RelativeTime)

    is when the spa nends

  • is (TimeSpan::TimeLIne)

    the associated TimeLine

  • The (String)

    name of the TimeSpan (Actually any Ruby Object for which ‘#respond_to?(:to_s) == true’)

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
# File 'lib/time_span.rb', line 25

def initialize(starting_at, ending_at, t_line, nom="(unnamed)")
  raise ArgumentError, "Cannot make a span unless both points are on the same time_line" unless  starting_at.colinear_with?(ending_at)
  self.starts           = starting_at
  self.ends             = ending_at
  self.time_line        = t_line
  self.time_line.spans  << self
  self.name             = nom
  starting_at.kind_of?(RelativeTime) && ending_at.kind_of?(RelativeTime) && (starting_at <= ending_at)
end

Instance Attribute Details

#endsObject

TimeSpan::RelativeTime end time



15
16
17
# File 'lib/time_span.rb', line 15

def ends
  @ends
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/time_span.rb', line 18

def name
  @name
end

#startsObject

TimeSpan::RelativeTime start time



13
14
15
# File 'lib/time_span.rb', line 13

def starts
  @starts
end

#time_lineObject

TimeSpan::TimeLine this TimeSpan is associated with



17
18
19
# File 'lib/time_span.rb', line 17

def time_line
  @time_line
end

Instance Method Details

#!=(other_time_span) ⇒ Boolean

tests if one TimeSpan is not the same as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if not same as the other



170
171
172
# File 'lib/time_span.rb', line 170

def != (other_time_span)
  !end_with(other_time_span) || !starts_with(other_time_span)
end

#<(other_time_span) ⇒ Boolean

tests if one TimeSpan ends before another starts (on the same TimeLine – delegated to RelativeTime) alias for ‘#ends_before_other_starts’

Parameters:

Returns:

  • (Boolean)

    true if self ends before another starts



178
179
180
# File 'lib/time_span.rb', line 178

def < (other_time_span)
  ends_before_other_starts?(other_time_span)
end

#==(other_time_span) ⇒ Boolean

tests if one TimeSpan is the same as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if same as the other



163
164
165
# File 'lib/time_span.rb', line 163

def == (other_time_span)
  ends_with?(other_time_span) && starts_with?(other_time_span)
end

#>(other_time_span) ⇒ Boolean

tests if one TimeSpan starts after another ends (on the same TimeLine – delegated to RelativeTime) alias for ‘#starts_after_other_ends’

Parameters:

Returns:

  • (Boolean)

    true if self starts after another ends



186
187
188
# File 'lib/time_span.rb', line 186

def > (other_time_span)
  starts_after_other_ends?(other_time_span)
end

#cloneObject

Raises:

  • (NotImplementedError)

    could create illegal objects by gem rules



36
37
38
# File 'lib/time_span.rb', line 36

def clone
  raise NotImplementedError, "Cannot use base Ruby clone which can create illegal objects by gem rules."
end

#contained_fully_inside?(other_time_span) ⇒ Boolean

tests if one TimeSpan is contained within another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self contained inside another



193
194
195
# File 'lib/time_span.rb', line 193

def contained_fully_inside?(other_time_span)
  starts_after?(other_time_span) && ends_before?(other_time_span)
end

#contained_inside?(other_time_span) ⇒ Boolean

tests if one TimeSpan is contained within another, possibly begining as or ends as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self contained inside another, including same endpoints



200
201
202
# File 'lib/time_span.rb', line 200

def contained_inside?(other_time_span)
  starts_on_or_after?(other_time_span) && ends_on_or_before?(other_time_span)
end

#contains?(other_time_span) ⇒ Boolean

tests if one TimeSpan contains within another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self contains another



214
215
216
# File 'lib/time_span.rb', line 214

def contains?(other_time_span)
  starts_before_or_with?(other_time_span) && ends_on_or_after?(other_time_span)
end

#contains_fully?(other_time_span) ⇒ Boolean

tests if one TimeSpan contains another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self contains another



207
208
209
# File 'lib/time_span.rb', line 207

def contains_fully?(other_time_span)
  starts_before?(other_time_span) && ends_after?(other_time_span)
end

#endpoint_statusesObject

returns the ‘statuses’ for the start and end times

Returns:

  • start and end time statuses in a hash with key is self



42
43
44
# File 'lib/time_span.rb', line 42

def endpoint_statuses
  {self => [self.starts.reference_to, self.ends.reference_to]}
end

#ends_after?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends after another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends after another time_span ends



111
112
113
# File 'lib/time_span.rb', line 111

def ends_after?(other_time_span)
  ends > other_time_span.ends
end

#ends_as_other_starts?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends at the same time as another starts (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends at the same time as another time_span starts (no gap)



132
133
134
# File 'lib/time_span.rb', line 132

def ends_as_other_starts?(other_time_span)
  ends == other_time_span.starts
end

#ends_before?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends before another starts (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends before another time_span starts



90
91
92
# File 'lib/time_span.rb', line 90

def ends_before?(other_time_span)
  ends < other_time_span.ends
end

#ends_before_other_starts?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends before another starts (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends before another time_span starts



125
126
127
# File 'lib/time_span.rb', line 125

def ends_before_other_starts?(other_time_span)
  ends < other_time_span.starts
end

#ends_on_or_after?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends after or at the same time as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends after or at the same time as another time_span ends



104
105
106
# File 'lib/time_span.rb', line 104

def ends_on_or_after?(other_time_span)
  ends >= other_time_span.ends
end

#ends_on_or_before?(other_time_span) ⇒ Boolean

tests if one TimeSpan end before or at the same time as another ends (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends before or at the same time as another time_span ends



97
98
99
# File 'lib/time_span.rb', line 97

def ends_on_or_before?(other_time_span)
  ends <= other_time_span.ends
end

#ends_with?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends at the same time as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends at the same time as another time_span ends



118
119
120
# File 'lib/time_span.rb', line 118

def ends_with?(other_time_span)
  ends == other_time_span.ends
end

#starts_after?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts after another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts after other_time_span starts



62
63
64
# File 'lib/time_span.rb', line 62

def starts_after?(other_time_span)
  starts > other_time_span.starts
end

#starts_after_other_ends?(other_time_span) ⇒ Boolean

tests if one TimeSpan ends at the same time as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self ends at the same time as another time_span ends



139
140
141
# File 'lib/time_span.rb', line 139

def starts_after_other_ends?(other_time_span)
  starts > other_time_span.ends
end

#starts_as_other_ends?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts at the same time as another ends (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts at the same time as another time_span ends



146
147
148
# File 'lib/time_span.rb', line 146

def starts_as_other_ends?(other_time_span)
  starts == other_time_span.ends
end

#starts_before?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts before another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts before b starts



55
56
57
# File 'lib/time_span.rb', line 55

def starts_before?(other_time_span)
  starts < other_time_span.starts
end

#starts_before_or_with?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts before or at the same time as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts before or at the same time as other_time_span starts



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

def starts_before_or_with?(other_time_span)
  starts <= other_time_span.starts
end

#starts_on_or_after?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts with or after another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts on or after other_time_span starts



69
70
71
# File 'lib/time_span.rb', line 69

def starts_on_or_after?(other_time_span)
  starts >= other_time_span.starts
end

#starts_with?(other_time_span) ⇒ Boolean

tests if one TimeSpan starts at the same time as another (on the same TimeLine – delegated to RelativeTime)

Parameters:

Returns:

  • (Boolean)

    true if self starts at the same time as other_time_span starts



76
77
78
# File 'lib/time_span.rb', line 76

def starts_with?(other_time_span)
  starts == other_time_span.starts
end