Class: PositionRange
- Inherits:
-
Range
- Object
- Range
- PositionRange
- Includes:
- Comparable
- Defined in:
- lib/position_range.rb,
lib/position_range.rb,
lib/position_range/version.rb
Overview
< Range (set in version)
Defined Under Namespace
Constant Summary collapse
- MaximumSize =
Mainly used by PositionRange::List
2 ** 31
- BLOCK_POSITION_RANGE =
Regexp building blocks
'(?:\d+,\d+)'- CHECK_POSITION_RANGE_RE =
Check-regexps
/^#{BLOCK_POSITION_RANGE}$/- VERSION =
'0.7.1'- @@attributes =
[]
Class Method Summary collapse
-
.from_s(position_range_string, options = {}) ⇒ Object
Creates a PositionRange from a string.
Instance Method Summary collapse
-
#-(other) ⇒ Object
Returns this PositionRange substracted by the previous.
-
#<=>(other) ⇒ Object
Comparisons happen in two stages.
-
#===(other) ⇒ Object
Returns true if there is overlap between the PositionRange given as other, and this range.
- #attributes ⇒ Object
-
#has_equal_pointer_attributes?(other_position_range) ⇒ Boolean
Returns true if the pointer_attributes (link and authorship) are equal.
-
#initialize(first, last, options = {}) ⇒ PositionRange
constructor
Initializes a new PositionRange.
-
#new_dup(first, last) ⇒ Object
Duplicates the current object, except for the two required arguments, which set the begin and end positions of the new PositionRange.
-
#size ⇒ Object
Returns the size of the range, that is last - first.
-
#to_s ⇒ Object
Turns a PositionRange into a string.
Constructor Details
#initialize(first, last, options = {}) ⇒ PositionRange
Initializes a new PositionRange.
Note that PositionRanges cannot be descending.
Options:
-
:<any attribute you need>- Usefull for associating Links orRemarks with this range.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/position_range.rb', line 59 def initialize(first, last, = {}) if first < 0 raise PositionRange::Error.new(first, last), 'Tried to create a negative PositionRange' end if first > last raise PositionRange::Error.new(first, last), 'Tried to create a descending PositionRange' end if last > MaximumSize raise PositionRange::Error.new(first, last), 'Tried to create a PositionRange that is' + ' larger than the MaximumSize' end .each_key do |attribute| if !self.respond_to?(attribute) self.define_attribute(attribute.to_s) end self.send(attribute.to_s + '=', [attribute]) end super(first, last, true) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments) ⇒ Object (protected)
Allows the dynamic adding of attributes
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/position_range.rb', line 216 def method_missing(method_id, *arguments) if method_id.to_s[-1..-1] == '=' attribute = method_id.to_s.slice!(0...-1) self.define_attribute(attribute) self.send(method_id.to_s, *arguments) elsif arguments.empty? return nil else super(method_id, *arguments) end end |
Class Method Details
.from_s(position_range_string, options = {}) ⇒ Object
Creates a PositionRange from a string.
The syntax is: <begin position>,<end position>
Where the end position is included in the range.
The optional options var allows one to pass attributes to the new PositionRange
93 94 95 96 97 98 99 100 |
# File 'lib/position_range.rb', line 93 def self.from_s(position_range_string, = {}) if position_range_string !~ CHECK_POSITION_RANGE_RE raise StandardError.new, 'Invalid position_range string given: ' + position_range_string end p_r_arr = position_range_string.split(',') return PositionRange.new(p_r_arr[0].to_i, p_r_arr[1].to_i, ) end |
Instance Method Details
#-(other) ⇒ Object
Returns this PositionRange substracted by the previous
NOTE: The substracted PositionRange must overlap with at least one side of this one. If it’s begin-position is bigger than this one’s and it’s end position smaller than this one’s, no meaningfull output is guaranteed.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/position_range.rb', line 131 def -(other) if other.begin >= self.end or other.end <= self.begin return self elsif other.begin < self.begin and other.end > self.end return nil elsif other.end < self.end return self.new_dup(other.end, self.end) elsif other.begin > self.begin return self.new_dup(self.begin, other.begin) end end |
#<=>(other) ⇒ Object
Comparisons happen in two stages.
First the begin-positions are compared.
4..5 > 1..3 => true
2..3 > 1..3 => true
If those are equal the end-positions are compared.
1..3 > 1..2
If also the end-positions are equal, 0 is returned
1..2 == 1..2
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/position_range.rb', line 177 def <=>(other) if self.begin < other.begin return -1 elsif self.begin > other.begin return 1 else if self.end < other.end return -1 elsif self.end > other.end return 1 else return 0 end end end |
#===(other) ⇒ Object
Returns true if there is overlap between the PositionRange given as other, and this range.
Other values are treated as Range normally does.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/position_range.rb', line 148 def ===(other) if other.kind_of?(PositionRange) if self.size > other.size return ((self) === other.begin or (self) === other.end) else return ((other) === self.begin or (other) === self.end) end else super(other) end end |
#attributes ⇒ Object
45 46 47 |
# File 'lib/position_range.rb', line 45 def attributes return @@attributes end |
#has_equal_pointer_attributes?(other_position_range) ⇒ Boolean
Returns true if the pointer_attributes (link and authorship) are equal
195 196 197 198 199 200 201 202 |
# File 'lib/position_range.rb', line 195 def has_equal_pointer_attributes?(other_position_range) self.attributes.each {|attribute| if self.send(attribute) != other_position_range.send(attribute) return false end } return true end |
#new_dup(first, last) ⇒ Object
Duplicates the current object, except for the two required arguments, which set the begin and end positions of the new PositionRange.
116 117 118 119 120 121 122 |
# File 'lib/position_range.rb', line 116 def new_dup(first, last) attributes_hash = Hash.new self.attributes.each {|attribute| attributes_hash[attribute.to_sym] = self.send(attribute) } PositionRange.new(first, last, attributes_hash) end |
#size ⇒ Object
Returns the size of the range, that is last - first
NOTE that PositionRanges cannot become negative.
108 109 110 |
# File 'lib/position_range.rb', line 108 def size return self.last - self.first end |
#to_s ⇒ Object
Turns a PositionRange into a string
206 207 208 |
# File 'lib/position_range.rb', line 206 def to_s return self.begin.to_s + ',' + self.end.to_s end |