Class: Lightning::CompletionMap

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning/completion_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*globs) ⇒ CompletionMap

Returns a new instance of CompletionMap.



7
8
9
10
11
12
# File 'lib/lightning/completion_map.rb', line 7

def initialize(*globs)
  options = globs[-1].is_a?(Hash) ? globs.pop : {}
  globs.flatten!
  @map = create_map_for_globs(globs)
  @alias_map = (options[:global_aliases] || {}).merge(options[:aliases] || {})
end

Instance Attribute Details

#alias_mapObject (readonly)

Returns the value of attribute alias_map.



5
6
7
# File 'lib/lightning/completion_map.rb', line 5

def alias_map
  @alias_map
end

#mapObject

Returns the value of attribute map.



4
5
6
# File 'lib/lightning/completion_map.rb', line 4

def map
  @map
end

Instance Method Details

#[](completion) ⇒ Object



14
15
16
# File 'lib/lightning/completion_map.rb', line 14

def [](completion)
  @map[completion] || @alias_map[completion]
end

#create_map_for_globs(globs) ⇒ Object

should return hash



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lightning/completion_map.rb', line 23

def create_map_for_globs(globs)
  path_hash = {}
  ignore_paths = ['.', '..'] + Lightning.ignore_paths
  globs.each do |d|
    Dir.glob(d, File::FNM_DOTMATCH).each do |e|
      basename = File.basename(e)
      unless ignore_paths.include?(basename)
        #save paths of duplicate basenames to process later
        if path_hash.has_key?(basename)
          if path_hash[basename].is_a?(Array)
            path_hash[basename] << e
          else
            path_hash[basename] = [path_hash[basename], e]
          end
        else
          path_hash[basename] = e
        end
      end
    end
  end
  map_duplicate_basenames(path_hash)
  path_hash
end

#keysObject



18
19
20
# File 'lib/lightning/completion_map.rb', line 18

def keys
  (@map.keys + @alias_map.keys).uniq
end

#map_duplicate_basenames(path_hash) ⇒ Object

map saved duplicates



48
49
50
51
52
53
54
55
56
# File 'lib/lightning/completion_map.rb', line 48

def map_duplicate_basenames(path_hash)
  path_hash.select {|k,v| v.is_a?(Array)}.each do |key,paths|
    paths.each do |e|
      new_key = "#{key}/#{File.dirname(e)}"
      path_hash[new_key] = e
    end
    path_hash.delete(key)
  end
end