class Planter::Spade

Utilities for editing specific parts of a tree.

Attributes

file[W]

The file we parse into a tree

Public Class Methods

new(file) click to toggle source
# File lib/sapling/planter.rb, line 61
def initialize(file)
  @file = file
end

Public Instance Methods

dig(branch_no) click to toggle source

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).

@param branch_no [Integer] The number of the branch to be displayed.

# 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) click to toggle source

Get a response for the displayed branch

@param branch [Hash] A branch data set @return [Integer] the next branch

# File lib/sapling/planter.rb, line 103
def get_response(branch)
  total_branches = @plot.branches.count - 1
  valid_options = ["1-#{total_branches}","t","a","b","x","l","s","q"]
  print_options = valid_options.join(",")

  print "\n[#{print_options}]> "
  STDOUT.flush
  response = STDIN.gets.chomp.to_s.downcase

  until valid_options.include?(response) or response.to_i.between?(1,total_branches)
    print "[## Invalid response. "
    print "Valid options are #{print_options}"
    print "\n[#{print_options}]> "
    response = STDIN.gets.chomp.to_s.downcase
  end

  return response
end
parse_response(response, branch_no) click to toggle source

Parse the response from get_response

@param response [String] The option selected @param branch_no [Integer] The currently-displayed branch @return [Integer] the branch to display

# 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() click to toggle source

Establish and populate a new Plot (in-memory tree), then control the flow of editing the Plot

# 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