Class: Yap::Shell::BuiltinCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/yap/shell/commands.rb

Instance Attribute Summary

Attributes inherited from Command

#args, #heredoc, #line, #str, #world

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#initialize

Constructor Details

This class inherits a constructor from Yap::Shell::Command

Class Method Details

.===(other) ⇒ Object



41
42
43
# File 'lib/yap/shell/commands.rb', line 41

def self.===(other)
  self.builtins.keys.include?(other.split(' ').first.to_sym) || super
end

.add(command, &action) ⇒ Object



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

def self.add(command, &action)
  builtins.merge!(command.to_sym => action)
end

.builtinsObject



45
46
47
48
49
50
51
# File 'lib/yap/shell/commands.rb', line 45

def self.builtins
  @builtins ||= {
    builtins: lambda { |stdout:, **| stdout.puts @builtins.keys.sort },
    exit: lambda { |code = 0, **| exit(code.to_i) },
    fg: lambda{ |**| :resume },
  }
end

Instance Method Details

#execute(stdin:, stdout:, stderr:) ⇒ Object



57
58
59
60
# File 'lib/yap/shell/commands.rb', line 57

def execute(stdin:, stdout:, stderr:)
  action = self.class.builtins.fetch(str.to_sym){ raise("Missing proc for builtin: '#{builtin}' in #{str.inspect}") }
  action.call world:world, args:args, stdin:stdin, stdout:stdout, stderr:stderr
end

#to_executable_strObject

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/yap/shell/commands.rb', line 66

def to_executable_str
  raise NotImplementedError, "#to_executable_str is not implemented on BuiltInCommand"
end

#typeObject



62
63
64
# File 'lib/yap/shell/commands.rb', line 62

def type
  :BuiltinCommand
end