class Gardner::Plot
The Plot
class handles a specific tree file. It provides functionality for parsing trunks and branches, and provides these as class attributes.
Attributes
The trunk and branches instance variables
The trunk and branches instance variables
The trunk and branches instance variables
Public Class Methods
Initialize a new Plot
from a tree file
@param tree [File] The dialogue tree file
# File lib/sapling/gardner.rb, line 14 def initialize(file) @tree = file prune_trunk prune_branches end
Public Instance Methods
Parse the tree array into an array of numbered branches, and ordered leaves.
@param tree [File] The dialogue tree @return [Array] An array of numbered branches, with numbered leaves
# File lib/sapling/gardner.rb, line 25 def prune_branches @branches = { 0 => { 'desc' => 'Thanks for using Sapling!' } } @tree.each do |b| @branches[b['branch']['number']] = { 'desc' => b['branch']['text'], 'options' => prune_leaves(b['branch']['leaf']) } end end
Parse the leaves of a branch into a numbered hash of options.
@param leaves [Array] The option of leaf hashes @return [Hash] A numbered hash of options
# File lib/sapling/gardner.rb, line 39 def prune_leaves(leaves) x = 1 options = {} return options if leaves.nil? leaves.each do |l| options[x] = { l['text'] => l['branch'] } x += 1 end options end
Parse the trunk of the tree.
@return [Array] The trunk, and the remainder of the tree
# File lib/sapling/gardner.rb, line 56 def prune_trunk @trunk = @tree.shift end