Class: TimeSpan::TimeLine
- Inherits:
-
Array
- Object
- Array
- TimeSpan::TimeLine
- Defined in:
- lib/time_span.rb
Overview
class TimeLine #
#
TimeLine is how RelativeTime objects are related to each other, they have to have the same #
frame of reference. #
#
instance methods: #
#
to_s -- convenience method to see which object we have #
position_of(obj) -- its location on the TimeLine #
increase_after(position, amount) -- make room in the index for an insertion #
append (obj) -- add obj to end of the TimeLine #
append_to_next(relative_object, object, relative) at relative offset to relative_object, put obj #
insert_before_next(relative_object, object, relative) inserts into new time slot relative to relative_object
insert_at(position, object) -- places object at position, appending if equal time exists #
remove(obj) -- remove object from the TimeLine #
compress! -- compresses the TimeLine, removing [] and adjusting the index accordingly #
#
Instance Attribute Summary collapse
-
#indices_of ⇒ Object
Returns the value of attribute indices_of.
-
#line ⇒ Object
Returns the value of attribute line.
-
#name ⇒ Object
Returns the value of attribute name.
-
#spans ⇒ Object
Returns the value of attribute spans.
Instance Method Summary collapse
-
#==(other_timeline) ⇒ Boolean
test for value equality.
-
#all_endpoint_statuses ⇒ Array
Endpoint statuses for all TimeSpan s on the TimeLine.
-
#all_relative_time_statuses ⇒ Array
all statuses on the TimeLine.
-
#append(obj) ⇒ Fixnum
add to the end of the TimeLine.
-
#append_to_next(relative_obj, obj, relative = 1) ⇒ Fixnum
inserts to the end of the relative object’s time, becoming equal with it.
- #clone ⇒ Object
-
#compress! ⇒ nil
removes all [] elements, and decrements accordingly the @indices_of ideally this should be transactional.
-
#increase_after(pos, by = 1) ⇒ Object
bump up indices after a point, so that a RelativeTime may be inserted.
-
#initialize(name = "") ⇒ TimeLine
constructor
A new instance of TimeLine.
-
#insert_at(pos, obj) ⇒ Fixnum
place obj at the numbered position.
-
#insert_before_next(relative_obj, obj, relative_offset = 1) ⇒ Fixnum
inserts into a new space before the relative object (first parameter) inserts obj before relative_obj by offset.
-
#inspect ⇒ String
endpoint statuses for all TimeSpans on the TimeLine, plus TimeLine name.
-
#position_of(obj) ⇒ Fixnum
find the position of a RelativeTime on the TimeLine.
-
#relative_times ⇒ Array
attached times only (internal API).
-
#remove(obj) ⇒ Object
cannot remove [] or the @indices_of will be wrong call #compress to remove the extra []s.
-
#to_s ⇒ String
returns the TimeLine’s name.
Constructor Details
#initialize(name = "") ⇒ TimeLine
Returns a new instance of TimeLine.
246 247 248 249 250 251 |
# File 'lib/time_span.rb', line 246 def initialize(name="") @name = name @line = [] @indices_of = {} @spans = [] end |
Instance Attribute Details
#indices_of ⇒ Object
Returns the value of attribute indices_of.
243 244 245 |
# File 'lib/time_span.rb', line 243 def indices_of @indices_of end |
#line ⇒ Object
Returns the value of attribute line.
243 244 245 |
# File 'lib/time_span.rb', line 243 def line @line end |
#name ⇒ Object
Returns the value of attribute name.
243 244 245 |
# File 'lib/time_span.rb', line 243 def name @name end |
#spans ⇒ Object
Returns the value of attribute spans.
243 244 245 |
# File 'lib/time_span.rb', line 243 def spans @spans end |
Instance Method Details
#==(other_timeline) ⇒ Boolean
test for value equality. Checking span trait is redundant – can’t have spans without times’
261 262 263 264 265 266 |
# File 'lib/time_span.rb', line 261 def == (other_timeline) (object_id == other_timeline.object_id) || (@name == other_timeline.name) && (@line.empty? && @indices_of.empty? && @spans.empty?) && (other_timeline.line.empty? && other_timeline.indices_of.empty? && other_timeline.spans.empty?) end |
#all_endpoint_statuses ⇒ Array
Returns endpoint statuses for all TimeSpan s on the TimeLine.
281 282 283 |
# File 'lib/time_span.rb', line 281 def all_endpoint_statuses spans.inject({}){ |acc, span| acc.merge!(span.endpoint_statuses) } end |
#all_relative_time_statuses ⇒ Array
all statuses on the TimeLine
293 294 295 |
# File 'lib/time_span.rb', line 293 def all_relative_time_statuses relative_times.inject([]) {|acc, v| acc << v.reference_to } end |
#append(obj) ⇒ Fixnum
add to the end of the TimeLine
318 319 320 |
# File 'lib/time_span.rb', line 318 def append(obj) insert_at(@line.size, obj) end |
#append_to_next(relative_obj, obj, relative = 1) ⇒ Fixnum
inserts to the end of the relative object’s time, becoming equal with it
326 327 328 |
# File 'lib/time_span.rb', line 326 def append_to_next(relative_obj, obj, relative=1) insert_at(position_of(relative_obj)+relative, obj) end |
#clone ⇒ Object
254 255 256 |
# File 'lib/time_span.rb', line 254 def clone raise NotImplementedError, "Cannot use base Ruby clone which can create illegal objects by gem rules." end |
#compress! ⇒ nil
removes all [] elements, and decrements accordingly the @indices_of
ideally this should be transactional
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/time_span.rb', line 373 def compress! mod_level = 0 offsets = [] 0.upto(line.size-1) do |i| mod_level -= 1 if @line[i].empty? offsets << mod_level end ## poor man's transaction. Don't do directly on indices_of so less chance of interruption indices = indices_of indices.each_key do |key| indices[key] = indices[key] + offsets[indices[key]] end indices_of = indices @line.delete([]) end |
#increase_after(pos, by = 1) ⇒ Object
bump up indices after a point, so that a RelativeTime may be inserted
307 308 309 310 311 |
# File 'lib/time_span.rb', line 307 def increase_after(pos, by=1) @indices_of.each_key do |key| @indices_of[key] += by if (@indices_of[key] >= pos) end end |
#insert_at(pos, obj) ⇒ Fixnum
place obj at the numbered position
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/time_span.rb', line 346 def insert_at(pos, obj) raise ArgumentError, "can only add a time to its own time_line" unless obj.time_line.equal? self if @line[pos].nil? @line[pos] = [obj] else op = @line[pos].kind_of?(Array) ? '<<' : '=' @line[pos].send(op.to_sym, obj).uniq! # no duplicates in same position # dup in diff position overwrites below end @indices_of[obj] = pos end |
#insert_before_next(relative_obj, obj, relative_offset = 1) ⇒ Fixnum
inserts into a new space before the relative object (first parameter) inserts obj before relative_obj by offset
335 336 337 338 339 340 |
# File 'lib/time_span.rb', line 335 def insert_before_next(relative_obj, obj, relative_offset=1) relative_position = position_of(relative_obj) increase_after(relative_position + relative_offset, relative_offset) @line[relative_position + relative_offset,0] = nil insert_at(relative_position+relative_offset, obj) end |
#inspect ⇒ String
endpoint statuses for all TimeSpans on the TimeLine, plus TimeLine name
276 277 278 |
# File 'lib/time_span.rb', line 276 def inspect line.inspect + name end |
#position_of(obj) ⇒ Fixnum
find the position of a RelativeTime on the TimeLine
301 302 303 |
# File 'lib/time_span.rb', line 301 def position_of(obj) @indices_of[obj] end |
#relative_times ⇒ Array
attached times only (internal API)
287 288 289 |
# File 'lib/time_span.rb', line 287 def relative_times indices_of.keys end |
#remove(obj) ⇒ Object
cannot remove [] or the @indices_of will be wrong call #compress to remove the extra []s
362 363 364 365 366 367 368 |
# File 'lib/time_span.rb', line 362 def remove(obj) pos = position_of(obj) if pos # do nothing if it isn't there' @line[pos].delete(obj) # remove from list @indices_of.delete(obj) # remove from index end end |
#to_s ⇒ String
returns the TimeLine’s name
270 271 272 |
# File 'lib/time_span.rb', line 270 def to_s name.to_s end |