Class: Origami::PageTreeNode
- Inherits:
-
Dictionary
- Object
- Hash
- Dictionary
- Origami::PageTreeNode
- Includes:
- StandardObject
- Defined in:
- lib/origami/page.rb
Overview
Class representing a node in a Page tree.
Constant Summary
Constants included from StandardObject
StandardObject::DEFAULT_ATTRIBUTES
Constants inherited from Dictionary
Constants included from Object
Instance Attribute Summary
Attributes included from ObjectCache
#names_cache, #strings_cache, #xref_cache
Attributes included from Object
#file_offset, #generation, #no, #objstm_offset, #parent
Instance Method Summary collapse
-
#append_page(page) ⇒ Object
Append a page at the end of this node.
-
#clear_pages ⇒ Object
Removes all pages in the node.
-
#each_page(browsed_nodes: [], &block) ⇒ Object
Iterate through each page of that node.
-
#get_page(n) ⇒ Object
Get the n-th Page object in this node, starting from 1.
-
#initialize(hash = {}, parser = nil) ⇒ PageTreeNode
constructor
A new instance of PageTreeNode.
-
#insert_page(n, page) ⇒ Object
Inserts a page into the node at a specified position (starting from 1).
-
#pages ⇒ Object
Returns an Array of Pages inheriting this tree node.
-
#pages? ⇒ Boolean
Returns true unless the node is empty.
-
#pre_build ⇒ Object
:nodoc:.
Methods included from StandardObject
Methods inherited from Dictionary
#[], #[]=, hint_type, #merge, parse, #to_h, #to_obfuscated_str, #to_s, #transform_values, #transform_values!
Methods included from TypeGuessing
Methods included from FieldAccessor
#method_missing, #respond_to_missing?
Methods included from CompoundObject
#copy, #delete, #include?, #update_values, #update_values!
Methods included from ObjectCache
Methods included from Object
#cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, #numbered?, parse, #post_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #version_required, #xrefs
Constructor Details
#initialize(hash = {}, parser = nil) ⇒ PageTreeNode
Returns a new instance of PageTreeNode.
280 281 282 283 284 285 |
# File 'lib/origami/page.rb', line 280 def initialize(hash = {}, parser = nil) super set_default_values # Ensure that basic tree fields are present. set_indirect(true) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Origami::FieldAccessor
Instance Method Details
#append_page(page) ⇒ Object
Append a page at the end of this node.
402 403 404 405 406 407 408 |
# File 'lib/origami/page.rb', line 402 def append_page(page) self.Kids ||= [] self.Kids.push(page) self.Count += 1 page.Parent = self end |
#clear_pages ⇒ Object
Removes all pages in the node.
387 388 389 390 |
# File 'lib/origami/page.rb', line 387 def clear_pages self.Count = 0 self.Kids = [] end |
#each_page(browsed_nodes: [], &block) ⇒ Object
Iterate through each page of that node.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/origami/page.rb', line 345 def each_page(browsed_nodes: [], &block) return enum_for(__method__) { self.Count.to_i } unless block_given? if browsed_nodes.any?{|node| node.equal?(self)} raise InvalidPageTreeError, "Cyclic tree graph detected" end unless self.Kids.is_a?(Array) raise InvalidPageTreeError, "Kids must be an Array" end browsed_nodes.push(self) unless self.Count.nil? [ self.Count.value, self.Kids.length ].min.times do |n| node = self.Kids[n].solve case node when PageTreeNode then node.each_page(browsed_nodes: browsed_nodes, &block) when Page then yield(node) else raise InvalidPageTreeError, "not a Page or PageTreeNode" end end end self end |
#get_page(n) ⇒ Object
Get the n-th Page object in this node, starting from 1.
377 378 379 380 381 382 |
# File 'lib/origami/page.rb', line 377 def get_page(n) raise IndexError, "Page numbers are referenced starting from 1" if n < 1 raise IndexError, "Page not found" if n > self.Count.to_i self.each_page.lazy.drop(n - 1).first or raise IndexError, "Page not found" end |
#insert_page(n, page) ⇒ Object
Inserts a page into the node at a specified position (starting from 1).
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/origami/page.rb', line 296 def insert_page(n, page) raise IndexError, "Page numbers are referenced starting from 1" if n < 1 kids = self.Kids unless kids.is_a?(Array) raise InvalidPageTreeError, "Kids must be an Array" end count = 0 kids.each_with_index do |kid, index| node = kid.solve case node when Page count = count + 1 if count == n kids.insert(index, page) page.Parent = self self.Count += 1 return self end when PageTreeNode count = count + node.Count if count >= n node.insert_page(n - count + node.Count, page) self.Count += 1 return self end else raise InvalidPageTreeError, "not a Page or PageTreeNode" end end raise IndexError, "Out of order page index" unless count + 1 == n self.append_page(page) end |
#pages ⇒ Object
Returns an Array of Pages inheriting this tree node.
338 339 340 |
# File 'lib/origami/page.rb', line 338 def pages self.each_page.to_a end |
#pages? ⇒ Boolean
Returns true unless the node is empty.
395 396 397 |
# File 'lib/origami/page.rb', line 395 def pages? self.each_page.size > 0 end |
#pre_build ⇒ Object
:nodoc:
287 288 289 290 291 |
# File 'lib/origami/page.rb', line 287 def pre_build #:nodoc: self.Count = self.pages.count super end |