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

.grow(tree) ⇒ Hash

The main method for Sapling. From here, the tree is grown.

Parameters:

  • tree (File)

    The dialogue tree file

Returns:

  • (Hash)

    The final, constructed data set



61
62
63
64
65
66
# File 'lib/sapling/gardner.rb', line 61

def self.grow(tree)
    trunk = Gardner.prune_trunk(tree)
    branches = Gardner.prune_branches(trunk)

    return branches
end

.prune_branches(tree) ⇒ Array

Parse the branch

Parameters:

  • tree (Array)

    The dialogue tree

Returns:

  • (Array)

    The array of options on the branch.



10
11
12
13
14
15
16
17
18
19
20
# 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 options

Parameters:

  • leaves (Array)

    The option of leaf hashes

Returns:

  • (Hash)

    A has of options



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

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) ⇒ Hash

Parse the trunk The trunk is like the introduction to the tree.

Parameters:

  • tree (Hash)

    The entire tree

Returns:

  • (Hash)

    The tree without the trunk



46
47
48
49
50
51
52
53
54
55
# File 'lib/sapling/gardner.rb', line 46

def self.prune_trunk(tree)
  trunk = tree.shift
  40.times { print "-" }
  puts "\n#{trunk["trunk"]}"
  40.times { print "-" }
  puts "\n"

  return tree

end