Class: Laborantin::Command

Inherits:
Object
  • Object
show all
Extended by:
Enumerable, Metaprog::Completeable
Defined in:
lib/laborantin/core/command.rb

Overview

The Command is a way to extend the labor script to help you modularize your work, and call the commands both via the command line, or in your scripts. This class provides parsing facilities. Namespacing (i.e., mapping between classes and command line is done in the Runner . Internal labor Commands are actually implemented this way.

Defined Under Namespace

Classes: Option

Constant Summary collapse

@@all =

An Array to store all the commands.

[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Metaprog::Completeable

#completion_block

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metaprog::Completeable

complete, completion_propositions_iterating_on

Constructor Details

#initialize(runner = nil) ⇒ Command

Initializes a new instance of command. Will first set the runner if provided, then will initialize default options and arguments.



194
195
196
197
198
# File 'lib/laborantin/core/command.rb', line 194

def initialize(runner=nil)
  @runner = runner
  initialize_opts
  initialize_args
end

Class Attribute Details

.blockObject

The block of execution for the instances of this commands. By default, this will raise an exception. TODO: transform the block into a default method.



118
119
120
# File 'lib/laborantin/core/command.rb', line 118

def block
  @block
end

.command_nameObject

By default, the name of the class, or an empty string if none (this can lead to big issues, so I’m still considering returning nil if a command has no name nor command_name.



123
124
125
# File 'lib/laborantin/core/command.rb', line 123

def command_name
  @command_name
end

.descriptionObject

The description string of a command, will be used on command line help.



107
108
109
# File 'lib/laborantin/core/command.rb', line 107

def description
  @description
end

.optionsObject

An Array contining the possible options of this command.



113
114
115
# File 'lib/laborantin/core/command.rb', line 113

def options
  @options
end

.plumberyObject

Wether or not this command is a plumbery one.



110
111
112
# File 'lib/laborantin/core/command.rb', line 110

def plumbery
  @plumbery
end

Instance Attribute Details

#argsObject (readonly)

An array containing the arguments of the command. Think of it like the trailing arguments of a command line. Arguments are ordered but not labeled, options are the opposite.



187
188
189
# File 'lib/laborantin/core/command.rb', line 187

def args
  @args
end

#optsObject (readonly)

A hash containing a merge of the defaults options of the class and the extra options provided to the run method. Think of it like the ‘–path=/some/path’ options of a command line. Options are labeled but not ordered, arguments are the opposite.



182
183
184
# File 'lib/laborantin/core/command.rb', line 182

def opts
  @opts
end

#runnerObject

The Runner object that will executes the command.



190
191
192
# File 'lib/laborantin/core/command.rb', line 190

def runner
  @runner
end

Class Method Details

.describe(str = nil) ⇒ Object

Set the description string.



142
143
144
# File 'lib/laborantin/core/command.rb', line 142

def describe(str=nil)
  self.description = str
end

.eachObject

Once we’ve extended Enumerable, it’s easy to find new commands.



98
99
100
# File 'lib/laborantin/core/command.rb', line 98

def self.each
  @@all.each{|e| yield e}
end

.execute(&blk) ⇒ Object

Set the execution block (executed in the scope of the instance). XXX this might well be changed into requiring ppl to define an “execute” method.



163
164
165
# File 'lib/laborantin/core/command.rb', line 163

def execute(&blk)
  self.block = blk
end

.inherited(klass) ⇒ Object

When a new Command class is created, sets up default value, and store the new class in the known commands.



134
135
136
137
138
139
# File 'lib/laborantin/core/command.rb', line 134

def inherited(klass)
  klass.description = ''
  klass.options = []
  klass.block = lambda { raise RuntimeError.new("no execution block for #{klass}") }
  @@all << klass
end

.option(name, &blk) ⇒ Object

Assign a new option to this command class, which name is name (preferably, give Symbol or String). The blk is evaluated in the scope of a new Option instance. Giving a name is required, because it is also the key of the command.opts hash.



171
172
173
174
175
# File 'lib/laborantin/core/command.rb', line 171

def option(name, &blk)
  arg = Option.new(name) 
  arg.instance_eval &blk
  self.options << arg
end

.plumbery!(val = true) ⇒ Object

Sets the plumbery flag.



147
148
149
# File 'lib/laborantin/core/command.rb', line 147

def plumbery!(val=true)
  self.plumbery = val
end

.plumbery?Boolean

Returns true if the plumbery flag is true.

Returns:

  • (Boolean)


152
153
154
# File 'lib/laborantin/core/command.rb', line 152

def plumbery?
  self.plumbery && true
end

.porcelain?Boolean

Opposite of plumbery?

Returns:

  • (Boolean)


157
158
159
# File 'lib/laborantin/core/command.rb', line 157

def porcelain?
  not self.plumbery?
end

Instance Method Details

#puts(line) ⇒ Object

Forward a line to the runner if it respond to a puts method too. Fallback on Kernel.puts if there is no runner or the runner does not respond to puts.



211
212
213
214
215
216
217
# File 'lib/laborantin/core/command.rb', line 211

def puts(line)
  if runner and runner.respond_to?(:puts)
    runner.puts(line)
  else
    Kernel.puts(line)
  end
end

#run(args = [], extra_opts = {}) ⇒ Object

Runs the command by merging the options with extra_opts and evaluating the block stored in the class.



202
203
204
205
206
# File 'lib/laborantin/core/command.rb', line 202

def run(args=[], extra_opts={})
  @opts.merge!(extra_opts)
  @args = args
  self.instance_eval &self.class.block
end