Class: PDF::Core::NameTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/core/name_tree.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, limit, parent = nil) ⇒ Node

Returns a new instance of Node.



21
22
23
24
25
26
27
# File 'lib/pdf/core/name_tree.rb', line 21

def initialize(document, limit, parent = nil)
  @document = document
  @children = []
  @limit = limit
  @parent = parent
  @ref = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



15
16
17
# File 'lib/pdf/core/name_tree.rb', line 15

def children
  @children
end

#documentObject (readonly)

Returns the value of attribute document.



17
18
19
# File 'lib/pdf/core/name_tree.rb', line 17

def document
  @document
end

#limitObject (readonly)

Returns the value of attribute limit.



16
17
18
# File 'lib/pdf/core/name_tree.rb', line 16

def limit
  @limit
end

#parentObject

Returns the value of attribute parent.



18
19
20
# File 'lib/pdf/core/name_tree.rb', line 18

def parent
  @parent
end

#refObject

Returns the value of attribute ref.



19
20
21
# File 'lib/pdf/core/name_tree.rb', line 19

def ref
  @ref
end

Instance Method Details

#<<(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pdf/core/name_tree.rb', line 74

def <<(value)
  if children.empty?
    children << value
  elsif leaf?
    children.insert(insertion_point(value), value)
    split! if children.length > limit
  else
    fit = children.find { |child| child >= value }
    fit ||= children.last
    fit << value
  end

  value
end

#>=(other) ⇒ Object



89
90
91
# File 'lib/pdf/core/name_tree.rb', line 89

def >=(other)
  children.empty? || children.last >= other
end

#add(name, value) ⇒ Object



41
42
43
# File 'lib/pdf/core/name_tree.rb', line 41

def add(name, value)
  self << Value.new(name, value)
end

#deep_copyObject

Returns a deep copy of this node, without copying expensive things like the ref to @document.



107
108
109
110
111
112
# File 'lib/pdf/core/name_tree.rb', line 107

def deep_copy
  node = dup
  node.instance_variable_set('@children', Utils.deep_clone(children))
  node.instance_variable_set('@ref', node.ref ? node.ref.deep_copy : nil)
  node
end

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pdf/core/name_tree.rb', line 29

def empty?
  children.empty?
end

#greatestObject



66
67
68
69
70
71
72
# File 'lib/pdf/core/name_tree.rb', line 66

def greatest
  if leaf?
    children.last.name
  else
    children.last.greatest
  end
end

#leaf?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/pdf/core/name_tree.rb', line 37

def leaf?
  children.empty? || children.first.is_a?(Value)
end

#leastObject



58
59
60
61
62
63
64
# File 'lib/pdf/core/name_tree.rb', line 58

def least
  if leaf?
    children.first.name
  else
    children.first.least
  end
end

#sizeObject



33
34
35
# File 'lib/pdf/core/name_tree.rb', line 33

def size
  leaf? ? children.size : children.sum(&:size)
end

#split!Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/pdf/core/name_tree.rb', line 93

def split!
  if parent
    parent.split(self)
  else
    left = new_node(self)
    right = new_node(self)
    split_children(self, left, right)
    children.replace([left, right])
  end
end

#to_hashObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pdf/core/name_tree.rb', line 45

def to_hash
  hash = {}

  hash[:Limits] = [least, greatest] if parent
  if leaf?
    hash[:Names] = children if leaf?
  else
    hash[:Kids] = children.map(&:ref)
  end

  hash
end