Class: Planter::Spade
- Inherits:
-
Object
- Object
- Planter::Spade
- Defined in:
- lib/sapling/planter.rb
Overview
Utilities for editing specific parts of a tree.
Instance Attribute Summary collapse
-
#file ⇒ Object
writeonly
The file we parse into a tree.
Instance Method Summary collapse
-
#dig(branch_no) ⇒ Object
Function for displaying a single branch in debug mode.
-
#get_response(branch) ⇒ Integer
Get a response for the displayed branch.
-
#initialize(file) ⇒ Spade
constructor
A new instance of Spade.
-
#parse_response(response, branch_no) ⇒ Integer
Parse the response from get_response.
-
#plant ⇒ Object
Establish and populate a new Plot (in-memory tree), then control the flow of editing the Plot.
Constructor Details
#initialize(file) ⇒ Spade
Returns a new instance of Spade
61 62 63 |
# File 'lib/sapling/planter.rb', line 61 def initialize(file) @file = file end |
Instance Attribute Details
#file=(value) ⇒ Object (writeonly)
The file we parse into a tree
59 60 61 |
# File 'lib/sapling/planter.rb', line 59 def file=(value) @file = value end |
Instance Method Details
#dig(branch_no) ⇒ Object
Function for displaying a single branch in debug mode. We also always display the trunk, since otherwise it's displayed a single time then gone forever (until next time).
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/sapling/planter.rb', line 87 def dig(branch_no) branch = @plot.branches[branch_no] Dialogue.display_trunk(@plot.trunk, true) Dialogue.display_branch(branch, branch_no, true) response = get_response(branch) to_branch = parse_response(response, branch_no) return to_branch end |
#get_response(branch) ⇒ Integer
Get a response for the displayed branch
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sapling/planter.rb', line 103 def get_response(branch) total_branches = @plot.branches.count - 1 = ["1-#{total_branches}","t","a","b","x","l","s","q"] = .join(",") print "\n[#{}]> " STDOUT.flush response = STDIN.gets.chomp.to_s.downcase until .include?(response) or response.to_i.between?(1,total_branches) print "[## Invalid response. " print "Valid options are #{}" print "\n[#{}]> " response = STDIN.gets.chomp.to_s.downcase end return response end |
#parse_response(response, branch_no) ⇒ Integer
Parse the response from get_response
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/sapling/planter.rb', line 127 def parse_response(response, branch_no) 10.times { print "*" } print "\n(Your choice: " if response.to_i >= 1 print "Change to branch #{response.to_i})\n\n" return response.to_i end case response.to_s.downcase when "t" print "Edit the trunk.)\n\n" @plot.edit_trunk return branch_no when "a" print "Add a new branch.)\n\n" return branch_no when "b" print "Edit the current branch.)\n\n" @plot.edit_branch(branch_no) return branch_no when "x" print "Delete the current branch.)\n\n" return branch_no when "l" print "Edit leaves of current branch.)\n\n" return branch_no when "s" print "Save changes.)\n\n" return branch_no when "q" print "Quit without saving.)\n\n" print "Unsaved changes will be lost. Still quit? [y/n]> " verify = STDIN.gets.chomp.to_s.downcase return 0 if verify == "y" return branch_no else print "Unknown option. Returning to current branch.)\n\n" return branch_no end end |
#plant ⇒ Object
Establish and populate a new Plot (in-memory tree), then control the flow of editing the Plot
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sapling/planter.rb', line 67 def plant @plot = Plot.new @plot.tree = @file @plot.trunk = @file.shift @plot.branches = Gardner.prune_branches(@file) next_branch = dig(1) until next_branch == 0 do next_branch = dig(next_branch) end puts "\n#{@plot.branches[0]["desc"]}" exit end |