From 6e16b250a5839765437290f4021f43aae5306e8c Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Wed, 18 Oct 2017 23:46:52 -0400 Subject: [PATCH] gardner.rb: Rubocop fixes and refactor changes - Fix several rubocop problems - Update module to more fully encapsulate all dialogue tree handling - Add Plot class for storing all tree data - Add Digiplot class for (eventual) tree editing --- lib/sapling/gardner.rb | 116 +++++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 34 deletions(-) diff --git a/lib/sapling/gardner.rb b/lib/sapling/gardner.rb index e6184bf..1512076 100644 --- a/lib/sapling/gardner.rb +++ b/lib/sapling/gardner.rb @@ -2,47 +2,95 @@ require 'yaml' # Gardner is the module for working with a dialogue tree file module Gardner + # The Plot class handles a specific tree file. It provides functionality for + # parsing trunks and branches, and provides these as class attributes. + class Plot + # The trunk and branches instance variables + attr_reader :branches, :tree, :trunk - # Parse the tree array into an array of numbered branches, and ordered leaves. - # - # @param tree [Array] The dialogue tree - # @return [Array] An array of numbered branches, with numbered leaves - def self.prune_branches(tree) - 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"]) } + # Initialize a new Plot from a tree file + # + # @param tree [File] The dialogue tree file + def initialize(file) + @tree = file + prune_trunk + prune_branches end - return branches - 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 - def self.prune_leaves(leaves) - x = 1 - options = {} - - return options if leaves.nil? - - leaves.each do |l| - options[x] = { l["text"] => l["branch"] } - x += 1 + # 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 + 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 - return options + # 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 + 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 + def prune_trunk + @trunk = @tree.shift + end end - # Parse the trunk of the tree. - # - # @param tree [Hash] The entire tree - # @return [Array] The trunk, and the remainder of the tree - def self.prune_trunk(tree) - trunk = tree.shift + # Digiplot represents a Plot used for editing. The Digiplot functions exactly + # like a Plot, except with additional functionality for over-writing existing + # branches, leaves, and the trunk. + class Digiplot < Plot + # Duplicate the "old" trunk and branches, for restoration purposes + attr_reader :old_branches, :old_trunk - return [trunk,tree] + # Enable editing for the trunk + attr_writer :trunk + + # Initialize a Digiplot just like a Plot, but also copy the trunk and + # branches to "old" instance variables. + def initialize + super + @old_trunk = @trunk + @old_branches = @branches + end + + # Change a branch + # + # @param branch [Integer] the number of the branch to be edited + def branch=(branch, text) + @branches[branch]['desc'] = text + end + + # Change a leaf on a branch, grasshopper + # + # @param branch [Integer] the number of the branch to be edited + # @param leaf [Integer] the number of the leaf to be edited + # @param text [String] the new text for the leaf + # @param target [Integer] the branch number target for the leaf option + def leaf=(branch, leaf, text, target) + @branches[branch]['options'][leaf] = { text => target } + end end end