Module: Bond::Yard::Loader

Extended by:
Loader
Included in:
Loader
Defined in:
lib/bond/yard.rb

Overview

Generates and loads completions for methods that take a hash of options and have been documented with @option.

Instance Method Summary collapse

Instance Method Details

#create_completion_file(yardoc, completion_file) ⇒ Object



36
37
38
39
40
41
# File 'lib/bond/yard.rb', line 36

def create_completion_file(yardoc, completion_file)
  YARD::Registry.load!(yardoc)
  methods_hash = find_methods_with_options
  body = generate_method_completions(methods_hash)
  File.open(completion_file, 'w') {|e| e.write body }
end

#dir(subdir) ⇒ Object



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

def dir(subdir)
  (@dirs ||= {})[subdir] ||= begin
    require 'fileutils'
    FileUtils.mkdir_p File.join(M.home, '.bond', subdir)
    File.join(M.home, '.bond', subdir)
  end
end

#find_methods_with_optionsObject



66
67
68
69
70
71
72
# File 'lib/bond/yard.rb', line 66

def find_methods_with_options
  YARD::Registry.all(:method).inject({}) {|a,m|
    opts = m.tags.select {|e| e.is_a?(YARD::Tags::OptionTag) }.map {|e| e.pair.name }
    a[m.path] = opts if !opts.empty? && m.path
    a
  }
end

#find_yardoc(rubygem) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bond/yard.rb', line 43

def find_yardoc(rubygem)
  (file = YARD::Registry.yardoc_file_for_gem(rubygem) rescue nil) and return(file)
  if (file = M.find_gem_file(rubygem, rubygem+'.rb'))
    output_dir = File.join(dir('.yardocs'), rubygem)
    cmd = ['yardoc', '-n', '-b', output_dir]
    cmd << '-q' unless @options[:verbose]
    cmd += ['-c', output_dir] unless @options[:reload]
    cmd += [file, File.expand_path(file+'/..')+"/#{rubygem}/**/*.rb"]
    puts "Bond: "+cmd.join(' ') if @options[:verbose]
    puts "Bond: Building/loading #{rubygem}'s .yardoc database ..."
    system *cmd
    output_dir
  end
end

#generate_method_completions(methods_hash) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/bond/yard.rb', line 74

def generate_method_completions(methods_hash)
  methods_hash.map do |meth, options|
    options.map! {|e| e.sub(/^:/, '') }
    meth = meth.sub(/#initialize$/, '.new')
    %Q[complete(:method=>'#{meth}') {\n  #{options.inspect}\n}]
  end.join("\n")
end

#load_yard_gem(rubygem) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/bond/yard.rb', line 27

def load_yard_gem(rubygem)
  raise("Unable to find gem.") unless (yardoc = find_yardoc(rubygem))
  completion_file = File.join(dir('yard_completions'), rubygem+'.rb')
  create_completion_file(yardoc, completion_file) if !File.exists?(completion_file) || @options[:reload]
  M.load_file completion_file
rescue
  $stderr.puts "Bond Error: Didn't load yard completions for gem '#{rubygem}'. #{$!.message}"
end

#load_yard_gems(*gems) ⇒ Object



22
23
24
25
# File 'lib/bond/yard.rb', line 22

def load_yard_gems(*gems)
  @options = gems[-1].is_a?(Hash) ? gems.pop : {}
  gems.select {|e| load_yard_gem(e) }
end