class Origami::PageTreeNode

Class representing a node in a Page tree.

Constants

Count
Kids
Parent

Public Class Methods

new(hash = {}, parser = nil) click to toggle source
Calls superclass method Origami::Dictionary::new
# File lib/origami/page.rb, line 288
def initialize(hash = {}, parser = nil)
    self.Count = 0
    self.Kids = []

    super

    set_indirect(true)
end

Public Instance Methods

<<(pageset) click to toggle source
# File lib/origami/page.rb, line 420
def <<(pageset)
    pageset = [pageset] unless pageset.is_a?(::Array)
    unless pageset.all? {|item| item.is_a?(Page) or item.is_a?(PageTreeNode) }
        raise TypeError, "Cannot add anything but Page and PageTreeNode to this node"
    end

    self.Kids ||= Array.new
    self.Kids.concat(pageset)
    self.Count = self.Kids.length

    pageset.each do |node|
        node.Parent = self
    end
end
each_page(browsed_nodes: []) { |node| ... } click to toggle source

Iterate through each page of that node.

# File lib/origami/page.rb, line 353
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, browsed_nodes: []) click to toggle source

Get the n-th Page object in this node, starting from 1.

# File lib/origami/page.rb, line 385
def get_page(n, browsed_nodes: [])
    raise IndexError, "Page numbers are referenced starting from 1" if n < 1

    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

    decount = n
    [ self.Count.value, self.Kids.length ].min.times do |i|
        node = self.Kids[i].solve

        case node
        when Page
            decount = decount - 1
            return node if decount == 0

        when PageTreeNode
            nchilds = [ node.Count.value, node.Kids.length ].min
            if nchilds >= decount
                return node.get_page(decount, browsed_nodes: browsed_nodes)
            else
                decount -= nchilds
            end
        else
            raise InvalidPageTreeError, "not a Page or PageTreeNode"
        end
    end

    raise IndexError, "Page not found"
end
insert_page(index, page) click to toggle source
# File lib/origami/page.rb, line 303
def insert_page(index, page)
    raise IndexError, "Invalid index for page tree" if index > self.Count

    count = 0
    kids = self.Kids

    kids.length.times do |n|
        if count == index
            kids.insert(n, page)
            self.Count = self.Count + 1
            page.Parent = self
            return self
        else
            node = kids[n].solve
            case node
            when Page
                count = count + 1
                next
            when PageTreeNode
                if count + node.Count > index
                    node.insert_page(index - count, page)
                    self.Count = self.Count + 1
                    return self
                else
                    count = count + node.Count
                    next
                end
            end
        end
    end

    if count == index
        self << page
    else
        raise IndexError, "An error occured while inserting page"
    end

    self
end
pages() click to toggle source

Returns an Array of Pages inheriting this tree node.

# File lib/origami/page.rb, line 346
def pages
    self.each_page.to_a
end