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, #to_s

Constructor Details

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

Class Method Details

.===(other) ⇒ Object



47
48
49
# File 'lib/yap/shell/commands.rb', line 47

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

.add(command, &action) ⇒ Object



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

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

.builtinsObject



51
52
53
54
55
56
57
# File 'lib/yap/shell/commands.rb', line 51

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



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

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)


72
73
74
# File 'lib/yap/shell/commands.rb', line 72

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

#typeObject



68
69
70
# File 'lib/yap/shell/commands.rb', line 68

def type
  :BuiltinCommand
end