Module: Timed::Moment
Overview
Timed Moment
By including this module into any object that responds to #begin and #end, it can be compared with other moments in time.
To fully support the Moment module the following must hold: a) #begin returns a Numeric value b) #end returns a Numeric value larger than (or equal to) begin c) a new object can be created by a single range-like argument
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if the two moments begin and end on exactly the same time.
-
#after?(other) ⇒ Boolean
Returns true if the moment begins after the other one ends.
-
#before?(other) ⇒ Boolean
Returns true if the moment ends before the other one begins.
-
#duration ⇒ Object
Returns the moment duration.
-
#during?(other) ⇒ Boolean
Returns true if the moment overlaps with the other one.
-
#inspect ⇒ Object
Override the default implementation and provide a more useful representation of the Timed Moment, including when it begins and ends.
-
#intersect(other) ⇒ Object
(also: #&)
Returns a new moment in the intersection.
Instance Method Details
#==(other) ⇒ Object
Returns true if the two moments begin and end on exactly the same time.
other - object that implements both #begin and #end.
25 26 27 28 29 |
# File 'lib/timed/moment.rb', line 25 def ==(other) self.begin == other.begin && self.end == other.end rescue NoMethodError false end |
#after?(other) ⇒ Boolean
Returns true if the moment begins after the other one ends. If given a numeric value it will be treated like instantaneous moment in time.
other - object that implements #end, or a numeric value.
46 47 48 49 |
# File 'lib/timed/moment.rb', line 46 def after?(other) time = other.is_a?(Numeric) ? other : other.end self.begin >= time end |
#before?(other) ⇒ Boolean
Returns true if the moment ends before the other one begins. If given a numeric value it will be treated like instantaneous moment in time.
other - object that implements #begin, or a numeric value.
36 37 38 39 |
# File 'lib/timed/moment.rb', line 36 def before?(other) time = other.is_a?(Numeric) ? other : other.begin self.end <= time end |
#duration ⇒ Object
Returns the moment duration.
17 18 19 |
# File 'lib/timed/moment.rb', line 17 def duration self.end - self.begin end |
#during?(other) ⇒ Boolean
Returns true if the moment overlaps with the other one. If given a numeric value it will be treated like instantaneous moment in time.
other - object that implements both #begin and #end, or a numeric value.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/timed/moment.rb', line 56 def during?(other) if other.is_a? Numeric other_begin = other_end = other else other_begin, other_end = other.begin, other.end end self_begin, self_end = self.begin, self.end # Check if either of the two items begins during the # span of the other other_begin <= self_begin && self_begin <= other_end || self_begin <= other_begin && other_begin <= self_end end |
#inspect ⇒ Object
Override the default implementation and provide a more useful representation of the Timed Moment, including when it begins and ends.
Example
moment.inspect # => "ClassName 12.20 -> 15.50"
Returns a string representation of the object.
92 93 94 |
# File 'lib/timed/moment.rb', line 92 def inspect format '%s %7.2f -> %.2f', self.class.name, self.begin, self.end end |
#intersect(other) ⇒ Object Also known as: &
Returns a new moment in the intersection
other - object that implements both #begin and #end.
75 76 77 78 79 80 |
# File 'lib/timed/moment.rb', line 75 def intersect(other) begin_at = self.begin >= other.begin ? self.begin : other.begin end_at = self.end <= other.end ? self.end : other.end begin_at <= end_at ? self.class.new(begin_at..end_at) : nil end |