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



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

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

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



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

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

#configObject



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

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

#debrief(options = {}) ⇒ Object

Validates and sets values in M.config.



48
49
50
51
52
53
54
55
# File 'lib/bond/m.rb', line 48

def debrief(options={})
  config.merge! options
  plugin_methods = %w{setup line_buffer}
  unless config[:readline_plugin].is_a?(Module) &&
    plugin_methods.all? {|e| config[:readline_plugin].instance_methods.map {|f| f.to_s}.include?(e)}
    $stderr.puts "Bond Error: Invalid readline plugin given."
  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.



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

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

#homeObject

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



104
105
106
107
108
109
110
# File 'lib/bond/m.rb', line 104

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.



91
92
93
94
95
96
# File 'lib/bond/m.rb', line 91

def load_dir(base_dir)
  if File.exists?(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.



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

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



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

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

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



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

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



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

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

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



58
59
60
61
# File 'lib/bond/m.rb', line 58

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

#spy(input) ⇒ Object

See Bond#spy



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

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

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



64
65
66
67
68
69
70
# File 'lib/bond/m.rb', line 64

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

#started?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/bond/m.rb', line 73

def started?
  !!@started
end