Class: EPUB::CFI::Location

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ Location

Returns a new instance of Location.



44
45
46
# File 'lib/epub/cfi.rb', line 44

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

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



21
22
23
# File 'lib/epub/cfi.rb', line 21

def paths
  @paths
end

Class Method Details

.from_parent_and_subpath(parent_path, subpath) ⇒ Object



24
25
26
# File 'lib/epub/cfi.rb', line 24

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

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/epub/cfi.rb', line 52

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



48
49
50
# File 'lib/epub/cfi.rb', line 48

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

#inspectObject



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

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

#join(*other_paths) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/epub/cfi.rb', line 91

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



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

def path_string
  paths.join('!')
end

#to_sObject



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

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