Class: Prawn::Core::NameTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/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.



19
20
21
22
23
24
25
# File 'lib/prawn/core/name_tree.rb', line 19

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.



13
14
15
# File 'lib/prawn/core/name_tree.rb', line 13

def children
  @children
end

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#limitObject (readonly)

Returns the value of attribute limit.



14
15
16
# File 'lib/prawn/core/name_tree.rb', line 14

def limit
  @limit
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#refObject

Returns the value of attribute ref.



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

def ref
  @ref
end

Instance Method Details

#<<(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/prawn/core/name_tree.rb', line 72

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

  value
end

#>=(value) ⇒ Object



87
88
89
# File 'lib/prawn/core/name_tree.rb', line 87

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

#add(name, value) ⇒ Object



39
40
41
# File 'lib/prawn/core/name_tree.rb', line 39

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.



104
105
106
107
108
109
110
111
# File 'lib/prawn/core/name_tree.rb', line 104

def deep_copy
  node = dup
  node.instance_variable_set("@children",
                             Marshal.load(Marshal.dump(children)))
  node.instance_variable_set("@ref",
                             node.ref ? node.ref.deep_copy : nil)
  node
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/prawn/core/name_tree.rb', line 27

def empty?
  children.empty?
end

#greatestObject



64
65
66
67
68
69
70
# File 'lib/prawn/core/name_tree.rb', line 64

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

#leaf?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/prawn/core/name_tree.rb', line 35

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

#leastObject



56
57
58
59
60
61
62
# File 'lib/prawn/core/name_tree.rb', line 56

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

#sizeObject



31
32
33
# File 'lib/prawn/core/name_tree.rb', line 31

def size
  leaf? ? children.size : children.inject(0) { |sum, child| sum + child.size }
end

#split!Object



91
92
93
94
95
96
97
98
99
# File 'lib/prawn/core/name_tree.rb', line 91

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

#to_hashObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prawn/core/name_tree.rb', line 43

def to_hash
  hash = {}

  hash[:Limits] = [least, greatest] if parent
  if leaf?
    hash[:Names] = children if leaf?
  else
    hash[:Kids] = children.map { |child| child.ref }
  end

  return hash
end