Module: FakeCmd
- Defined in:
- lib/fakecmd.rb,
lib/fakecmd/version.rb
Defined Under Namespace
Modules: VERSION
Class Method Summary collapse
- .add(cmd, status = 0, output = "", &block) ⇒ Object
- .clear! ⇒ Object
- .commands ⇒ Object
- .off! ⇒ Object
- .on! ⇒ Object
- .on? ⇒ Boolean
- .process_command(cmd) ⇒ Object
Class Method Details
.add(cmd, status = 0, output = "", &block) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/fakecmd.rb', line 7 def add(cmd, status = 0, output = "", &block) cmd = cmd.to_s if cmd.is_a?(Symbol) commands << { :regexp => Regexp.new(cmd), :output => block ? block.call : output, :status => status } end |
.clear! ⇒ Object
16 17 18 |
# File 'lib/fakecmd.rb', line 16 def clear! commands.clear end |
.commands ⇒ Object
20 21 22 |
# File 'lib/fakecmd.rb', line 20 def commands @_commands ||= Set.new end |
.off! ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/fakecmd.rb', line 28 def off! if on? Kernel.class_eval do alias_method :`, :fakecmd_backquote remove_method :fakecmd_backquote end true end end |
.on! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fakecmd.rb', line 38 def on! unless on? Kernel.class_eval do alias_method :fakecmd_backquote, :` def `(cmd) FakeCmd.process_command(cmd) end end true end end |
.on? ⇒ Boolean
24 25 26 |
# File 'lib/fakecmd.rb', line 24 def on? Kernel.private_methods.map(&:to_sym).include?(:fakecmd_backquote) end |
.process_command(cmd) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/fakecmd.rb', line 50 def process_command(cmd) commands.each do |h| if cmd[h[:regexp]] fakecmd_backquote("(exit #{h[:status]})") return h[:output] end end system "" end |