Class: StackLoop::App
- Inherits:
-
Object
- Object
- StackLoop::App
- Defined in:
- lib/stack_loop/app.rb
Defined Under Namespace
Classes: Command
Instance Attribute Summary collapse
-
#collect_file ⇒ Object
Returns the value of attribute collect_file.
-
#command ⇒ Object
Returns the value of attribute command.
-
#default_args ⇒ Object
Returns the value of attribute default_args.
-
#stack_file ⇒ Object
Returns the value of attribute stack_file.
Instance Method Summary collapse
- #command_hash ⇒ Object
- #command_list ⇒ Object
- #command_names ⇒ Object
- #commands ⇒ Object
- #current_command_line ⇒ Object
- #get_argset ⇒ Object
-
#initialize ⇒ App
constructor
A new instance of App.
- #pop_argset ⇒ Object
- #prompt ⇒ Object
- #push_argset(args) ⇒ Object
- #push_new ⇒ Object
- #read_stack ⇒ Object
- #run ⇒ Object
- #run_stack ⇒ Object
- #run_stack_loop ⇒ Object
- #stack_depth ⇒ Object
- #validate_options! ⇒ Object
- #write_stack ⇒ Object
Constructor Details
#initialize ⇒ App
Returns a new instance of App.
31 32 33 |
# File 'lib/stack_loop/app.rb', line 31 def initialize @default_args = "" end |
Instance Attribute Details
#collect_file ⇒ Object
Returns the value of attribute collect_file.
29 30 31 |
# File 'lib/stack_loop/app.rb', line 29 def collect_file @collect_file end |
#command ⇒ Object
Returns the value of attribute command.
29 30 31 |
# File 'lib/stack_loop/app.rb', line 29 def command @command end |
#default_args ⇒ Object
Returns the value of attribute default_args.
29 30 31 |
# File 'lib/stack_loop/app.rb', line 29 def default_args @default_args end |
#stack_file ⇒ Object
Returns the value of attribute stack_file.
29 30 31 |
# File 'lib/stack_loop/app.rb', line 29 def stack_file @stack_file end |
Instance Method Details
#command_hash ⇒ Object
131 132 133 |
# File 'lib/stack_loop/app.rb', line 131 def command_hash @command_hash ||= Hash[ commands.map{|cmd| [cmd.name, cmd] } ] end |
#command_list ⇒ Object
111 112 113 |
# File 'lib/stack_loop/app.rb', line 111 def command_list @command_list ||= command.split(/\s/) end |
#command_names ⇒ Object
127 128 129 |
# File 'lib/stack_loop/app.rb', line 127 def command_names @command_names ||= command_hash.keys end |
#commands ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/stack_loop/app.rb', line 19 def commands [ Command.new("run"){ run_stack_loop }, Command.new("collect"){ push_new; run_stack_loop }, Command.new("pop"){ pop_argset }, Command.new("quit"){ raise "Quitting!" }, Command.new("help"){ puts "Don't know that one - try: #{command_names.join(", ")}" } ] end |
#current_command_line ⇒ Object
115 116 117 118 119 |
# File 'lib/stack_loop/app.rb', line 115 def current_command_line args = get_argset command_list + get_argset end |
#get_argset ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/stack_loop/app.rb', line 63 def get_argset read_stack if @stack.empty? [default_args] else @stack.last end end |
#pop_argset ⇒ Object
78 79 80 81 82 |
# File 'lib/stack_loop/app.rb', line 78 def pop_argset read_stack @stack.pop write_stack end |
#prompt ⇒ Object
121 122 123 124 125 |
# File 'lib/stack_loop/app.rb', line 121 def prompt puts puts "Next: " + current_command_line.join(" ") print "#{stack_depth} > " end |
#push_argset(args) ⇒ Object
72 73 74 75 76 |
# File 'lib/stack_loop/app.rb', line 72 def push_argset(args) read_stack @stack.push(args) write_stack end |
#push_new ⇒ Object
40 41 42 43 44 |
# File 'lib/stack_loop/app.rb', line 40 def push_new unless collect_file.nil? push_argset File::read(collect_file).split("\n") end end |
#read_stack ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/stack_loop/app.rb', line 46 def read_stack stack_string = File.read(stack_file) @stack = stack_string.split("\n\n").map do |item| item.split("\n") end.reject do |item| item.empty? end rescue Errno::ENOENT @stack = [] end |
#run ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/stack_loop/app.rb', line 89 def run puts "Starting with: " + current_command_line.join(" ") puts run_stack_loop abbreviations = Abbrev.abbrev(command_names) loop do prompt command_name = $stdin.gets.chomp if command_name.empty? command_name = "run" end command_name = abbreviations[command_name] command = command_hash.fetch(command_name, command_hash["help"]) command.run end end |
#run_stack ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/stack_loop/app.rb', line 143 def run_stack success = system(*current_command_line) if success.nil? raise "#{[command, *args].inspect} couldn't be run" end pop_argset if success return false if @stack.empty? return success end |
#run_stack_loop ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/stack_loop/app.rb', line 135 def run_stack_loop while run_stack puts puts "Success - running again..." puts end end |
#stack_depth ⇒ Object
84 85 86 87 |
# File 'lib/stack_loop/app.rb', line 84 def stack_depth read_stack @stack.length end |
#validate_options! ⇒ Object
35 36 37 38 |
# File 'lib/stack_loop/app.rb', line 35 def raise "Need a command!" if command.nil? raise "Need a stack file!" if stack_file.nil? end |
#write_stack ⇒ Object
57 58 59 60 61 |
# File 'lib/stack_loop/app.rb', line 57 def write_stack File::write(stack_file, @stack.map do |argset| argset.join("\n") end.join("\n\n") + "\n") end |