Class: Command::CommandSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common, DSL::CommandSetDefinition
Defined in:
lib/command-set/command-set.rb

Overview

This class packs up a set of commands, for presentation to an interpreter. CommandSet objects are defined using methods from DSL::CommandSetDefinition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#completion_list, #find_command, #path, #process_terms

Methods included from DSL::CommandSetDefinition

#command, #define_commands, #include_commands, #require_commands, #root_command, #sub_command

Constructor Details

#initialize(parent = nil, name = "") ⇒ CommandSet

Returns a new instance of CommandSet.



149
150
151
152
153
154
155
156
157
158
# File 'lib/command-set/command-set.rb', line 149

def initialize(parent=nil, name="")
  @parent = parent
  @name = name
  @command_list = { nil => RootCommand.setup(self, nil) {} }
  @subject_template = nil
  @documentation = ""
  @prompt = nil
  @arguments = []
  @most_recent_args = {}
end

Instance Attribute Details

#documentation(width, parent = "") ⇒ Object Also known as: short_docs

Returns the value of attribute documentation.



160
161
162
# File 'lib/command-set/command-set.rb', line 160

def documentation
  @documentation
end

#most_recent_argsObject

Returns the value of attribute most_recent_args.



160
161
162
# File 'lib/command-set/command-set.rb', line 160

def most_recent_args
  @most_recent_args
end

#parentObject

Returns the value of attribute parent.



160
161
162
# File 'lib/command-set/command-set.rb', line 160

def parent
  @parent
end

Class Method Details

.define_commands(&block) ⇒ Object

The preferred way to use a CommandSet is to call CommandSet::define_commands with a block, and then call #command, #include_commands and #sub_command on it.



166
167
168
169
170
# File 'lib/command-set/command-set.rb', line 166

def define_commands(&block)
	set = self.new
	set.define_commands(&block)
	return set
end

.require_commands(mod, file = nil, cmd_path = []) ⇒ Object

In driver code, it’s often quickest to yank in commands from a file. To do that, create a code file with a module in it. The module needs a method of the form

def self.define_commands()

define_commands should return a CommandSet. Then, pass the require path and module name to require_commands, and it’ll take care of creating the command set. You can even call DSL::CommandSetDefinition#define_commands on the set that’s returned in order to add one-off commands or fold in other command sets.



183
184
185
186
187
# File 'lib/command-set/command-set.rb', line 183

def require_commands(mod, file=nil, cmd_path=[])
  set = self.new
  set.require_commands(mod, file, cmd_path)
  return set
end

Instance Method Details

#add_requirements(subject) ⇒ Object



250
251
252
253
254
255
# File 'lib/command-set/command-set.rb', line 250

def add_requirements(subject)
  @command_list.each_value do |command|
	command.add_requirements subject
  end
  return subject
end

#command_listObject



273
274
275
# File 'lib/command-set/command-set.rb', line 273

def command_list
  return @command_list.dup
end

#command_namesObject



277
278
279
# File 'lib/command-set/command-set.rb', line 277

def command_names
  return @command_list.keys.compact
end

#consume_terms(terms, subject, arg_hash) ⇒ Object



224
225
226
227
228
# File 'lib/command-set/command-set.rb', line 224

def consume_terms(terms, subject, arg_hash)
  @command_list[nil].consume_terms(terms, subject, arg_hash)
  @most_recent_args = arg_hash.dup
  return arg_hash
end

#each_command(&block) ⇒ Object

:section: Workhorse methods - not usually used by client code



199
200
201
202
203
# File 'lib/command-set/command-set.rb', line 199

def each_command(&block)
  @command_list.each_value do |command|
    command.each_command(&block)
  end
end

#get_rootObject



230
231
232
# File 'lib/command-set/command-set.rb', line 230

def get_root
  command = @command_list[nil]
end

#promptObject



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/command-set/command-set.rb', line 234

def prompt
  if @prompt.nil?
    if @name.empty?
      return [/$/, ""]
    else
      return [/$/, "#@name : "]
    end
  else
    return @prompt
  end
end

#set_prompt(match, replace) ⇒ Object



246
247
248
# File 'lib/command-set/command-set.rb', line 246

def set_prompt(match, replace)
  @prompt = [match, replace]
end

#visit(terms, visitor) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/command-set/command-set.rb', line 205

def visit(terms, visitor)
  if(terms.empty?)
    return visitor.set_out_of_terms(self)
  end

  next_hop = @command_list[terms.first]
  if(next_hop.nil?)
    return visitor.term_without_hop(terms, self)
  else
    terms.shift
  end

  visitor.leave_from(terms, self)

  visitor.arrive_at(terms, next_hop)

  return next_hop.visit(terms, visitor)
end