Class: EPUB::CFI::Location

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

Overview

Location indicates a point in an EPUB Publication.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ Location

Returns a new instance of Location.



59
60
61
# File 'lib/epub/cfi.rb', line 59

def initialize(paths=[])
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



36
37
38
# File 'lib/epub/cfi.rb', line 36

def paths
  @paths
end

Class Method Details

.from_parent_and_subpath(parent_path, subpath) ⇒ Object



39
40
41
# File 'lib/epub/cfi.rb', line 39

def from_parent_and_subpath(parent_path, subpath)
  new(resolve_path(parent_path, subpath))
end

Instance Method Details

#<=>(other) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/epub/cfi.rb', line 67

def <=>(other)
  index = 0
  other_paths = other.paths
  cmp = nil
  paths.each do |path|
    other_path = other_paths[index]
    return 1 unless other_path
    cmp = path <=> other_path
    break unless cmp == 0
    index += 1
  end

  unless cmp == 0
    if cmp == 1 and paths[index].offset and other_paths[index + 1]
      return nil
    else
      return cmp
    end
  end

  return nil if paths.last.offset && other_paths[index]

  return -1 if other_paths[index]

  0
end

#initialize_copy(original) ⇒ Object



63
64
65
# File 'lib/epub/cfi.rb', line 63

def initialize_copy(original)
  @paths = original.paths.collect(&:dup)
end

#inspectObject



102
103
104
# File 'lib/epub/cfi.rb', line 102

def inspect
  "#<#{self.class}:#{path_string}>"
end

#join(*other_paths) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/epub/cfi.rb', line 106

def join(*other_paths)
  new_paths = paths.dup
  other_paths.each do |path|
    new_paths << path
  end
  self.class.new(new_paths)
end

#path_stringObject



94
95
96
# File 'lib/epub/cfi.rb', line 94

def path_string
  paths.join('!')
end

#to_sObject



98
99
100
# File 'lib/epub/cfi.rb', line 98

def to_s
 "epubcfi(#{path_string})"
end