Class: Gardner::Plot
- Inherits:
-
Object
- Object
- Gardner::Plot
- 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
Instance Attribute Summary collapse
-
#branches ⇒ Object
readonly
The trunk and branches instance variables.
-
#tree ⇒ Object
readonly
The trunk and branches instance variables.
-
#trunk ⇒ Object
readonly
The trunk and branches instance variables.
Instance Method Summary collapse
-
#initialize(file) ⇒ Plot
constructor
Initialize a new Plot from a tree file.
-
#prune_branches ⇒ Array
Parse the tree array into an array of numbered branches, and ordered leaves.
-
#prune_leaves(leaves) ⇒ Hash
Parse the leaves of a branch into a numbered hash of options.
-
#prune_trunk ⇒ Array
Parse the trunk of the tree.
Constructor Details
#initialize(file) ⇒ Plot
Initialize a new Plot from a 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
#branches ⇒ Object (readonly)
The trunk and branches instance variables
9 10 11 |
# File 'lib/sapling/gardner.rb', line 9 def branches @branches end |
#tree ⇒ Object (readonly)
The trunk and branches instance variables
9 10 11 |
# File 'lib/sapling/gardner.rb', line 9 def tree @tree end |
#trunk ⇒ Object (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_branches ⇒ Array
Parse the tree array into an array of numbered branches, and ordered 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.
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 = {} return if leaves.nil? leaves.each do |l| [x] = { l['text'] => l['branch'] } x += 1 end end |
#prune_trunk ⇒ Array
Parse the trunk of the tree.
56 57 58 |
# File 'lib/sapling/gardner.rb', line 56 def prune_trunk @trunk = @tree.shift end |