Module: Rails::Sh::Command

Defined in:
lib/rails/sh/command.rb

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



28
29
30
# File 'lib/rails/sh/command.rb', line 28

def [](name)
  commands[name.to_sym]
end

.clearObject



54
55
56
57
# File 'lib/rails/sh/command.rb', line 54

def clear
  commands.clear
  completions.clear
end

.command_namesObject



24
25
26
# File 'lib/rails/sh/command.rb', line 24

def command_names
  commands.keys
end

.commandsObject



5
6
7
# File 'lib/rails/sh/command.rb', line 5

def commands
  @commands ||= {}
end

.completion_procObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails/sh/command.rb', line 32

def completion_proc
  lambda { |line|
    regex = /#{Regexp.quote(line)}/
    completions.map { |completion|
      case completion
      when String
        completion if completion.match(regex)
      when Proc
        completion.call(line)
      end
    }.compact
  }
end

.completionsObject



46
47
48
# File 'lib/rails/sh/command.rb', line 46

def completions
  @completions ||= []
end

.completions=(completions) ⇒ Object



50
51
52
# File 'lib/rails/sh/command.rb', line 50

def completions=(completions)
  @completions = completions
end

.define(*names, &block) ⇒ Object



9
10
11
12
13
14
# File 'lib/rails/sh/command.rb', line 9

def define(*names, &block)
  names.each do |name|
    commands[name.to_sym] = block
    completions << name.to_s
  end
end

.find(line) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rails/sh/command.rb', line 16

def find(line)
  if name = line.split(/\s+/, 2)[0]
    commands[name.to_sym]
  else
    nil
  end
end