Module: Bond::M

Extended by:
M
Included in:
M
Defined in:
lib/bond/m.rb

Overview

Takes international quagmires (a user’s completion setup) and passes them on as missions to an Agent.

Instance Method Summary collapse

Instance Method Details

#agentObject



28
29
30
# File 'lib/bond/m.rb', line 28

def agent
  @agent ||= Agent.new(config)
end

#complete(options = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/bond/m.rb', line 8

def complete(options={}, &block)
  if (result = agent.complete(options, &block)).is_a?(String)
    $stderr.puts "Bond Error: "+result
    false
  else
    true
  end
end

#configObject



33
34
35
# File 'lib/bond/m.rb', line 33

def config
  @config ||= {:debug => false, :default_search => :underscore}
end

#debrief(options = {}) ⇒ Object

Validates and sets values in M.config.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bond/m.rb', line 49

def debrief(options={})
  config.merge! options
  config[:readline] ||= default_readline
  if !config[:readline].is_a?(Module) &&
    Bond.const_defined?(config[:readline].to_s.capitalize)
    config[:readline] = Bond.const_get(config[:readline].to_s.capitalize)
  end
  unless %w{setup line_buffer}.all? {|e| config[:readline].respond_to?(e) }
    $stderr.puts "Bond Error: Invalid readline plugin '#{config[:readline]}'."
  end
end

#find_gem_file(rubygem, file) ⇒ Object

Finds the full path to a gem’s file relative it’s load path directory. Returns nil if not found.



83
84
85
86
# File 'lib/bond/m.rb', line 83

def find_gem_file(rubygem, file)
  begin gem(rubygem); rescue Exception; end
  (dir = $:.find {|e| File.exist?(File.join(e, file)) }) && File.join(dir, file)
end

#homeObject

Find a user’s home in a cross-platform way



109
110
111
112
113
114
115
# File 'lib/bond/m.rb', line 109

def home
  ['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
  return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
  File.expand_path("~")
rescue
  File::ALT_SEPARATOR ? "C:/" : "/"
end

#load_dir(base_dir) ⇒ Object

Loads completion files in given directory.



96
97
98
99
100
101
# File 'lib/bond/m.rb', line 96

def load_dir(base_dir)
  if File.exist?(dir = File.join(base_dir, 'completions'))
    Dir[dir + '/*.rb'].each {|file| load_file(file) }
    true
  end
end

#load_file(file) ⇒ Object

Loads a completion file in Rc namespace.



89
90
91
92
93
# File 'lib/bond/m.rb', line 89

def load_file(file)
  Rc.module_eval File.read(file)
rescue Exception => e
  $stderr.puts "Bond Error: Completion file '#{file}' failed to load with:", e.message
end

#load_gems(*gems) ⇒ Object

Loads completions from gems



104
105
106
# File 'lib/bond/m.rb', line 104

def load_gems(*gems)
  gems.select {|e| load_gem_completion(e) }
end

#recomplete(options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/bond/m.rb', line 18

def recomplete(options={}, &block)
  if (result = agent.recomplete(options, &block)).is_a?(String)
    $stderr.puts "Bond Error: "+result
    false
  else
    true
  end
end

#resetObject

Resets M’s missions and config



38
39
40
41
# File 'lib/bond/m.rb', line 38

def reset
  MethodMission.reset
  @config = @agent = nil
end

#restart(options = {}, &block) ⇒ Object



62
63
64
65
# File 'lib/bond/m.rb', line 62

def restart(options={}, &block)
  reset
  start(options, &block)
end

#spy(input) ⇒ Object

See Bond#spy



44
45
46
# File 'lib/bond/m.rb', line 44

def spy(input)
  agent.spy(input)
end

#start(options = {}, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/bond/m.rb', line 68

def start(options={}, &block)
  debrief options
  @started = true
  load_completions
  Rc.module_eval(&block) if block
  true
end

#started?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/bond/m.rb', line 77

def started?
  !!@started
end