Class: SystemCommand
- Inherits:
-
Object
- Object
- SystemCommand
- Defined in:
- lib/system_command.rb,
lib/system_command/version.rb
Overview
Allows you to run external process with syntax a little bit more polished than the ruby built-ins.
Turn an external process into a class:
git = SystemCommand.new('git')
git.run('status')
Constant Summary collapse
- Error =
Class.new(StandardError)
- VERSION =
'2.0.2'
Class Attribute Summary collapse
-
.verbose ⇒ Object
writeonly
Sets the attribute verbose.
Class Method Summary collapse
- .each_line(chomp: false) ⇒ Object
- .gets ⇒ Object
- .lines(chomp: false) ⇒ Object
- .run ⇒ Object
- .run? ⇒ Boolean
- .verbose? ⇒ Boolean
Instance Method Summary collapse
- #each_line ⇒ Object
- #gets ⇒ Object
-
#initialize(path, *options) ⇒ SystemCommand
constructor
A new instance of SystemCommand.
- #lines ⇒ Object
- #run ⇒ Object
- #run? ⇒ Boolean
Constructor Details
#initialize(path, *options) ⇒ SystemCommand
Returns a new instance of SystemCommand.
82 83 84 85 |
# File 'lib/system_command.rb', line 82 def initialize(path, *) @path = path @options = end |
Class Attribute Details
.verbose=(value) ⇒ Object (writeonly)
Sets the attribute verbose
21 22 23 |
# File 'lib/system_command.rb', line 21 def verbose=(value) @verbose = value end |
Class Method Details
.each_line(chomp: false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/system_command.rb', line 52 def each_line(*, chomp: false, **, &) argv0, *argv = coerce_argv(*) raise LocalJumpError, 'no block given' unless block_given? puts_command argv0, argv IO.popen([argv0, *argv], **) do |io| io.each_line(chomp:, &) end raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success? nil end |
.gets ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/system_command.rb', line 40 def gets(*, **) argv0, *argv = coerce_argv(*) puts_command argv0, argv result = IO.popen([argv0, *argv], **, &:read) raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success? result end |
.lines(chomp: false) ⇒ Object
50 |
# File 'lib/system_command.rb', line 50 def lines(*, chomp: false, **) = gets(*, **).lines(chomp:) |
.run ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/system_command.rb', line 30 def run(*, **) argv0, *argv = coerce_argv(*) puts_command argv0, argv system([argv0, argv0], *argv, **) raise SystemCommand::Error, "#{argv0} failed (#{Process.last_status})" unless Process.last_status.success? nil end |
.run? ⇒ Boolean
23 24 25 26 27 28 |
# File 'lib/system_command.rb', line 23 def run?(*, **) argv0, *argv = coerce_argv(*) puts_command argv0, argv system([argv0, argv0], *argv, **) end |
.verbose? ⇒ Boolean
17 18 19 |
# File 'lib/system_command.rb', line 17 def verbose? @verbose ||= false end |
Instance Method Details
#each_line ⇒ Object
91 |
# File 'lib/system_command.rb', line 91 def each_line(...) = SystemCommand.each_line(@path, *@options, ...) |
#gets ⇒ Object
89 |
# File 'lib/system_command.rb', line 89 def gets(...) = SystemCommand.gets(@path, *@options, ...) |
#lines ⇒ Object
90 |
# File 'lib/system_command.rb', line 90 def lines(...) = SystemCommand.lines(@path, *@options, ...) |
#run ⇒ Object
88 |
# File 'lib/system_command.rb', line 88 def run(...) = SystemCommand.run(@path, *@options, ...) |
#run? ⇒ Boolean
87 |
# File 'lib/system_command.rb', line 87 def run?(...) = SystemCommand.run?(@path, *@options, ...) |