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.



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

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



86
87
88
89
90
91
92
# File 'lib/bond/m.rb', line 86

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.



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

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

#load_file(file) ⇒ Object

Loads a completion file in Rc namespace.



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

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

#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 by deleting all missions.



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

def reset
  MethodMission.reset
  @agent = nil
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



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

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