Class: Walnut::Shell

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

Overview

A simple shell.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Shell

Constructor.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/walnut/shell.rb', line 11

def initialize(&block)
  # Set the default config to the global.
  @config = Walnut.config.dup
  
  # Start with the global hooks.
  events = Walnut.ev.dup
  define_singleton_method(:ev) { events }

  instance_exec(@config, &block) if block_given?
  
end

Instance Method Details

#[](key) ⇒ Object

A shortcut to the configuration



37
38
39
# File 'lib/walnut/shell.rb', line 37

def [](key)
  @config.send(key.to_s)
end

#[]=(key, value) ⇒ Object

A shortcut to assign configuration values.



48
49
50
# File 'lib/walnut/shell.rb', line 48

def []=(key,value)
  @config.send(key.to_s << "=", value)
end

#configure {|@config| ... } ⇒ Object

Configure the shell.

Yields:



25
26
27
28
29
# File 'lib/walnut/shell.rb', line 25

def configure

  yield @config if block_given?

end

#crack(input = @config[:input].interface, output = @config[:output].interface) ⇒ Walnut::Shell

Begin a session, crack being a cutesy way to allude to the name of the project.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/walnut/shell.rb', line 59

def crack(input=@config[:input].interface, output=@config[:output].interface)
  
  if input  then @config[:input].interface  = input  end
  if output then @config[:output].interface = output end

  input  ||= Walnut[:input].interface
  output ||= Walnut[:output].interface
  ev.nut_throw :cracking
  loop do
    ev.nut_throw :before_read
    text = input.gets
    
    if input.eof?
      ev.nut_throw :eof, self

      # This has to be unset in a hook to recover from the EOF
      break if input.eof?
    end
    
    ev.nut_throw :after_read, text
    
    ev.nut_throw :before_parse, text 
    ast = nil #Parse -- ast == Abstract Syntax Tree. This is basically dependant on how the parser handles it.
              #      -- There is no definite interface for this because of the vast differences in parsers/lexers.
    ev.nut_throw :after_parse, ast
  end

  # Cute after session hook name.
  Walnut.ev.nut_throw :scrambled
end