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.



15
16
17
18
# File 'lib/completely/completions.rb', line 15

def initialize(config, function_name: nil)
  @config = config.is_a?(Config) ? config : Config.new(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
# File 'lib/completely/completions.rb', line 9

def load(config_path, function_name: nil)
  config = Config.load config_path
  new config, function_name: function_name
end

Instance Method Details

#flat_configObject



20
21
22
# File 'lib/completely/completions.rb', line 20

def flat_config
  @flat_config ||= config.flat_config
end

#patternsObject



24
25
26
# File 'lib/completely/completions.rb', line 24

def patterns
  @patterns ||= patterns!
end

#scriptObject



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

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

#testerObject



47
48
49
# File 'lib/completely/completions.rb', line 47

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

#valid?Boolean

Returns:

  • (Boolean)


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

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

#wrapper_function(name = nil) ⇒ Object



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

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