123456789_123456789_123456789_123456789_123456789_

Class: YARD::CLI::YRI

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Command
Instance Chain:
self, Command
Inherits: YARD::CLI::Command
Defined in: lib/yard/cli/yri.rb

Overview

A tool to view documentation in the console like ri

Constant Summary

Class Method Summary

Command - Inherited

.run

Helper method to run the utility on an instance.

Instance Method Summary

Command - Inherited

Constructor Details

.newYRI

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 31

def initialize
  super
  @cache = {}
  @search_paths = []
  add_default_paths
  add_gem_paths
  load_cache
  @search_paths.uniq!
end

Class Method Details

.run(*args)

Helper method to run the utility on an instance.

See Also:

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 29

def self.run(*args) new.run(*args) end

Instance Method Details

#add_default_paths (private)

Adds paths in SEARCH_PATHS_FILE

Since:

  • 0.5.1

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 181

def add_default_paths
  @search_paths.concat(DEFAULT_SEARCH_PATHS)
  return unless File.file?(SEARCH_PATHS_FILE)
  paths = File.readlines(SEARCH_PATHS_FILE).map(&:strip)
  @search_paths.concat(paths)
end

#add_gem_pathsvoid (private)

This method returns an undefined value.

Adds all RubyGems yardoc files to search paths

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 161

def add_gem_paths
  require 'rubygems'
  gem_paths = []
  YARD::GemIndex.each do |spec|
    yfile = Registry.yardoc_file_for_gem(spec.name)
    next if yfile.nil?

    if spec.name =~ /^yard-doc-/
      gem_paths.unshift(yfile)
    else
      gem_paths.push(yfile)
    end
  end
  @search_paths += gem_paths
rescue LoadError
  nil # noop
end

#description

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 41

def description
  "A tool to view documentation in the console like `ri`"
end

#load_cachevoid (private)

This method returns an undefined value.

Loads CACHE_FILE

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 151

def load_cache
  return unless File.file?(CACHE_FILE)
  File.readlines(CACHE_FILE).each do |line|
    line = line.strip.split(/\s+/)
    @cache[line[0]] = line[1]
  end
end

#optparse(*args) (private)

Parses commandline options.

Parameters:

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 190

def optparse(*args)
  opts = OptionParser.new
  opts.banner = "Usage: yri [options] <Path to object>"
  opts.separator "Example: yri String#gsub"
  opts.separator ""
  opts.separator "General Options:"

  opts.on('-b', '--db FILE', 'Use a specified .yardoc db to search in') do |yfile|
    @search_paths.unshift(yfile)
  end

  opts.on('-T', '--no-pager', 'No pager') do
    @serializer = YARD::Serializers::StdoutSerializer.new
  end

  opts.on('-p PAGER', '--pager') do |pager|
    @serializer = YARD::Serializers::ProcessSerializer.new(pager)
  end

  common_options(opts)
  parse_options(opts, args)
  @name = args.first
end

#run(*args)

Runs the command-line utility.

Examples:

YRI.new.run('String#reverse')

Parameters:

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 50

def run(*args)
  optparse(*args)

  if ::RbConfig::CONFIG['host_os'] =~ /mingw|win32/
    @serializer ||= YARD::Serializers::StdoutSerializer.new
  else
    @serializer ||= YARD::Serializers::ProcessSerializer.new('less')
  end

  if @name.nil? || @name.strip.empty?
    print_usage
    return exit(1)
  end

  object = find_object(@name)
  if object
    print_object(object)
  else
    STDERR.puts "No documentation for `#{@name}'"
    return exit(1)
  end
end

#try_load_object(name, cache_path) ⇒ void (private)

This method returns an undefined value.

Tries to load the object with name. If successful, caches the object with the cache_path

Parameters:

  • name (String)

    the object path

  • cache_path (String)

    the location of the yardoc db containing the object to cache for future lookups. No caching is done if this is nil.

[ GitHub ]

  
# File 'lib/yard/cli/yri.rb', line 143

def try_load_object(name, cache_path)
  obj = Registry.at(name)
  cache_object(name, cache_path) if obj && cache_path
  obj
end