Class: StackLoop::App

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_loop/app.rb

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

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_fileObject

Returns the value of attribute collect_file.



29
30
31
# File 'lib/stack_loop/app.rb', line 29

def collect_file
  @collect_file
end

#commandObject

Returns the value of attribute command.



29
30
31
# File 'lib/stack_loop/app.rb', line 29

def command
  @command
end

#default_argsObject

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_fileObject

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_hashObject



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_listObject



111
112
113
# File 'lib/stack_loop/app.rb', line 111

def command_list
  @command_list ||= command.split(/\s/)
end

#command_namesObject



127
128
129
# File 'lib/stack_loop/app.rb', line 127

def command_names
  @command_names ||= command_hash.keys
end

#commandsObject



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_lineObject



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_argsetObject



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_argsetObject



78
79
80
81
82
# File 'lib/stack_loop/app.rb', line 78

def pop_argset
  read_stack
  @stack.pop
  write_stack
end

#promptObject



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_newObject



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_stackObject



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

#runObject



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
  validate_options!

  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_stackObject



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_loopObject



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_depthObject



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 validate_options!
  raise "Need a command!" if command.nil?
  raise "Need a stack file!" if stack_file.nil?
end

#write_stackObject



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