Class: EPUB::CFI::Range

Inherits:
Range
  • Object
show all
Defined in:
lib/epub/cfi.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endObject

Returns the value of attribute end.



134
135
136
# File 'lib/epub/cfi.rb', line 134

def end
  @end
end

#parentObject

Returns the value of attribute parent.



134
135
136
# File 'lib/epub/cfi.rb', line 134

def parent
  @parent
end

#startObject

Returns the value of attribute start.



134
135
136
# File 'lib/epub/cfi.rb', line 134

def start
  @start
end

Class Method Details

.from_parent_and_start_and_end(parent_path, start_subpath, end_subpath) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/epub/cfi.rb', line 139

def from_parent_and_start_and_end(parent_path, start_subpath, end_subpath)
  start_str = start_subpath.join
  end_str = end_subpath.join

  first_paths = parent_path.collect(&:dup)
  if start_subpath
    offset_of_first = start_subpath.last.offset
    offset_of_first = offset_of_first.dup if offset_of_first
    last_of_first_paths = first_paths.pop
    first_paths << last_of_first_paths
    last_of_first_paths.steps.concat start_subpath.shift.steps
    first_paths.concat start_subpath
    first_paths.last.instance_variable_set :@offset, offset_of_first
  end
  offset_of_last = end_subpath.last.offset
  offset_of_last = offset_of_last.dup if offset_of_last
  last_paths = parent_path.collect(&:dup)
  last_of_last_paths = last_paths.pop
  last_paths << last_of_last_paths
  last_of_last_paths.steps.concat end_subpath.shift.steps
  last_paths.concat end_subpath
  last_paths.last.instance_variable_set :@offset, offset_of_last

  first = CFI::Location.new(first_paths)
  last = CFI::Location.new(last_paths)

  new_range = new(first, last)

  new_range.parent = Location.new(parent_path)
  new_range.start = start_str
  new_range.end = end_str

  new_range
end

Instance Method Details

#to_fragmentObject



179
180
181
# File 'lib/epub/cfi.rb', line 179

def to_fragment
  @fragment_cache ||= "epubcfi(#{@parent},#{@start},#{@end})".freeze
end

#to_sObject



175
176
177
# File 'lib/epub/cfi.rb', line 175

def to_s
  @string_cache ||= (first.to_fragment + (exclude_end? ? '...' : '..') + last.to_fragment).freeze
end