Parent

Class/Module Index [+]

Quicksearch

Origami::PageTreeNode

Class representing a node in a Page tree.

Constants

Count
Kids
Parent

Public Class Methods

new(hash = {}) click to toggle source
# File lib/origami/page.rb, line 262
def initialize(hash = {})
  self.Count = 0
  self.Kids = []

  super(hash)
  
  set_indirect(true)
end

Public Instance Methods

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

  self.Kids ||= Array.new
  self.Kids.concat(pageset)
  self.Count = self.Kids.length
    
  pageset.each do |node| 
    node.Parent = self 
  end
end
children() click to toggle source

Returns an array of Page inheriting this tree node.

# File lib/origami/page.rb, line 323
def children
  pageset = []
 
  unless self.Count.nil?
    [ self.Count.value, self.Kids.length ].min.times do |n|
      node = self.Kids[n].is_a?(Reference) ? self.Kids[n].solve : self.Kids[n]
      case node
        when PageTreeNode then pageset.concat(node.children) 
        when Page then pageset << node
      end
    end
  end
  
  pageset
end
each_page(&b) click to toggle source

Iterate through each page of that node.

# File lib/origami/page.rb, line 342
def each_page(&b)
  unless self.Count.nil?
    [ self.Count.value, self.Kids.length ].min.times do |n|
      node = self.Kids[n].is_a?(Reference) ? self.Kids[n].solve : self.Kids[n]
      case node
        when PageTreeNode then node.each_page(&b)
        when Page then b.call(node)
      end
    end
  end
end
get_page(n) click to toggle source

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

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

  decount = n
  loop do
    [ self.Count.value, self.Kids.length ].min.times do |i|
      node = self.Kids[i].is_a?(Reference) ? self.Kids[i].solve : self.Kids[i]

      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)
          else
            decount -= nchilds
          end
      end
    end
  end
end
insert_page(index, page) click to toggle source
# File lib/origami/page.rb, line 277
def insert_page(index, page)
  
  if index > self.Count
    raise IndexError, "Invalid index for page tree"
  end

  count = 0
  kids = self.Kids

  kids.length.times { |n|
    if count == index
      kids.insert(n, page)
      self.Count = self.Count + 1
      page.Parent = self
      return self
    else
      node = kids[n].is_a?(Reference) ? kids[n].solve : kids[n]
      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
  }

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

  self
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.