Module: SegmentRanges
Class Method Summary collapse
Instance Method Summary collapse
- #includes?(segment) ⇒ Boolean
- #make_relative(segments, &block) ⇒ Object
- #overlaps(segments) ⇒ Object
- #overlaps?(segment) ⇒ Boolean
- #pull(offset) ⇒ Object
- #push(offset) ⇒ Object
- #range_in(container = nil) ⇒ Object
Class Method Details
.collisions(main, secondary) ⇒ Object
58 59 60 61 62 |
# File 'lib/rbbt/segment/overlaps.rb', line 58 def self.collisions(main, secondary) secondary.select do |ss| main.select{|ms| ms.overlaps? ss }.any? end end |
Instance Method Details
#includes?(segment) ⇒ Boolean
44 45 46 47 |
# File 'lib/rbbt/segment/overlaps.rb', line 44 def includes?(segment) (segment.offset.to_i >= self.offset.to_i) and (segment.offset.to_i + segment.segment_length.to_i <= self.offset.to_i + self.segment_length.to_i) end |
#make_relative(segments, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/rbbt/segment/overlaps.rb', line 22 def make_relative(segments, &block) if block_given? segments.each{|s| s.push offset} yield(segments) segments.each{|s| s.pull offset} else segments.each{|s| s.push offset} end end |
#overlaps(segments) ⇒ Object
54 55 56 |
# File 'lib/rbbt/segment/overlaps.rb', line 54 def overlaps(segments) segments.select{|s| self.overlaps?(s) } end |
#overlaps?(segment) ⇒ Boolean
49 50 51 52 |
# File 'lib/rbbt/segment/overlaps.rb', line 49 def overlaps?(segment) segment.offset.to_i >= self.offset.to_i && segment.offset.to_i <= self.eend || self.offset.to_i >= segment.offset.to_i && self.offset.to_i <= segment.eend end |
#pull(offset) ⇒ Object
2 3 4 5 6 7 8 9 10 |
# File 'lib/rbbt/segment/overlaps.rb', line 2 def pull(offset) if self.offset.nil? or offset.nil? self.offset = nil else self.offset += offset end self end |
#push(offset) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/rbbt/segment/overlaps.rb', line 12 def push(offset) if self.offset.nil? or offset.nil? self.offset = nil else self.offset -= offset end self end |
#range_in(container = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rbbt/segment/overlaps.rb', line 32 def range_in(container = nil) raise "No offset specified" if offset.nil? case when (Segment === container and not container.offset.nil?) ((offset - container.offset)..(self.eend - container.offset)) when Integer === container ((offset - container)..(self.eend - container)) else range end end |