Module: Gardner

Defined in:
lib/sapling/gardner.rb

Overview

Gardner is the module for working with a dialogue tree file

Class Method Summary collapse

Class Method Details

.prune_branches(tree) ⇒ Array

Parse the tree array into an array of numbered branches, and ordered leaves.

Parameters:

  • tree (Array)

    The dialogue tree

Returns:

  • (Array)

    An array of numbered branches, with numbered leaves



10
11
12
13
14
15
16
17
18
19
# File 'lib/sapling/gardner.rb', line 10

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"]) }
  end

  return branches
end

.prune_leaves(leaves) ⇒ Hash

Parse the leaves of a branch into a numbered hash of options.

Parameters:

  • leaves (Array)

    The option of leaf hashes

Returns:

  • (Hash)

    A numbered hash of options



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sapling/gardner.rb', line 25

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
  end

  return options
end

.prune_trunk(tree) ⇒ Array

Parse the trunk of the tree.

Parameters:

  • tree (Hash)

    The entire tree

Returns:

  • (Array)

    The trunk, and the remainder of the tree



43
44
45
46
47
# File 'lib/sapling/gardner.rb', line 43

def self.prune_trunk(tree)
  trunk = tree.shift

  return [trunk,tree]
end