Class: Gardner::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/sapling/gardner.rb

Overview

The Plot class handles a specific tree file. It provides functionality for parsing trunks and branches, and provides these as class attributes.

Direct Known Subclasses

Digiplot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Plot

Initialize a new Plot from a tree file

Parameters:

  • tree (File)

    The dialogue tree file



14
15
16
17
18
# File 'lib/sapling/gardner.rb', line 14

def initialize(file)
  @tree = file
  prune_trunk
  prune_branches
end

Instance Attribute Details

#branchesObject (readonly)

The trunk and branches instance variables



9
10
11
# File 'lib/sapling/gardner.rb', line 9

def branches
  @branches
end

#treeObject (readonly)

The trunk and branches instance variables



9
10
11
# File 'lib/sapling/gardner.rb', line 9

def tree
  @tree
end

#trunkObject (readonly)

The trunk and branches instance variables



9
10
11
# File 'lib/sapling/gardner.rb', line 9

def trunk
  @trunk
end

Instance Method Details

#prune_branchesArray

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

Parameters:

  • tree (File)

    The dialogue tree

Returns:

  • (Array)

    An array of numbered branches, with numbered leaves



25
26
27
28
29
30
31
32
33
# 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

#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



39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#prune_trunkArray

Parse the trunk of the tree.

Returns:

  • (Array)

    The trunk, and the remainder of the tree



56
57
58
# File 'lib/sapling/gardner.rb', line 56

def prune_trunk
  @trunk = @tree.shift
end