Module: FakeCmd

Defined in:
lib/fakecmd.rb,
lib/fakecmd/version.rb

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

@@enabled =
false

Class Method Summary collapse

Class Method Details

.add(cmd, status = 0, output = "", &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/fakecmd.rb', line 48

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



30
31
32
# File 'lib/fakecmd.rb', line 30

def clear!
  commands.clear
end

.commandsObject



34
35
36
# File 'lib/fakecmd.rb', line 34

def commands
  @_commands ||= Set.new
end

.off!Object



20
21
22
23
24
25
26
27
28
# File 'lib/fakecmd.rb', line 20

def off!
  if @@enabled
    Kernel.class_eval do
      alias_method :`, :fakecmd_backquote
      remove_method :fakecmd_backquote
    end
    !@@enabled = false
  end
end

.on!Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fakecmd.rb', line 8

def on!
  unless @@enabled
    Kernel.class_eval do
      alias_method :fakecmd_backquote, :`
      def `(cmd)
        FakeCmd.process_command(cmd)
      end
    end
    @@enabled = true
  end
end

.process_command(cmd) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/fakecmd.rb', line 38

def process_command(cmd)
  commands.each do |h|
    if cmd[h[:regexp]]
      fakecmd_backquote("(exit #{h[:status]})")
      return h[:output]
    end
  end
  system ""
end