Module: Gardner

Defined in:
lib/sapling.rb

Overview

Gardner is the module for working with a dialogue tree file

Class Method Summary collapse

Class Method Details

.grow(file) ⇒ Hash

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

Parameters:

  • file (File)

    The dialogue tree file

Returns:

  • (Hash)

    The final, constructed data set



65
66
67
68
69
70
71
# File 'lib/sapling.rb', line 65

def self.grow(file)
    tree = YAML.load_file(file[0])
    tree = Gardner.prune_trunk(tree)
    branches = Gardner.prune_branches(tree)

    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.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sapling.rb', line 13

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sapling.rb', line 29

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sapling.rb', line 49

def self.prune_trunk(tree)
  trunk = tree.shift
  puts "Welcome to Sapling, a Dialogue Tree Utility.\n"
  40.times { print "-" }
  puts "\n#{trunk["trunk"]}"
  40.times { print "-" }
  puts "\n"

  return tree

end

.verify_tree(file) ⇒ Boolean

Verify that a file is a dialogue tree file.

Parameters:

  • file (File)

    The provided file

Returns:

  • (Boolean)

    True if the file is a tree; false otherwise



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sapling.rb', line 77

def self.verify_tree(file)
  results = []
  begin
    tree = YAML.load_file(file)
    results << tree[0].keys.include?("trunk")
    results << tree[1]["branch"].keys.include?("number")
    results << tree[1]["branch"].keys.include?("text")
    results << tree[1]["branch"].keys.include?("leaf")
  rescue
    puts "Sorry chummer, I don't think this is a tree."
    puts "Verify your YAML file is formatted properly."
    results << false
  end

  results.include?(false) ? false : true

end