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

#add_requirements, #completion_list, #find_command, #path, #process_terms

Methods included from DSL::CommandSetDefinition

#command, #command_alias, #define_commands, #include_commands, #mode_command, #require_commands, #root_command, #sub_command, #subject_defaults

Constructor Details

#initialize(name = "") ⇒ CommandSet

Returns a new instance of CommandSet.



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/command-set/command-set.rb', line 155

def initialize(name="")
  @name = name
  @command_list = { nil => RootCommand.setup(self, nil) {} }
  @mode_commands = {}
  @included_sets = []
  @documentation = ""
  @prompt = nil
  @arguments = []
  @most_recent_args = {}
  @subject_defaults = proc {|s|}
  @context = []
  @root_blocks = []
end

Instance Attribute Details

#documentation(width, prefix = []) ⇒ Object Also known as: short_docs

Returns the value of attribute documentation.



179
180
181
# File 'lib/command-set/command-set.rb', line 179

def documentation
  @documentation
end

#most_recent_argsObject

Returns the value of attribute most_recent_args.



179
180
181
# File 'lib/command-set/command-set.rb', line 179

def most_recent_args
  @most_recent_args
end

#root_blocksObject (readonly)

Returns the value of attribute root_blocks.



180
181
182
# File 'lib/command-set/command-set.rb', line 180

def root_blocks
  @root_blocks
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.



186
187
188
189
190
# File 'lib/command-set/command-set.rb', line 186

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.



203
204
205
206
207
# File 'lib/command-set/command-set.rb', line 203

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_defaults(subject) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/command-set/command-set.rb', line 276

def add_defaults(subject)
  included_subject = @included_sets.inject(Subject.new) do |merger, (subset, options)|
    merger.merge(options[:context], subset.get_subject)
  end
  subject.absorb(included_subject)
  @subject_defaults.call(subject)
end

#apply_root_blocks(blocks) ⇒ Object



262
263
264
265
266
267
# File 'lib/command-set/command-set.rb', line 262

def apply_root_blocks(blocks)
  @root_blocks += blocks
  blocks.each do |block|
    @command_list[nil].instance_eval(&block)
  end
end

#build_command(home, name_or_class, name_or_nil, block) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/command-set/command-set.rb', line 316

def build_command(home, name_or_class, name_or_nil, block)
  if Class === name_or_class && Command > name_or_class
    if block.nil?
      command = name_or_class.dup
      name = command.name
    else
      name = name_or_nil.to_s
      command = name_or_class.setup(self, name, &block)
    end
  else
    if String === name_or_class or Symbol === name_or_class
      name = name_or_class.to_s
    else
      raise RuntimeError, "#{name_or_class} is neither a Command class nor a name!"
    end
    command = Command.setup(name, &block)
  end

  home[name] = command
end

#command_listObject



337
338
339
# File 'lib/command-set/command-set.rb', line 337

def command_list
  return @command_list.dup
end

#command_namesObject



345
346
347
# File 'lib/command-set/command-set.rb', line 345

def command_names
  return @command_list.keys.compact + @mode_commands.keys.compact
end

#consume_terms(terms, subject, arg_hash) ⇒ Object



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

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(path, visitor) ⇒ Object

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



219
220
221
222
223
224
225
226
227
# File 'lib/command-set/command-set.rb', line 219

def each_command(path, visitor)
  @command_list.each_pair do |term, command|
    command.each_command(path + [term], visitor)
  end
  return unless path.empty?
  @mode_commands.each_pair do |term, command|
    command.each_command([term], visitor)
  end
end

#get_rootObject



258
259
260
# File 'lib/command-set/command-set.rb', line 258

def get_root
  command = @command_list[nil]
end

#get_subjectObject



269
270
271
272
273
274
# File 'lib/command-set/command-set.rb', line 269

def get_subject
  subject = Subject.new
  add_requirements(subject)
  add_defaults(subject)
  return subject
end

#initialize_copy(original) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/command-set/command-set.rb', line 169

def initialize_copy(original)
  super
  base_list = original.instance_variable_get("@command_list")
  @command_list = {}
  @context = [] #original.context.dup
  base_list.each_pair do |name, cmd|
    @command_list[name] = cmd.dup
  end
end

#mode_commandsObject



341
342
343
# File 'lib/command-set/command-set.rb', line 341

def mode_commands
  return @mode_commands.dup
end

#promptObject



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/command-set/command-set.rb', line 284

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

#root_visit(terms, visitor) ⇒ Object



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

def root_visit(terms, visitor)
  next_hop = if(terms.empty?)
               visitor.set_out_of_terms(self)
               @command_list[nil]
             elsif(visitor.command_path.empty?)
               @mode_commands[terms.first] || @command_list[terms.first]
             else
               @command_list[terms.first]
             end

  return visitor.term_without_hop(terms, self) if(next_hop.nil?)

  visitor.leave_from(terms.shift, terms, self)

  return next_hop.visit(terms, visitor)
end

#set_prompt(match, replace) ⇒ Object



296
297
298
# File 'lib/command-set/command-set.rb', line 296

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

#visit(terms, visitor) ⇒ Object



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

def visit(terms, visitor)
  visitor.arrive_at(terms, self)

  root_visit(terms, visitor)
end