Class: EPUB::CFI::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps = [], offset = nil) ⇒ Path

Returns a new instance of Path.



78
79
80
# File 'lib/epub/cfi.rb', line 78

def initialize(steps=[], offset=nil)
  @steps, @offset = steps, offset
end

Instance Attribute Details

#offsetObject (readonly)

Returns the value of attribute offset.



76
77
78
# File 'lib/epub/cfi.rb', line 76

def offset
  @offset
end

#stepsObject (readonly)

Returns the value of attribute steps.



76
77
78
# File 'lib/epub/cfi.rb', line 76

def steps
  @steps
end

Instance Method Details

#<=>(other) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/epub/cfi.rb', line 95

def <=>(other)
  other_steps = other.steps
  index = 0
  steps.each do |step|
    other_step = other_steps[index]
    return 1 unless other_step
    cmp = step <=> other_step
    return cmp unless cmp == 0
    index += 1
  end

  return -1 if other_steps[index]

  other_offset = other.offset
  if offset
    if other_offset
      offset <=> other_offset
    else
      1
    end
  else
    if other_offset
      -1
    else
      0
    end
  end
end

#each_step_with_instruction {|[step, nil]| ... } ⇒ Object

Yields:

  • ([step, nil])


124
125
126
127
128
129
130
# File 'lib/epub/cfi.rb', line 124

def each_step_with_instruction
  yield [step, nil]
  local_path.each_step_with_instruction do |s, instruction|
    yield [s, instruction]
  end
  self
end

#initialize_copy(original) ⇒ Object



82
83
84
85
# File 'lib/epub/cfi.rb', line 82

def initialize_copy(original)
  @steps = original.steps.collect(&:dup)
  @offset = original.offset.dup if original.offset
end

#to_fragmentObject



91
92
93
# File 'lib/epub/cfi.rb', line 91

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

#to_sObject



87
88
89
# File 'lib/epub/cfi.rb', line 87

def to_s
  @string_cache ||= (steps.join + offset.to_s).freeze
end