Class: TimeSpan::RelativeTime

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

Overview

class RelativeTime #

#

public methods: #

comparators: < <= == != >= >                                                                      #
   work on any two RelativeTime objects on the same TimeLine                                      #
positioned?                                                                                       #
   true if a RelativeTime has been put on a TimeLine                                              #                                                                                   #
colinear_with?(RelativeTime.new)                                                                  #
   true if both RelativeTime objects are positioned and on same TimeLine                          #
                                                                                                  #

protected method: #

valid_and_comparable_with?(RelativeTime)                                                          #
   true if                                                                                        #
                                                                                                  #

diff cannot be done, it makes no sense, due to the fuzziness #

                                                                                                   #
RelativeTime must be within a TimeLine                                                             #

Author:

  • Craig A. Cook

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tline, ref) ⇒ Object

create a realtive time *within a time_line* after position

Parameters:

  • tline (TimeSpan::TimeLine)

    TimeLIne on which this RelativeTime is placed

  • ref (Object)

    the object placed on the timeline



421
422
423
424
# File 'lib/time_span.rb', line 421

def initialize  tline, ref
   @time_line= tline
   @reference_to= ref
end

Instance Attribute Details

#reference_toObject

Object (PORO) reference_to should respond_to? :to_s



415
416
417
# File 'lib/time_span.rb', line 415

def reference_to
  @reference_to
end

#time_lineObject

TimeLine on which this RelativeTime is placed



414
415
416
# File 'lib/time_span.rb', line 414

def time_line
  @time_line
end

Instance Method Details

#cloneObject

Raises:

  • (NotImplementedError)

    cannot do without much more work



427
428
429
# File 'lib/time_span.rb', line 427

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

#colinear_with?(other_relative_time) ⇒ Boolean

Returns true if both are on the same TimeLine.

Parameters:

Returns:

  • (Boolean)

    true if both are on the same TimeLine



456
457
458
# File 'lib/time_span.rb', line 456

def colinear_with?(other_relative_time)
  other_relative_time.kind_of?(self.class) &&  other_relative_time.positioned? && positioned? &&  time_line.equal?(other_relative_time.time_line)
end

#methBoolean

comparator methods any method on fixnum with 1 RelativeTime param can be in the list below

Parameters:

Returns:

  • (Boolean)

    depending on the relationship



442
443
444
445
446
447
# File 'lib/time_span.rb', line 442

%w{< <= == != >= >}.each{ |meth|
  self.send(:define_method, meth) {|other_relative_time|
    raise ArgumentError, "can only compare to other times on the same time_line." unless valid_and_comparable_with?(other_relative_time)     # can NOT compare across TimeLines
    self.time_line.position_of(self).send(meth, other_relative_time.time_line.position_of(other_relative_time))
  }
}

#positioned?Boolean

Returns true if self has been properly placed on a TimeLine.

Returns:

  • (Boolean)

    true if self has been properly placed on a TimeLine



450
451
452
# File 'lib/time_span.rb', line 450

def positioned?
  self.time_line && self.time_line.indices_of.include?(self)
end

#to_sString

Returns the string representation of referenced object.

Returns:

  • (String)

    the string representation of referenced object



434
435
436
# File 'lib/time_span.rb', line 434

def to_s
  @reference_to.to_s
end