Class: Completely::Completions

Inherits:
Object
  • Object
show all
Defined in:
lib/completely/completions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, function_name: nil) ⇒ Completions

Returns a new instance of Completions.



22
23
24
# File 'lib/completely/completions.rb', line 22

def initialize(config, function_name: nil)
  @config, @function_name = config, function_name
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/completely/completions.rb', line 6

def config
  @config
end

Class Method Details

.load(config_path, function_name: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/completely/completions.rb', line 9

def load(config_path, function_name: nil)
  begin
    data = YAML.load_file config_path, aliases: true
  rescue ArgumentError
    # :nocov:
    data = YAML.load_file config_path
    # :nocov:
  end
  
  new data, function_name: function_name
end

Instance Method Details

#patternsObject



26
27
28
# File 'lib/completely/completions.rb', line 26

def patterns
  @patterns ||= patterns!
end

#scriptObject



34
35
36
# File 'lib/completely/completions.rb', line 34

def script
  ERB.new(template, trim_mode: '%-').result(binding)
end

#testerObject



49
50
51
# File 'lib/completely/completions.rb', line 49

def tester
  @tester ||= Tester.new script: script, function_name: function_name
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/completely/completions.rb', line 30

def valid?
  pattern_prefixes.uniq.count == 1
end

#wrapper_function(name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/completely/completions.rb', line 38

def wrapper_function(name = nil)
  name ||= "send_completions"

  script_lines = script.split("\n").map do |line|
    clean_line = line.gsub("'") { "\\'" }
    %Q[  echo $'#{clean_line}']
  end.join("\n")

  "#{name}() {\n#{script_lines}\n}"
end