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
25
# File 'lib/completely/completions.rb', line 22

def initialize(config, function_name: nil)
  @config = config
  @function_name = 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



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

def patterns
  @patterns ||= patterns!
end

#scriptObject



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

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

#testerObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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

#wrapper_function(name = nil) ⇒ Object



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

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

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

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