Class: Toys::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_dir_name: nil, config_file_name: nil, index_file_name: nil) ⇒ Lookup

Returns a new instance of Lookup.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/toys/lookup.rb', line 3

def initialize(config_dir_name: nil, config_file_name: nil, index_file_name: nil)
  @config_dir_name = config_dir_name
  if @config_dir_name && File.extname(@config_dir_name) == ".rb"
    raise LookupError, "Illegal config dir name #{@config_dir_name.inspect}"
  end
  @config_file_name = config_file_name
  if @config_file_name && File.extname(@config_file_name) != ".rb"
    raise LookupError, "Illegal config file name #{@config_file_name.inspect}"
  end
  @index_file_name = index_file_name
  if @index_file_name && File.extname(@index_file_name) != ".rb"
    raise LookupError, "Illegal index file name #{@index_file_name.inspect}"
  end
  @load_worklist = []
  @tools = {[] => [Tool.new(nil, nil), nil]}
  @max_priority = @min_priority = 0
end

Instance Method Details

#add_config_paths(paths, high_priority: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/toys/lookup.rb', line 32

def add_config_paths(paths, high_priority: false)
  paths = Array(paths)
  paths = paths.reverse if high_priority
  paths.each do |path|
    if !File.directory?(path) || !File.readable?(path)
      raise LookupError, "Cannot read config directory #{path}"
    end
    priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1)
    if @config_file_name
      p = File.join(path, @config_file_name)
      if !File.directory?(p) && File.readable?(p)
        @load_worklist << [p, [], priority]
      end
    end
    if @config_dir_name
      p = File.join(path, @config_dir_name)
      if File.directory?(p) && File.readable?(p)
        @load_worklist << [p, [], priority]
      end
    end
  end
  self
end

#add_paths(paths, high_priority: false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/toys/lookup.rb', line 21

def add_paths(paths, high_priority: false)
  paths = Array(paths)
  paths = paths.reverse if high_priority
  paths.each do |path|
    path = check_path(path)
    priority = high_priority ? (@max_priority += 1) : (@min_priority -= 1)
    @load_worklist << [path, [], priority]
  end
  self
end

#get_tool(words, priority) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/toys/lookup.rb', line 70

def get_tool(words, priority)
  if @tools.key?(words)
    tool, tool_priority = @tools[words]
    return tool if tool_priority.nil? || tool_priority == priority
    return nil if tool_priority > priority
  end
  parent = get_tool(words[0..-2], priority)
  return nil if parent.nil?
  tool = Tool.new(parent, words.last)
  @tools[words] = [tool, priority]
  tool
end

#include_path(path, words, remaining_words, priority) ⇒ Object



109
110
111
# File 'lib/toys/lookup.rb', line 109

def include_path(path, words, remaining_words, priority)
  handle_path(check_path(path), words, remaining_words, priority)
end

#list_subtools(words, recursive) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/toys/lookup.rb', line 87

def list_subtools(words, recursive)
  found_tools = []
  len = words.length
  @tools.each do |n, tp|
    if n.length > 0
      if !recursive && n.slice(0..-2) == words ||
          recursive && n.length > len && n.slice(0, len) == words
        found_tools << tp.first
      end
    end
  end
  found_tools.sort do |a, b|
    a = a.full_name
    b = b.full_name
    while !a.empty? && !b.empty? && a.first == b.first
      a = a.slice(1..-1)
      b = b.slice(1..-1)
    end
    a.first.to_s <=> b.first.to_s
  end
end

#lookup(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/toys/lookup.rb', line 56

def lookup(args)
  orig_prefix = args.take_while{ |arg| !arg.start_with?("-") }
  cur_prefix = orig_prefix.dup
  loop do
    load_for_prefix(cur_prefix)
    p = orig_prefix.dup
    while p.length >= cur_prefix.length
      return @tools[p].first if @tools.key?(p)
      p.pop
    end
    raise "Bug: No tools found" unless cur_prefix.pop
  end
end

#tool_defined?(words) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/toys/lookup.rb', line 83

def tool_defined?(words)
  @tools.key?(words)
end