Class: RProgram::Program
- Inherits:
-
Object
- Object
- RProgram::Program
- Defined in:
- lib/rprogram/program.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Name of the program.
-
#path ⇒ Object
readonly
Path to the program.
Class Method Summary collapse
-
.alias_program(*aliases) ⇒ Object
Sets the program aliases for the class.
-
.find(*arguments) {|prog| ... } ⇒ Program
Finds and creates the program using it's
program_names
. -
.find_with_path(path, *arguments) {|prog| ... } ⇒ Program?
Creates a new program object.
-
.find_with_paths(paths, *arguments) {|prog| ... } ⇒ Program?
Creates a new program object with the specified paths, if a path within paths is a valid file.
-
.name_program(name) ⇒ Object
Sets the program name for the class.
-
.path ⇒ String?
The default path of the program.
-
.path=(new_path) ⇒ String?
Sets the default path to the program.
-
.program_aliases ⇒ Array
The program's aliases.
-
.program_name ⇒ String
The name of the program.
-
.program_names ⇒ Array
Combines program_name with program_aliases.
Instance Method Summary collapse
-
#initialize(path) {|prog| ... } ⇒ Program
constructor
Creates a new Program object.
-
#program_aliases ⇒ Array
The program aliases of the class.
-
#program_name ⇒ String
The program name of the class.
-
#program_names ⇒ Array
The program names of the class.
-
#run(*arguments) ⇒ true, false
Runs the program.
-
#run_task(task, options = {}) ⇒ true, false
Runs the program with the arguments from the given task.
-
#sudo(*arguments) ⇒ Boolean
Runs the program under sudo.
-
#sudo_task(task, options = {}) {|sudo| ... } ⇒ true, false
Runs the program under
sudo
with the arguments from the given task. -
#to_s ⇒ String
Converts the program to a String.
Constructor Details
#initialize(path) {|prog| ... } ⇒ Program
Creates a new Program object.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rprogram/program.rb', line 35 def initialize(path,&block) path = File.(path) unless File.file?(path) raise(ProgramNotFound,"program #{path.dump} does not exist",caller) end @path = path @name = File.basename(path) block.call(self) if block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Name of the program
14 15 16 |
# File 'lib/rprogram/program.rb', line 14 def name @name end |
#path ⇒ Object (readonly)
Path to the program
11 12 13 |
# File 'lib/rprogram/program.rb', line 11 def path @path end |
Class Method Details
.alias_program(*aliases) ⇒ Object
Sets the program aliases for the class.
96 97 98 |
# File 'lib/rprogram/program.rb', line 96 def self.alias_program(*aliases) @program_aliases = aliases.map { |name| name.to_s } end |
.find(*arguments) {|prog| ... } ⇒ Program
Finds and creates the program using it's program_names
.
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rprogram/program.rb', line 227 def self.find(*arguments,&block) path = self.path path ||= System.find_program_by_names(*self.program_names) unless path names = self.program_names.map { |name| name.dump }.join(', ') raise(ProgramNotFound,"programs #{names} were not found",caller) end return self.new(path,*arguments,&block) end |
.find_with_path(path, *arguments) {|prog| ... } ⇒ Program?
Creates a new program object.
157 158 159 |
# File 'lib/rprogram/program.rb', line 157 def self.find_with_path(path,*arguments,&block) self.new(path,*arguments,&block) if File.file?(path) end |
.find_with_paths(paths, *arguments) {|prog| ... } ⇒ Program?
Creates a new program object with the specified paths, if a path within paths is a valid file. Any given arguments or a given block will be used in creating the new program.
191 192 193 194 195 196 197 |
# File 'lib/rprogram/program.rb', line 191 def self.find_with_paths(paths,*arguments,&block) paths.each do |path| if File.file?(path) return self.new(path,*arguments,&block) end end end |
.name_program(name) ⇒ Object
Sets the program name for the class.
83 84 85 |
# File 'lib/rprogram/program.rb', line 83 def self.name_program(name) @program_name = name.to_s end |
.path ⇒ String?
The default path of the program.
108 109 110 |
# File 'lib/rprogram/program.rb', line 108 def self.path @program_path end |
.path=(new_path) ⇒ String?
Sets the default path to the program.
123 124 125 126 127 |
# File 'lib/rprogram/program.rb', line 123 def self.path=(new_path) @program_path = if new_path File.(new_path) end end |
.program_aliases ⇒ Array
Returns The program's aliases.
60 61 62 |
# File 'lib/rprogram/program.rb', line 60 def self.program_aliases @program_aliases ||= [] end |
.program_name ⇒ String
Returns The name of the program.
52 53 54 |
# File 'lib/rprogram/program.rb', line 52 def self.program_name @program_name ||= nil end |
.program_names ⇒ Array
Combines program_name with program_aliases.
70 71 72 |
# File 'lib/rprogram/program.rb', line 70 def self.program_names ([program_name] + program_aliases).compact end |
Instance Method Details
#program_aliases ⇒ Array
Returns The program aliases of the class.
252 253 254 |
# File 'lib/rprogram/program.rb', line 252 def program_aliases self.class.program_aliases end |
#program_name ⇒ String
Returns The program name of the class.
244 245 246 |
# File 'lib/rprogram/program.rb', line 244 def program_name self.class.program_name end |
#program_names ⇒ Array
Returns The program names of the class.
260 261 262 |
# File 'lib/rprogram/program.rb', line 260 def program_names self.class.program_names end |
#run(*arguments) ⇒ true, false #run(*arguments, options) ⇒ true, false
Runs the program.
301 302 303 |
# File 'lib/rprogram/program.rb', line 301 def run(*arguments) System.run(@path,*arguments) end |
#run_task(task, options = {}) ⇒ true, false
Runs the program with the arguments from the given task.
371 372 373 374 375 376 |
# File 'lib/rprogram/program.rb', line 371 def run_task(task,={}) arguments = task.arguments arguments << unless .empty? return run(*arguments) end |
#sudo(*arguments) ⇒ Boolean #sudo(*arguments, options) ⇒ Boolean
Runs the program under sudo.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/rprogram/program.rb', line 341 def sudo(*arguments) = if arguments.last.kind_of?(Hash) arguments.pop else {} end task = SudoTask.new(.delete(:sudo) || {}) task.command = [@path] + arguments arguments = task.arguments arguments << unless .empty? return System.sudo(*arguments) end |
#sudo_task(task, options = {}) {|sudo| ... } ⇒ true, false
Runs the program under sudo
with the arguments from the given task.
400 401 402 403 404 405 |
# File 'lib/rprogram/program.rb', line 400 def sudo_task(task,={},&block) arguments = task.arguments arguments << unless .empty? return sudo(*arguments,&block) end |
#to_s ⇒ String
Converts the program to a String.
417 418 419 |
# File 'lib/rprogram/program.rb', line 417 def to_s @path.to_s end |