Class: Spade::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/spade/shell.rb

Constant Summary collapse

METHODS =
[:to_s, :exit, :quit, :help, :evalrb, :inspectjs]

Instance Method Summary collapse

Constructor Details

#initializeShell

Returns a new instance of Shell.



16
17
18
19
# File 'lib/spade/shell.rb', line 16

def initialize
  @history_path = File.expand_path("~/.spadehistory")
  load_history
end

Instance Method Details

#evalrb(str) ⇒ Object



53
54
55
# File 'lib/spade/shell.rb', line 53

def evalrb(str)
  Kernel.eval str, binding
end

#exit(status = 0) ⇒ Object Also known as: quit



31
32
33
34
35
# File 'lib/spade/shell.rb', line 31

def exit(status=0)
  save_history
ensure
  @ctx.reactor.exit(status)
end

#help(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spade/shell.rb', line 39

def help(*args)
  <<-HELP
  print(msg)
  print msg to STDOUT

  exit(status = 0)
  exit the shell
  also: quit()

  evalrb(source)
  evaluate some ruby source
  HELP
end

#inject(ctx) ⇒ Object



21
22
23
24
25
# File 'lib/spade/shell.rb', line 21

def inject(ctx)
  @ctx = ctx
  METHODS.each{|meth| @ctx[meth.to_s] = method(meth) }
  true
end

#inspectjs(obj) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/spade/shell.rb', line 57

def inspectjs(obj)
  # check for exact class match so we don't match things like arrays
  spacer = (obj.class == V8::Object) ? 2 : nil
  # the self[] uses the JS version of JSON
  json = @ctx['JSON'].stringify(obj, nil, spacer)
  # Some things can't be converted into json
  json || obj.inspect
end

#to_sObject



27
28
29
# File 'lib/spade/shell.rb', line 27

def to_s
  "[object Shell]"
end