Sha256: 12a54eec3bb5af18a54f08241b8e112fd3ad9f7c093b74a37e19fa25c902a7f4

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

module StubShell
  class Shell
    attr_reader :parent_context

    def initialize parent_context = nil, &block
      @parent_context  = parent_context
      @commands        = [ ]

      instance_eval &block
    end

    def command name, &block
      @commands << Command.new(name, self, &block)
    end

    # Resolves the requested command and returns the command definition and
    # the context from which to execute the next command.
    def execute command_string
      resolved_command = resolve(command_string)
      [resolved_command, resolved_command.current_context || self]
    end

    protected

    # Look in current context and recursively through any available parent contexts to
    # find definition of command. An Exception is raised if no implementation of command
    # is found.
    def resolve command_string
      if detected_command = @commands.detect{|cmd| cmd.matches? command_string }
        detected_command
      elsif parent_context
        parent_context.resolve(command_string)
      else
        raise "Command #{command_string} could not be resolved from the current context."
      end
    end  
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stub_shell-0.0.3 lib/stub_shell/shell.rb
stub_shell-0.0.2 lib/stub_shell/shell.rb
stub_shell-0.0.1 lib/stub_shell/shell.rb