Class: Resque::Pool::Dynamic::Shell

Inherits:
Ripl::Shell
  • Object
show all
Defined in:
lib/resque/pool/dynamic/shell.rb

Overview

Ripl shell subclassed for nicer CLI experience

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(instance, opts = {}) ⇒ Object

Run the Ripl shell in context of specified instance

Parameters:

  • instance (Object)

    Instance to get bindings from

  • opts (Hash) (defaults to: {})

    Options passed to Ripl::Shell



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/resque/pool/dynamic/shell.rb', line 80

def self.run(instance, opts={})
  options = {
    :readline => true, 
    :riplrc => ENV['RIPL_RC'] || '~/.riplrc',
    :completion => {},
    :binding => instance.instance_eval { binding }
  }
  options.update(opts)
  Ripl::Runner::load_rc(options[:riplrc]) if options[:riplrc]!=''
  create(options).loop
end

Instance Method Details

#eval_input(input) ⇒ Object

Handle help as special command

Parameters:

  • input (String)

    Input string



68
69
70
71
72
73
74
75
# File 'lib/resque/pool/dynamic/shell.rb', line 68

def eval_input(input)
  splut = input.split(/\s+/,2)
  if splut.first == 'help'
    help(splut[1])
  else
    super
  end
end

#help(subject = nil) ⇒ Object

Show help

Parameters:

  • subject (String) (defaults to: nil)

    Topic to display



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/resque/pool/dynamic/shell.rb', line 48

def help(subject=nil)
  @help ||= File.open(
    Pathname.new(__FILE__).dirname.join('help.json')
    ) { |f| JSON::load(f.read) }
  if subject
    if @help.has_key?(subject)
      puts @help[subject]['full']
      return
    end
    puts "I don't know anything about #{subject}."
  end
  puts "Known commands:"
  @help.keys.sort.each do |cmd|
    puts "  #{cmd} - #{@help[cmd]['summary']}"
  end
  puts "For details, type: help command"
end

When printing error, cut backtrace at (ripl)



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/resque/pool/dynamic/shell.rb', line 28

def print_eval_error(e)
  i = e.backtrace.index { |ln| ln =~ /\(ripl\):\d+:in `run'/ }
  if not i
    bt = e.backtrace
  elsif i > 0
    bt = e.backtrace[0..i-1]
  else
    bt = []
  end
  bt = bt.map { |ln| "  " << ln }.join("\n")
  puts "ERROR: #{e.to_s}\n#{bt}"
end

Don’t print nil return values



42
43
44
# File 'lib/resque/pool/dynamic/shell.rb', line 42

def print_result(result)
  super if @error_raised or !result.nil?
end