Class: Origami::PageTreeNode

Inherits:
Dictionary show all
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

Dictionary::TOKENS

Constants included from Object

Object::TOKENS

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

Methods included from StandardObject

included, #version_required

Methods inherited from Dictionary

#[], #[]=, hint_type, #merge, parse, #to_h, #to_obfuscated_str, #to_s, #transform_values, #transform_values!

Methods included from TypeGuessing

#guess_type

Methods included from FieldAccessor

#method_missing, #respond_to_missing?

Methods included from CompoundObject

#copy, #delete, #include?, #update_values, #update_values!

Methods included from ObjectCache

#rebuild_caches

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.



318
319
320
321
322
323
# File 'lib/origami/page.rb', line 318

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.



440
441
442
443
444
445
446
# File 'lib/origami/page.rb', line 440

def append_page(page)
  self.Kids ||= []
  self.Kids.push(page)
  self.Count += 1

  page.Parent = self
end

#clear_pagesObject

Removes all pages in the node.



425
426
427
428
# File 'lib/origami/page.rb', line 425

def clear_pages
  self.Count = 0
  self.Kids = []
end

#each_page(browsed_nodes: [], &block) ⇒ Object

Iterate through each page of that node.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/origami/page.rb', line 383

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.

Raises:

  • (IndexError)


415
416
417
418
419
420
# File 'lib/origami/page.rb', line 415

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

  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).

Raises:

  • (IndexError)


334
335
336
337
338
339
340
341
342
343
344
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
# File 'lib/origami/page.rb', line 334

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 += 1
      if count == n
        kids.insert(index, page)
        page.Parent = self
        self.Count += 1
        return self
      end

    when PageTreeNode
      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

  append_page(page)
end

#pagesObject

Returns an Array of Pages inheriting this tree node.



376
377
378
# File 'lib/origami/page.rb', line 376

def pages
  each_page.to_a
end

#pages?Boolean

Returns true unless the node is empty.

Returns:



433
434
435
# File 'lib/origami/page.rb', line 433

def pages?
  each_page.size > 0
end

#pre_buildObject

:nodoc:



325
326
327
328
329
# File 'lib/origami/page.rb', line 325

def pre_build # :nodoc:
  self.Count = pages.count

  super
end