time_span 0.0.1.alpha05 → 0.0.1.beta.01

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .idea/*
6
+ .gitignore
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ Time Span
2
+ ==========
3
+
4
+ This gem is still in Alpha. The API might well break between now and release, use at your risk.
5
+
6
+ Better documentation will also follow, as I learn how to write Yard docs.
7
+
8
+ A RelativeTime can occur with more than one TimeSpan, so must also keep track of the TimeLine to TimeSpan relationship.
9
+
10
+ For now I am focused on spec coverage and core code functionality.
11
+
@@ -133,20 +133,34 @@
133
133
 
134
134
  <div id="description" class="description">
135
135
 
136
+ <p>class <a href="RelativeTime.html">RelativeTime</a>
137
+ #</p>
138
+
136
139
  <pre>#</pre>
137
140
 
138
- <p>A relative time must be able to be have equal times
139
- # implemented as an Array of Arrays; all elements of the ‘subarray’ are
140
- equal in time, # otherwise, the time relationship are based on position
141
- in the main array #</p>
141
+ <p>public methods:
142
+ #</p>
142
143
 
143
- <pre>#</pre>
144
+ <pre>comparators: &lt; &lt;= == != &gt;= &gt; #
145
+ work on any two RelativeTime objects on the same TimeLine #
146
+ positioned? #
147
+ true if a RelativeTime has been put on a TimeLine # #
148
+ colinear_with?(RelativeTime.new) #
149
+ true if both RelativeTime objects are positioned and on same TimeLine #
150
+ #</pre>
151
+
152
+ <p>protected method:
153
+ #</p>
154
+
155
+ <pre>valid_and_comparable_with?(RelativeTime) #
156
+ true if #
157
+ #</pre>
144
158
 
145
159
  <p>diff cannot be done, it makes no sense, due to the fuzziness
146
160
  #</p>
147
161
 
148
- <pre> #
149
- RelativeTime must be within a TimeLine #</pre>
162
+ <pre> #
163
+ RelativeTime must be within a TimeLine #</pre>
150
164
 
151
165
  </div><!-- description -->
152
166
 
@@ -228,7 +242,7 @@ RelativeTime must be within a TimeLine
228
242
 
229
243
  <div class="method-source-code" id="new-source">
230
244
  <pre>
231
- <span class="ruby-comment"># File lib/time_span.rb, line 226</span>
245
+ <span class="ruby-comment"># File lib/time_span.rb, line 261</span>
232
246
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span> <span class="ruby-identifier">tline</span>, <span class="ruby-identifier">ref</span>
233
247
  <span class="ruby-ivar">@timeline</span>= <span class="ruby-identifier">tline</span>
234
248
  <span class="ruby-ivar">@reference_to</span>= <span class="ruby-identifier">ref</span>
@@ -268,9 +282,9 @@ RelativeTime must be within a TimeLine
268
282
 
269
283
  <div class="method-source-code" id="colinear_with-3F-source">
270
284
  <pre>
271
- <span class="ruby-comment"># File lib/time_span.rb, line 247</span>
285
+ <span class="ruby-comment"># File lib/time_span.rb, line 282</span>
272
286
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">colinear_with?</span>(<span class="ruby-identifier">b</span>)
273
- <span class="ruby-keyword">self</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">b</span>.<span class="ruby-identifier">timeline</span>)
287
+ <span class="ruby-identifier">b</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">positioned?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">positioned?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">b</span>.<span class="ruby-identifier">timeline</span>)
274
288
  <span class="ruby-keyword">end</span></pre>
275
289
  </div><!-- colinear_with-3F-source -->
276
290
 
@@ -301,9 +315,9 @@ RelativeTime must be within a TimeLine
301
315
 
302
316
  <div class="method-source-code" id="positioned-3F-source">
303
317
  <pre>
304
- <span class="ruby-comment"># File lib/time_span.rb, line 243</span>
318
+ <span class="ruby-comment"># File lib/time_span.rb, line 278</span>
305
319
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">positioned?</span>
306
- <span class="ruby-keyword">self</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">indices_of</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-keyword">self</span>)
320
+ <span class="ruby-keyword">self</span>.<span class="ruby-identifier">timeline</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">indices_of</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-keyword">self</span>)
307
321
  <span class="ruby-keyword">end</span></pre>
308
322
  </div><!-- positioned-3F-source -->
309
323
 
@@ -334,7 +348,7 @@ RelativeTime must be within a TimeLine
334
348
 
335
349
  <div class="method-source-code" id="to_s-source">
336
350
  <pre>
337
- <span class="ruby-comment"># File lib/time_span.rb, line 231</span>
351
+ <span class="ruby-comment"># File lib/time_span.rb, line 266</span>
338
352
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">to_s</span>
339
353
  <span class="ruby-identifier">reference_to</span>.<span class="ruby-identifier">to_s</span>
340
354
  <span class="ruby-keyword">end</span></pre>
@@ -373,7 +387,7 @@ RelativeTime must be within a TimeLine
373
387
 
374
388
  <div class="method-source-code" id="valid_and_comparable_with-3F-source">
375
389
  <pre>
376
- <span class="ruby-comment"># File lib/time_span.rb, line 254</span>
390
+ <span class="ruby-comment"># File lib/time_span.rb, line 289</span>
377
391
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">valid_and_comparable_with?</span>(<span class="ruby-identifier">b</span>)
378
392
  <span class="ruby-operator">!</span><span class="ruby-keyword">self</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">b</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">colinear_with?</span>(<span class="ruby-identifier">b</span>)
379
393
  <span class="ruby-keyword">end</span></pre>
@@ -143,6 +143,33 @@
143
143
 
144
144
  <div id="description" class="description">
145
145
 
146
+ <p>class <a href="TimeLine.html">TimeLine</a>
147
+ #</p>
148
+
149
+ <pre>#</pre>
150
+
151
+ <p><a href="TimeLine.html">TimeLine</a> is how <a
152
+ href="RelativeTime.html">RelativeTime</a> objects are related to each
153
+ other, they have to have the same #</p>
154
+
155
+ <pre>frame of reference. #
156
+ #</pre>
157
+
158
+ <p>instance methods:
159
+ #</p>
160
+
161
+ <pre> #
162
+ to_s -- convenience method to see which object we have #
163
+ position_of(obj) -- its location on the TimeLine #
164
+ increase_after(position, amount) -- make room in the index for an insertion #
165
+ append (obj) -- add obj to end of the TimeLine #
166
+ append_to_next(relative_object, object, relative) at relative offset to relative_object, put obj #
167
+ insert_before_next(relative_object, object, relative) inserts into new time slot relative to relative_object
168
+ insert_at(position, object) -- places object at position, appending if equal time exists #
169
+ remove(obj) -- remove object from the TimeLine #
170
+ compress! -- compresses the TimeLine, removing [] and adjusting the index accordingly #
171
+ #</pre>
172
+
146
173
  </div><!-- description -->
147
174
 
148
175
 
@@ -240,7 +267,7 @@
240
267
 
241
268
  <div class="method-source-code" id="new-source">
242
269
  <pre>
243
- <span class="ruby-comment"># File lib/time_span.rb, line 127</span>
270
+ <span class="ruby-comment"># File lib/time_span.rb, line 156</span>
244
271
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">name</span>=<span class="ruby-string">&quot;&quot;</span>)
245
272
  <span class="ruby-ivar">@name</span> = <span class="ruby-identifier">name</span>
246
273
  <span class="ruby-ivar">@line</span> = []
@@ -275,13 +302,13 @@
275
302
 
276
303
  <div class="method-description">
277
304
 
278
-
305
+ <p>add to the end of the <a href="TimeLine.html">TimeLine</a></p>
279
306
 
280
307
 
281
308
 
282
309
  <div class="method-source-code" id="append-source">
283
310
  <pre>
284
- <span class="ruby-comment"># File lib/time_span.rb, line 137</span>
311
+ <span class="ruby-comment"># File lib/time_span.rb, line 177</span>
285
312
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">append</span>(<span class="ruby-identifier">obj</span>)
286
313
  <span class="ruby-identifier">insert_at</span>(<span class="ruby-ivar">@line</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">obj</span>)
287
314
  <span class="ruby-keyword">end</span></pre>
@@ -308,13 +335,13 @@
308
335
 
309
336
  <div class="method-description">
310
337
 
311
-
338
+ <p>inserts to the end of the relative object’s time, becoming equal with it</p>
312
339
 
313
340
 
314
341
 
315
342
  <div class="method-source-code" id="append_to_next-source">
316
343
  <pre>
317
- <span class="ruby-comment"># File lib/time_span.rb, line 147</span>
344
+ <span class="ruby-comment"># File lib/time_span.rb, line 182</span>
318
345
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">append_to_next</span>(<span class="ruby-identifier">relative_obj</span>, <span class="ruby-identifier">obj</span>, <span class="ruby-identifier">relative</span>=<span class="ruby-value">1</span>)
319
346
  <span class="ruby-identifier">insert_at</span>(<span class="ruby-identifier">position_of</span>(<span class="ruby-identifier">relative_obj</span>)<span class="ruby-operator">+</span><span class="ruby-identifier">relative</span>, <span class="ruby-identifier">obj</span>)
320
347
  <span class="ruby-keyword">end</span></pre>
@@ -350,24 +377,19 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
350
377
 
351
378
  <div class="method-source-code" id="compress-21-source">
352
379
  <pre>
353
- <span class="ruby-comment"># File lib/time_span.rb, line 187</span>
380
+ <span class="ruby-comment"># File lib/time_span.rb, line 219</span>
354
381
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">compress!</span>
355
-
356
382
  <span class="ruby-identifier">mod_level</span> = <span class="ruby-value">0</span>
357
383
  <span class="ruby-identifier">offsets</span> = []
358
384
  <span class="ruby-value">0</span>.<span class="ruby-identifier">upto</span>(<span class="ruby-identifier">line</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
359
385
  <span class="ruby-identifier">mod_level</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">i</span>].<span class="ruby-identifier">empty?</span>
360
386
  <span class="ruby-identifier">offsets</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">mod_level</span>
361
387
  <span class="ruby-keyword">end</span>
362
-
363
388
  <span class="ruby-comment">## poor man's transaction. Don't do directly on indices_of so less chance of interruption</span>
364
389
  <span class="ruby-identifier">indices</span> = <span class="ruby-identifier">indices_of</span>
365
-
366
- <span class="ruby-comment">## refactor to use increase_after</span>
367
390
  <span class="ruby-identifier">indices</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span>
368
391
  <span class="ruby-identifier">indices</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">indices</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">+</span> <span class="ruby-identifier">offsets</span>[<span class="ruby-identifier">indices</span>[<span class="ruby-identifier">key</span>]]
369
392
  <span class="ruby-keyword">end</span>
370
-
371
393
  <span class="ruby-identifier">indices_of</span> = <span class="ruby-identifier">indices</span>
372
394
  <span class="ruby-ivar">@line</span>.<span class="ruby-identifier">delete</span>([])
373
395
  <span class="ruby-keyword">end</span></pre>
@@ -400,7 +422,7 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
400
422
 
401
423
  <div class="method-source-code" id="increase_after-source">
402
424
  <pre>
403
- <span class="ruby-comment"># File lib/time_span.rb, line 141</span>
425
+ <span class="ruby-comment"># File lib/time_span.rb, line 168</span>
404
426
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">increase_after</span>(<span class="ruby-identifier">pos</span>, <span class="ruby-identifier">by</span>=<span class="ruby-value">1</span>)
405
427
  <span class="ruby-ivar">@indices_of</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span>
406
428
  <span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">+=</span> <span class="ruby-identifier">by</span> <span class="ruby-keyword">if</span> (<span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">pos</span>)
@@ -429,24 +451,23 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
429
451
 
430
452
  <div class="method-description">
431
453
 
432
- <p>needs work for <a href="RelativeTime.html">RelativeTime</a> objects</p>
454
+ <p>place obj at the numbered position</p>
433
455
 
434
456
 
435
457
 
436
458
  <div class="method-source-code" id="insert_at-source">
437
459
  <pre>
438
- <span class="ruby-comment"># File lib/time_span.rb, line 159</span>
460
+ <span class="ruby-comment"># File lib/time_span.rb, line 195</span>
439
461
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">insert_at</span>(<span class="ruby-identifier">pos</span>, <span class="ruby-identifier">obj</span>)
440
-
441
462
  <span class="ruby-identifier">raise</span> <span class="ruby-string">&quot;can only add a time to its own timeline&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">timeline</span>.<span class="ruby-identifier">equal?</span> <span class="ruby-keyword">self</span>
442
463
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">nil?</span>
443
464
  <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>] = [<span class="ruby-identifier">obj</span>]
444
- <span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">obj</span>] = <span class="ruby-identifier">pos</span>
445
465
  <span class="ruby-keyword">else</span>
446
466
  <span class="ruby-identifier">op</span> = <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">?</span> <span class="ruby-string">'&lt;&lt;'</span> <span class="ruby-operator">:</span> <span class="ruby-string">'='</span>
447
- <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">send</span>(<span class="ruby-identifier">op</span>.<span class="ruby-identifier">to_sym</span>, <span class="ruby-identifier">obj</span>) <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">obj</span>) <span class="ruby-comment"># no dupliates</span>
448
- <span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">obj</span>] = <span class="ruby-identifier">pos</span> <span class="ruby-comment"># dup overwrites</span>
467
+ <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">send</span>(<span class="ruby-identifier">op</span>.<span class="ruby-identifier">to_sym</span>, <span class="ruby-identifier">obj</span>).<span class="ruby-identifier">uniq!</span> <span class="ruby-comment"># no duplicates in same position</span>
468
+ <span class="ruby-comment"># dup in diff position overwrites below</span>
449
469
  <span class="ruby-keyword">end</span>
470
+ <span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">obj</span>] = <span class="ruby-identifier">pos</span>
450
471
  <span class="ruby-keyword">end</span></pre>
451
472
  </div><!-- insert_at-source -->
452
473
 
@@ -471,13 +492,13 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
471
492
 
472
493
  <div class="method-description">
473
494
 
474
-
495
+ <p>inserts into a new space before the relative object (first parameter)</p>
475
496
 
476
497
 
477
498
 
478
499
  <div class="method-source-code" id="insert_before_next-source">
479
500
  <pre>
480
- <span class="ruby-comment"># File lib/time_span.rb, line 151</span>
501
+ <span class="ruby-comment"># File lib/time_span.rb, line 187</span>
481
502
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">insert_before_next</span>(<span class="ruby-identifier">relative_obj</span>, <span class="ruby-identifier">obj</span>, <span class="ruby-identifier">relative_offset</span>=<span class="ruby-value">1</span>)
482
503
  <span class="ruby-identifier">relative_position</span> = <span class="ruby-identifier">position_of</span>(<span class="ruby-identifier">relative_obj</span>)
483
504
  <span class="ruby-identifier">increase_after</span>(<span class="ruby-identifier">relative_position</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">relative_offset</span>, <span class="ruby-identifier">relative_offset</span>)
@@ -507,13 +528,13 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
507
528
 
508
529
  <div class="method-description">
509
530
 
510
-
531
+ <p>indices methods</p>
511
532
 
512
533
 
513
534
 
514
535
  <div class="method-source-code" id="position_of-source">
515
536
  <pre>
516
- <span class="ruby-comment"># File lib/time_span.rb, line 133</span>
537
+ <span class="ruby-comment"># File lib/time_span.rb, line 164</span>
517
538
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">position_of</span>(<span class="ruby-identifier">obj</span>)
518
539
  <span class="ruby-ivar">@indices_of</span>[<span class="ruby-identifier">obj</span>]
519
540
  <span class="ruby-keyword">end</span></pre>
@@ -540,23 +561,21 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
540
561
 
541
562
  <div class="method-description">
542
563
 
543
-
564
+ <p>cannot remove [] or the @<a
565
+ href="TimeLine.html#attribute-i-indices_of">indices_of</a> will be wrong
566
+ call compress to remove the extra []s</p>
544
567
 
545
568
 
546
569
 
547
570
  <div class="method-source-code" id="remove-source">
548
571
  <pre>
549
- <span class="ruby-comment"># File lib/time_span.rb, line 172</span>
572
+ <span class="ruby-comment"># File lib/time_span.rb, line 209</span>
550
573
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">remove</span>(<span class="ruby-identifier">obj</span>)
551
- <span class="ruby-comment"># cannot remove [] or the @indices_of will be wrong</span>
552
- <span class="ruby-comment"># call to #compress to remove the extra []s</span>
553
574
  <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">position_of</span>(<span class="ruby-identifier">obj</span>)
554
-
555
575
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">pos</span> <span class="ruby-comment"># do nothing if it isn't there'</span>
556
- <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">obj</span>)
557
- <span class="ruby-ivar">@indices_of</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">obj</span>)
576
+ <span class="ruby-ivar">@line</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">obj</span>) <span class="ruby-comment"># remove from list</span>
577
+ <span class="ruby-ivar">@indices_of</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">obj</span>) <span class="ruby-comment"># remove from index</span>
558
578
  <span class="ruby-keyword">end</span>
559
-
560
579
  <span class="ruby-keyword">end</span></pre>
561
580
  </div><!-- remove-source -->
562
581
 
@@ -587,7 +606,7 @@ href="TimeLine.html#attribute-i-indices_of">indices_of</a></p>
587
606
 
588
607
  <div class="method-source-code" id="to_s-source">
589
608
  <pre>
590
- <span class="ruby-comment"># File lib/time_span.rb, line 123</span>
609
+ <span class="ruby-comment"># File lib/time_span.rb, line 152</span>
591
610
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">to_s</span>
592
611
  <span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>
593
612
  <span class="ruby-keyword">end</span></pre>
@@ -169,6 +169,13 @@
169
169
 
170
170
  <div id="description" class="description">
171
171
 
172
+ <p>this classes’ object have a starting and ending <a
173
+ href="RelativeTime.html">RelativeTime</a>, and both times must be on the
174
+ same <a href="TimeLine.html">TimeLine</a></p>
175
+
176
+ <p>it implements a large selection of comparators, both for start / end times
177
+ (single time compartors), and also range comparators</p>
178
+
172
179
  </div><!-- description -->
173
180
 
174
181
 
@@ -243,18 +250,18 @@
243
250
 
244
251
  <div class="method-description">
245
252
 
246
- <p>time comparison #</p>
253
+
247
254
 
248
255
 
249
256
 
250
257
  <div class="method-source-code" id="new-source">
251
258
  <pre>
252
- <span class="ruby-comment"># File lib/time_span.rb, line 13</span>
259
+ <span class="ruby-comment"># File lib/time_span.rb, line 14</span>
253
260
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">starting_at</span>, <span class="ruby-identifier">ending_at</span>)
254
261
  <span class="ruby-identifier">raise</span> <span class="ruby-string">&quot;Cannot make a span unless both points are on the same timeline&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">starting_at</span>.<span class="ruby-identifier">colinear_with?</span>(<span class="ruby-identifier">ending_at</span>)
255
262
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">starts</span> = <span class="ruby-identifier">starting_at</span>
256
263
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">ends</span> = <span class="ruby-identifier">ending_at</span>
257
- <span class="ruby-identifier">starting_at</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">RelativeTime</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ending_at</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">RelativeTime</span>)
264
+ <span class="ruby-identifier">starting_at</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">RelativeTime</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ending_at</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">RelativeTime</span>) <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">starting_at</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">ending_at</span>)
258
265
  <span class="ruby-keyword">end</span></pre>
259
266
  </div><!-- new-source -->
260
267
 
@@ -291,7 +298,7 @@
291
298
 
292
299
  <div class="method-source-code" id="21-3D-source">
293
300
  <pre>
294
- <span class="ruby-comment"># File lib/time_span.rb, line 89</span>
301
+ <span class="ruby-comment"># File lib/time_span.rb, line 98</span>
295
302
  <span class="ruby-keyword">def</span> <span class="ruby-operator">!=</span> (<span class="ruby-identifier">b</span>)
296
303
  <span class="ruby-operator">!</span><span class="ruby-identifier">end_with</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">||</span> <span class="ruby-operator">!</span><span class="ruby-identifier">starts_with</span>(<span class="ruby-identifier">b</span>)
297
304
  <span class="ruby-keyword">end</span></pre>
@@ -324,7 +331,7 @@
324
331
 
325
332
  <div class="method-source-code" id="3C-source">
326
333
  <pre>
327
- <span class="ruby-comment"># File lib/time_span.rb, line 93</span>
334
+ <span class="ruby-comment"># File lib/time_span.rb, line 102</span>
328
335
  <span class="ruby-keyword">def</span> <span class="ruby-operator">&lt;</span> (<span class="ruby-identifier">b</span>)
329
336
  <span class="ruby-identifier">ends_before_other_starts?</span>(<span class="ruby-identifier">b</span>)
330
337
  <span class="ruby-keyword">end</span></pre>
@@ -352,12 +359,16 @@
352
359
  <div class="method-description">
353
360
 
354
361
  <p>&gt;= and &lt;= intentionally not defined;</p>
362
+
363
+ <pre>logically can only mean starts_after? or ends_before? respectively
364
+ which are basically aliased to &gt; &amp; &lt;
365
+ unless it is meant &lt; XOR =</pre>
355
366
 
356
367
 
357
368
 
358
369
  <div class="method-source-code" id="3D-3D-source">
359
370
  <pre>
360
- <span class="ruby-comment"># File lib/time_span.rb, line 85</span>
371
+ <span class="ruby-comment"># File lib/time_span.rb, line 94</span>
361
372
  <span class="ruby-keyword">def</span> <span class="ruby-operator">==</span> (<span class="ruby-identifier">b</span>)
362
373
  <span class="ruby-identifier">ends_with?</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">starts_with?</span>(<span class="ruby-identifier">b</span>)
363
374
  <span class="ruby-keyword">end</span></pre>
@@ -390,7 +401,7 @@
390
401
 
391
402
  <div class="method-source-code" id="3E-source">
392
403
  <pre>
393
- <span class="ruby-comment"># File lib/time_span.rb, line 97</span>
404
+ <span class="ruby-comment"># File lib/time_span.rb, line 106</span>
394
405
  <span class="ruby-keyword">def</span> <span class="ruby-operator">&gt;</span> (<span class="ruby-identifier">b</span>)
395
406
  <span class="ruby-identifier">starts_after_other_ends?</span>(<span class="ruby-identifier">b</span>)
396
407
  <span class="ruby-keyword">end</span></pre>
@@ -423,7 +434,7 @@
423
434
 
424
435
  <div class="method-source-code" id="contained_fully_inside-3F-source">
425
436
  <pre>
426
- <span class="ruby-comment"># File lib/time_span.rb, line 101</span>
437
+ <span class="ruby-comment"># File lib/time_span.rb, line 110</span>
427
438
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">contained_fully_inside?</span>(<span class="ruby-identifier">b</span>)
428
439
  <span class="ruby-identifier">starts_after?</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ends_before?</span>(<span class="ruby-identifier">b</span>)
429
440
  <span class="ruby-keyword">end</span></pre>
@@ -456,7 +467,7 @@
456
467
 
457
468
  <div class="method-source-code" id="contained_inside-3F-source">
458
469
  <pre>
459
- <span class="ruby-comment"># File lib/time_span.rb, line 105</span>
470
+ <span class="ruby-comment"># File lib/time_span.rb, line 114</span>
460
471
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">contained_inside?</span>(<span class="ruby-identifier">b</span>)
461
472
  <span class="ruby-identifier">starts_on_or_after?</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ends_on_or_before?</span>(<span class="ruby-identifier">b</span>)
462
473
  <span class="ruby-keyword">end</span></pre>
@@ -489,7 +500,7 @@
489
500
 
490
501
  <div class="method-source-code" id="contains-3F-source">
491
502
  <pre>
492
- <span class="ruby-comment"># File lib/time_span.rb, line 113</span>
503
+ <span class="ruby-comment"># File lib/time_span.rb, line 122</span>
493
504
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">contains?</span>(<span class="ruby-identifier">b</span>)
494
505
  <span class="ruby-identifier">starts_before_or_with?</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ends_on_or_after?</span>(<span class="ruby-identifier">b</span>)
495
506
  <span class="ruby-keyword">end</span></pre>
@@ -522,7 +533,7 @@
522
533
 
523
534
  <div class="method-source-code" id="contains_fully-3F-source">
524
535
  <pre>
525
- <span class="ruby-comment"># File lib/time_span.rb, line 109</span>
536
+ <span class="ruby-comment"># File lib/time_span.rb, line 118</span>
526
537
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">contains_fully?</span>(<span class="ruby-identifier">b</span>)
527
538
  <span class="ruby-identifier">starts_before?</span>(<span class="ruby-identifier">b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ends_after?</span>(<span class="ruby-identifier">b</span>)
528
539
  <span class="ruby-keyword">end</span></pre>
@@ -555,7 +566,7 @@
555
566
 
556
567
  <div class="method-source-code" id="ends_after-3F-source">
557
568
  <pre>
558
- <span class="ruby-comment"># File lib/time_span.rb, line 54</span>
569
+ <span class="ruby-comment"># File lib/time_span.rb, line 60</span>
559
570
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_after?</span>(<span class="ruby-identifier">b</span>)
560
571
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
561
572
  <span class="ruby-keyword">end</span></pre>
@@ -588,7 +599,7 @@
588
599
 
589
600
  <div class="method-source-code" id="ends_as_other_starts-3F-source">
590
601
  <pre>
591
- <span class="ruby-comment"># File lib/time_span.rb, line 66</span>
602
+ <span class="ruby-comment"># File lib/time_span.rb, line 72</span>
592
603
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_as_other_starts?</span>(<span class="ruby-identifier">b</span>)
593
604
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
594
605
  <span class="ruby-keyword">end</span></pre>
@@ -621,7 +632,7 @@
621
632
 
622
633
  <div class="method-source-code" id="ends_before-3F-source">
623
634
  <pre>
624
- <span class="ruby-comment"># File lib/time_span.rb, line 42</span>
635
+ <span class="ruby-comment"># File lib/time_span.rb, line 48</span>
625
636
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_before?</span>(<span class="ruby-identifier">b</span>)
626
637
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
627
638
  <span class="ruby-keyword">end</span></pre>
@@ -654,7 +665,7 @@
654
665
 
655
666
  <div class="method-source-code" id="ends_before_other_starts-3F-source">
656
667
  <pre>
657
- <span class="ruby-comment"># File lib/time_span.rb, line 62</span>
668
+ <span class="ruby-comment"># File lib/time_span.rb, line 68</span>
658
669
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_before_other_starts?</span>(<span class="ruby-identifier">b</span>)
659
670
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
660
671
  <span class="ruby-keyword">end</span></pre>
@@ -687,7 +698,7 @@
687
698
 
688
699
  <div class="method-source-code" id="ends_on_or_after-3F-source">
689
700
  <pre>
690
- <span class="ruby-comment"># File lib/time_span.rb, line 50</span>
701
+ <span class="ruby-comment"># File lib/time_span.rb, line 56</span>
691
702
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_on_or_after?</span>(<span class="ruby-identifier">b</span>)
692
703
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
693
704
  <span class="ruby-keyword">end</span></pre>
@@ -720,7 +731,7 @@
720
731
 
721
732
  <div class="method-source-code" id="ends_on_or_before-3F-source">
722
733
  <pre>
723
- <span class="ruby-comment"># File lib/time_span.rb, line 46</span>
734
+ <span class="ruby-comment"># File lib/time_span.rb, line 52</span>
724
735
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_on_or_before?</span>(<span class="ruby-identifier">b</span>)
725
736
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
726
737
  <span class="ruby-keyword">end</span></pre>
@@ -753,7 +764,7 @@
753
764
 
754
765
  <div class="method-source-code" id="ends_with-3F-source">
755
766
  <pre>
756
- <span class="ruby-comment"># File lib/time_span.rb, line 58</span>
767
+ <span class="ruby-comment"># File lib/time_span.rb, line 64</span>
757
768
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">ends_with?</span>(<span class="ruby-identifier">b</span>)
758
769
  <span class="ruby-identifier">ends</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
759
770
  <span class="ruby-keyword">end</span></pre>
@@ -786,7 +797,7 @@
786
797
 
787
798
  <div class="method-source-code" id="starts_after-3F-source">
788
799
  <pre>
789
- <span class="ruby-comment"># File lib/time_span.rb, line 25</span>
800
+ <span class="ruby-comment"># File lib/time_span.rb, line 31</span>
790
801
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_after?</span>(<span class="ruby-identifier">b</span>)
791
802
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
792
803
  <span class="ruby-keyword">end</span></pre>
@@ -819,7 +830,7 @@
819
830
 
820
831
  <div class="method-source-code" id="starts_after_other_ends-3F-source">
821
832
  <pre>
822
- <span class="ruby-comment"># File lib/time_span.rb, line 70</span>
833
+ <span class="ruby-comment"># File lib/time_span.rb, line 76</span>
823
834
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_after_other_ends?</span>(<span class="ruby-identifier">b</span>)
824
835
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
825
836
  <span class="ruby-keyword">end</span></pre>
@@ -852,7 +863,7 @@
852
863
 
853
864
  <div class="method-source-code" id="starts_as_other_ends-3F-source">
854
865
  <pre>
855
- <span class="ruby-comment"># File lib/time_span.rb, line 74</span>
866
+ <span class="ruby-comment"># File lib/time_span.rb, line 80</span>
856
867
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_as_other_ends?</span>(<span class="ruby-identifier">b</span>)
857
868
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">ends</span>
858
869
  <span class="ruby-keyword">end</span></pre>
@@ -879,13 +890,18 @@
879
890
 
880
891
  <div class="method-description">
881
892
 
882
-
893
+ <pre>#</pre>
894
+
895
+ <p>single <a href="RelativeTime.html">RelativeTime</a> comparators
896
+ #</p>
897
+
898
+ <pre>#</pre>
883
899
 
884
900
 
885
901
 
886
902
  <div class="method-source-code" id="starts_before-3F-source">
887
903
  <pre>
888
- <span class="ruby-comment"># File lib/time_span.rb, line 21</span>
904
+ <span class="ruby-comment"># File lib/time_span.rb, line 27</span>
889
905
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_before?</span>(<span class="ruby-identifier">b</span>)
890
906
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
891
907
  <span class="ruby-keyword">end</span></pre>
@@ -918,7 +934,7 @@
918
934
 
919
935
  <div class="method-source-code" id="starts_before_or_with-3F-source">
920
936
  <pre>
921
- <span class="ruby-comment"># File lib/time_span.rb, line 38</span>
937
+ <span class="ruby-comment"># File lib/time_span.rb, line 44</span>
922
938
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_before_or_with?</span>(<span class="ruby-identifier">b</span>)
923
939
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
924
940
  <span class="ruby-keyword">end</span></pre>
@@ -951,7 +967,7 @@
951
967
 
952
968
  <div class="method-source-code" id="starts_on_or_after-3F-source">
953
969
  <pre>
954
- <span class="ruby-comment"># File lib/time_span.rb, line 29</span>
970
+ <span class="ruby-comment"># File lib/time_span.rb, line 35</span>
955
971
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_on_or_after?</span>(<span class="ruby-identifier">b</span>)
956
972
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
957
973
  <span class="ruby-keyword">end</span></pre>
@@ -984,7 +1000,7 @@
984
1000
 
985
1001
  <div class="method-source-code" id="starts_with-3F-source">
986
1002
  <pre>
987
- <span class="ruby-comment"># File lib/time_span.rb, line 34</span>
1003
+ <span class="ruby-comment"># File lib/time_span.rb, line 40</span>
988
1004
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">starts_with?</span>(<span class="ruby-identifier">b</span>)
989
1005
  <span class="ruby-identifier">starts</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">starts</span>
990
1006
  <span class="ruby-keyword">end</span></pre>
data/doc/created.rid CHANGED
@@ -1,7 +1,7 @@
1
- Sat, 04 Feb 2012 21:02:13 -0800
1
+ Sun, 05 Feb 2012 20:05:38 -0800
2
2
  ./spec/spec_helper.rb Mon, 30 Jan 2012 19:58:16 -0800
3
3
  ./spec/time_span/time_span_spec.rb Sat, 04 Feb 2012 20:55:54 -0800
4
- ./lib/time_span/version.rb Sat, 04 Feb 2012 20:58:56 -0800
5
- ./lib/time_span.rb Sat, 04 Feb 2012 20:53:50 -0800
4
+ ./lib/time_span/version.rb Sat, 04 Feb 2012 21:03:20 -0800
5
+ ./lib/time_span.rb Sun, 05 Feb 2012 20:01:10 -0800
6
6
  ./Gemfile Mon, 30 Jan 2012 19:39:04 -0800
7
7
  ./Rakefile Mon, 30 Jan 2012 19:39:04 -0800
data/doc/index.html CHANGED
@@ -123,10 +123,10 @@
123
123
 
124
124
  <li><a href="TimeSpan/TimeSpan.html#method-i-starts_with-3F">#starts_with? &mdash; TimeSpan::TimeSpan</a></li>
125
125
 
126
- <li><a href="TimeSpan/RelativeTime.html#method-i-to_s">#to_s &mdash; TimeSpan::RelativeTime</a></li>
127
-
128
126
  <li><a href="TimeSpan/TimeLine.html#method-i-to_s">#to_s &mdash; TimeSpan::TimeLine</a></li>
129
127
 
128
+ <li><a href="TimeSpan/RelativeTime.html#method-i-to_s">#to_s &mdash; TimeSpan::RelativeTime</a></li>
129
+
130
130
  <li><a href="TimeSpan/RelativeTime.html#method-i-valid_and_comparable_with-3F">#valid_and_comparable_with? &mdash; TimeSpan::RelativeTime</a></li>
131
131
 
132
132
  </ul>
@@ -24,7 +24,7 @@
24
24
  <div id="metadata">
25
25
  <dl>
26
26
  <dt class="modified-date">Last Modified</dt>
27
- <dd class="modified-date">2012-02-04 20:58:56 -0800</dd>
27
+ <dd class="modified-date">2012-02-04 21:03:20 -0800</dd>
28
28
 
29
29
 
30
30
  <dt class="requires">Requires</dt>
@@ -24,7 +24,7 @@
24
24
  <div id="metadata">
25
25
  <dl>
26
26
  <dt class="modified-date">Last Modified</dt>
27
- <dd class="modified-date">2012-02-04 20:53:50 -0800</dd>
27
+ <dd class="modified-date">2012-02-05 20:01:10 -0800</dd>
28
28
 
29
29
 
30
30
  <dt class="requires">Requires</dt>
@@ -1,3 +1,3 @@
1
1
  module TimeSpan
2
- VERSION = "0.0.1.alpha05"
2
+ VERSION = "0.0.1.beta.01"
3
3
  end
data/lib/time_span.rb CHANGED
@@ -2,21 +2,34 @@
2
2
 
3
3
  module TimeSpan
4
4
 
5
+ #######################################################################################################
6
+ # this classes' object have a starting and ending RelativeTime, and both times must be on the same TimeLine
7
+ #
8
+ # it implements a large selection of comparators, both for start / end times (single time compartors),
9
+ # and also range comparators
5
10
  class TimeSpan
6
11
 
7
- attr_accessor :starts, :ends # RelativeTime objects
12
+ attr_accessor :starts, :ends, :time_line, :name # RelativeTime objects
8
13
 
9
- ##################################
10
- ## time comparison #
11
- ##################################
14
+ def initialize(starting_at, ending_at, t_line, nom="(unnamed)")
15
+ raise "Cannot make a span unless both points are on the same time_line" unless starting_at.colinear_with?(ending_at)
16
+ self.starts = starting_at
17
+ self.ends = ending_at
18
+ self.time_line = t_line
19
+ self.time_line.spans << self
20
+ self.name = nom
21
+ starting_at.kind_of?(RelativeTime) && ending_at.kind_of?(RelativeTime) && (starting_at <= ending_at)
22
+ end
12
23
 
13
- def initialize(starting_at, ending_at)
14
- raise "Cannot make a span unless both points are on the same timeline" unless starting_at.colinear_with?(ending_at)
15
- self.starts = starting_at
16
- self.ends = ending_at
17
- starting_at.kind_of?(RelativeTime) && ending_at.kind_of?(RelativeTime)
24
+ def endpoint_statuses
25
+ {self => [self.starts.reference_to, self.ends.reference_to]}
18
26
  end
19
27
 
28
+ #######################################################################################################
29
+ # #
30
+ # single RelativeTime comparators #
31
+ # #
32
+ #######################################################################################################
20
33
 
21
34
  def starts_before?(b)
22
35
  starts < b.starts
@@ -80,6 +93,9 @@ module TimeSpan
80
93
  ##################################
81
94
 
82
95
  ## >= and <= intentionally not defined;
96
+ ## logically can only mean starts_after? or ends_before? respectively
97
+ ## which are basically aliased to > & <
98
+ ## unless it is meant < XOR =
83
99
 
84
100
 
85
101
  def == (b)
@@ -115,39 +131,84 @@ module TimeSpan
115
131
  end
116
132
 
117
133
  end
118
- #
134
+
135
+
136
+ #######################################################################################################
137
+ # class TimeLine #
138
+ # #
139
+ # TimeLine is how RelativeTime objects are related to each other, they have to have the same #
140
+ # frame of reference. #
141
+ # #
142
+ # instance methods: #
143
+ # #
144
+ # to_s -- convenience method to see which object we have #
145
+ # position_of(obj) -- its location on the TimeLine #
146
+ # increase_after(position, amount) -- make room in the index for an insertion #
147
+ # append (obj) -- add obj to end of the TimeLine #
148
+ # append_to_next(relative_object, object, relative) at relative offset to relative_object, put obj #
149
+ # insert_before_next(relative_object, object, relative) inserts into new time slot relative to relative_object
150
+ # insert_at(position, object) -- places object at position, appending if equal time exists #
151
+ # remove(obj) -- remove object from the TimeLine #
152
+ # compress! -- compresses the TimeLine, removing [] and adjusting the index accordingly #
153
+ # #
154
+ #######################################################################################################
119
155
  class TimeLine < Array
120
156
 
121
- attr_accessor :line, :indices_of, :name
157
+ attr_accessor :line, :indices_of, :name, :spans
122
158
 
123
159
  def to_s
124
160
  name.to_s
125
161
  end
126
162
 
163
+ def inspect
164
+ line.inspect + name
165
+ end
166
+
167
+ def all_endpoint_statuses
168
+ spans.inject({}){ |acc, span| acc.merge!(span.endpoint_statuses) }
169
+ end
170
+
171
+ ## attached times only
172
+ def relative_times
173
+ indices_of.keys
174
+ end
175
+
176
+ def all_relative_time_statuses
177
+ relative_times.inject([]) {|acc, v| acc << v.reference_to }
178
+ end
179
+
127
180
  def initialize(name="")
128
181
  @name = name
129
182
  @line = []
130
183
  @indices_of = {}
184
+ @spans = []
131
185
  end
132
186
 
187
+ ## indices methods
188
+
133
189
  def position_of(obj)
134
190
  @indices_of[obj]
135
191
  end
136
192
 
137
- def append(obj)
138
- insert_at(@line.size, obj)
139
- end
140
-
141
193
  def increase_after(pos, by=1)
142
194
  @indices_of.each_key do |key|
143
195
  @indices_of[key] += by if (@indices_of[key] >= pos)
144
196
  end
145
197
  end
146
198
 
199
+ ## insertion
200
+
201
+ # add to the end of the TimeLine
202
+ def append(obj)
203
+ insert_at(@line.size, obj)
204
+ end
205
+
206
+ # inserts to the end of the relative object's time, becoming equal with it
147
207
  def append_to_next(relative_obj, obj, relative=1)
148
208
  insert_at(position_of(relative_obj)+relative, obj)
149
209
  end
150
210
 
211
+ ## inserts into a new space before the relative object (first parameter)
151
212
  def insert_before_next(relative_obj, obj, relative_offset=1)
152
213
  relative_position = position_of(relative_obj)
153
214
  increase_after(relative_position + relative_offset, relative_offset)
@@ -155,76 +216,75 @@ module TimeSpan
155
216
  insert_at(relative_position+relative_offset, obj)
156
217
  end
157
218
 
158
- ## needs work for RelativeTime objects
219
+ ## place obj at the numbered position
159
220
  def insert_at(pos, obj)
160
-
161
- raise "can only add a time to its own timeline" unless obj.timeline.equal? self
221
+ raise "can only add a time to its own time_line" unless obj.time_line.equal? self
162
222
  if @line[pos].nil?
163
223
  @line[pos] = [obj]
164
- @indices_of[obj] = pos
165
224
  else
166
225
  op = @line[pos].kind_of?(Array) ? '<<' : '='
167
- @line[pos].send(op.to_sym, obj) unless @line[pos].include?(obj) # no dupliates
168
- @indices_of[obj] = pos # dup overwrites
226
+ @line[pos].send(op.to_sym, obj).uniq! # no duplicates in same position
227
+ # dup in diff position overwrites below
169
228
  end
229
+ @indices_of[obj] = pos
170
230
  end
171
231
 
232
+ # cannot remove [] or the @indices_of will be wrong
233
+ # call #compress to remove the extra []s
172
234
  def remove(obj)
173
- # cannot remove [] or the @indices_of will be wrong
174
- # call to #compress to remove the extra []s
175
235
  pos = position_of(obj)
176
-
177
236
  if pos # do nothing if it isn't there'
178
- @line[pos].delete(obj)
179
- @indices_of.delete(obj)
237
+ @line[pos].delete(obj) # remove from list
238
+ @indices_of.delete(obj) # remove from index
180
239
  end
181
-
182
240
  end
183
241
 
184
242
  ## removes all [] elements, and decrements accordingly the @indices_of
185
243
  ## ideally this should be transactional
186
-
187
244
  def compress!
188
-
189
245
  mod_level = 0
190
246
  offsets = []
191
247
  0.upto(line.size-1) do |i|
192
248
  mod_level -= 1 if @line[i].empty?
193
249
  offsets << mod_level
194
250
  end
195
-
196
251
  ## poor man's transaction. Don't do directly on indices_of so less chance of interruption
197
252
  indices = indices_of
198
-
199
- ## refactor to use increase_after
200
253
  indices.each_key do |key|
201
254
  indices[key] = indices[key] + offsets[indices[key]]
202
255
  end
203
-
204
256
  indices_of = indices
205
257
  @line.delete([])
206
258
  end
207
259
 
208
260
  end
209
261
 
210
- ###########################################################################################
211
- # #
212
- # A relative time must be able to be have equal times #
213
- # implemented as an Array of Arrays; all elements of the 'subarray' are equal in time, #
214
- # otherwise, the time relationship are based on position in the main array #
215
- # #
216
- # diff cannot be done, it makes no sense, due to the fuzziness #
217
- # #
218
- # RelativeTime must be within a TimeLine #
219
- ###########################################################################################
220
-
262
+ #######################################################################################################
263
+ # class RelativeTime #
264
+ # #
265
+ # public methods: #
266
+ # comparators: < <= == != >= > #
267
+ # work on any two RelativeTime objects on the same TimeLine #
268
+ # positioned? #
269
+ # true if a RelativeTime has been put on a TimeLine # #
270
+ # colinear_with?(RelativeTime.new) #
271
+ # true if both RelativeTime objects are positioned and on same TimeLine #
272
+ # #
273
+ # protected method: #
274
+ # valid_and_comparable_with?(RelativeTime) #
275
+ # true if #
276
+ # #
277
+ # diff cannot be done, it makes no sense, due to the fuzziness #
278
+ # #
279
+ # RelativeTime must be within a TimeLine #
280
+ #######################################################################################################
221
281
  class RelativeTime
222
282
 
223
- attr_accessor :timeline, :reference_to # reference_to should respond_to? :to_s
283
+ attr_accessor :time_line, :reference_to # reference_to should respond_to? :to_s
224
284
 
225
- # create a realtive time *within a timeline* after position
285
+ # create a realtive time *within a time_line* after position
226
286
  def initialize tline, ref
227
- @timeline= tline
287
+ @time_line= tline
228
288
  @reference_to= ref
229
289
  end
230
290
 
@@ -235,24 +295,24 @@ module TimeSpan
235
295
  ## any method on fixnum with 1 RelativeTime param can be in the list below
236
296
  %w{< <= == != >= >}.each{ |meth|
237
297
  self.send(:define_method, meth) {|b|
238
- raise "can only compare to other times on the same timeline." unless valid_and_comparable_with?(b) # can NOT compare across TimeLines
239
- self.timeline.position_of(self).send(meth, b.timeline.position_of(b))
298
+ raise "can only compare to other times on the same time_line." unless valid_and_comparable_with?(b) # can NOT compare across TimeLines
299
+ self.time_line.position_of(self).send(meth, b.time_line.position_of(b))
240
300
  }
241
301
  }
242
302
 
243
303
  def positioned?
244
- self.timeline.indices_of.keys.include?(self)
304
+ self.time_line && self.time_line.indices_of.include?(self)
245
305
  end
246
306
 
247
307
  def colinear_with?(b)
248
- self.timeline.equal?(b.timeline)
308
+ b.kind_of?(self.class) && b.positioned? && positioned? && time_line.equal?(b.time_line)
249
309
  end
250
310
 
251
311
 
252
312
  protected
253
313
 
254
314
  def valid_and_comparable_with?(b)
255
- !self.timeline.nil? && !b.timeline.nil? && colinear_with?(b)
315
+ !self.time_line.nil? && !b.time_line.nil? && colinear_with?(b)
256
316
  end
257
317
 
258
318
 
@@ -22,20 +22,40 @@ describe "TimeSpan" do
22
22
  TimeSpan::RelativeTime.new timeline, "Some timepoint d"
23
23
  end
24
24
 
25
+ let (:time_span) do
26
+ TimeSpan::TimeSpan.new(time_a, time_c, timeline, "testing time span")
27
+ end
28
+
29
+
25
30
  before(:each) do
26
31
  timeline.append time_a
27
32
  timeline.append time_b
28
33
  timeline.append time_c
29
34
  end
30
35
 
31
- let (:time_span) do
32
- TimeSpan::TimeSpan.new(time_a, time_c)
33
- end
34
36
 
35
37
  context "TimeSpan::TimeLine" do
36
38
 
37
39
  context "instance methods" do
38
40
 
41
+ context "statues" do
42
+
43
+ it "should get all the endpoint statuses" do
44
+ timeline.all_endpoint_statuses.should == {time_span => [time_span.starts.reference_to, time_span.ends.reference_to]}
45
+ end
46
+
47
+ it "knows the statuses for all associated TimeSpan objects" do
48
+ timeline.all_relative_time_statuses.sort.should == ["Some timepoint a", "Some timepoint b", "Some timepoint c"]
49
+ end
50
+
51
+ end
52
+
53
+ context "spans" do
54
+ it "the TimeLine knows its TimeSpan list" do
55
+ timeline.spans.should include(time_span)
56
+ end
57
+ end
58
+
39
59
  context "insertion" do
40
60
 
41
61
  it "inserts a TimeSpan::RelativeTime into the timeline." do
@@ -172,12 +192,21 @@ describe "TimeSpan" do
172
192
 
173
193
  end
174
194
 
195
+ context "relative status methods" do
196
+
197
+ it "should get the relative statuses for the endpoints" do
198
+ time_span.endpoint_statuses.should == { time_span => [time_a.reference_to, time_c.reference_to]}
199
+ end
200
+
201
+ end
202
+
203
+
175
204
  context "single time point comparators" do
176
205
 
177
206
  context "same end time, time_span starts before other time_span" do
178
207
 
179
208
  let (:time_span_2) do
180
- TimeSpan::TimeSpan.new(time_b, time_c)
209
+ TimeSpan::TimeSpan.new(time_b, time_c, timeline)
181
210
  end
182
211
 
183
212
  it "start before other time_span" do
@@ -243,19 +272,19 @@ describe "TimeSpan" do
243
272
  end
244
273
 
245
274
  let (:time_span_mid) do
246
- TimeSpan::TimeSpan.new(time_b, time_c)
275
+ TimeSpan::TimeSpan.new(time_b, time_c, timeline)
247
276
  end
248
277
 
249
278
  let(:time_span_all) do
250
- TimeSpan::TimeSpan.new(time_a, time_e)
279
+ TimeSpan::TimeSpan.new(time_a, time_e, timeline)
251
280
  end
252
281
 
253
282
  let (:time_span_last) do
254
- TimeSpan::TimeSpan.new(time_d, time_e)
283
+ TimeSpan::TimeSpan.new(time_d, time_e, timeline)
255
284
  end
256
285
 
257
286
  it "identical start and end times should return == as true" do
258
- time_span_3 = TimeSpan::TimeSpan.new(time_a, time_c)
287
+ time_span_3 = TimeSpan::TimeSpan.new(time_a, time_c, timeline)
259
288
  time_span.should == time_span_3
260
289
  end
261
290
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_span
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha05
4
+ version: 0.0.1.beta.01
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-05 00:00:00.000000000 Z
12
+ date: 2012-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &85009820 !ruby/object:Gem::Requirement
16
+ requirement: &77411640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *85009820
24
+ version_requirements: *77411640
25
25
  description: Time spans, including many comparators
26
26
  email:
27
27
  - craig.a.cook@gmail.com
@@ -31,6 +31,7 @@ extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
33
  - Gemfile
34
+ - README.md
34
35
  - Rakefile
35
36
  - doc/Gemfile.html
36
37
  - doc/Rakefile.html
@@ -100,3 +101,4 @@ signing_key:
100
101
  specification_version: 3
101
102
  summary: Time Span
102
103
  test_files: []
104
+ has_rdoc: