class YARD::CLI::YRI

A tool to view documentation in the console like ‘ri`

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

Other tags:
    See: #run -
def self.run(*args) new.run(*args) end

def add_default_paths

Other tags:
    Since: - 0.5.1
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

def add_gem_paths

Returns:
  • (void) -
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

def cache_object(name, path)

Returns:
  • (void) -
def cache_object(name, path)
  return if path == Registry.yardoc_file
  @cache[name] = path
  File.open!(CACHE_FILE, 'w') do |file|
    @cache.each do |key, value|
      file.puts("#{key} #{value}")
    end
  end
end

def description

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

def find_object(name)

Returns:
  • (nil) - if no object is found
  • (CodeObjects::Base) - an object if found

Parameters:
  • name (String) -- the full name of the object
def find_object(name)
  @search_paths.unshift(@cache[name]) if @cache[name]
  @search_paths.unshift(Registry.yardoc_file)
  # Try to load it from in memory cache
  log.debug "Searching for #{name} in memory"
  obj = try_load_object(name, nil)
  return obj if obj
  log.debug "Searching for #{name} in search paths"
  @search_paths.each do |path|
    next unless File.exist?(path)
    log.debug "Searching for #{name} in #{path}..."
    Registry.load(path)
    obj = try_load_object(name, path)
    return obj if obj
  end
  nil
end

def initialize

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

def load_cache

Returns:
  • (void) -
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

def optparse(*args)

Parameters:
  • args (Array) -- each tokenized argument
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

def print_object(object)

Returns:
  • (String) - the formatted output for an object.

Parameters:
  • object (CodeObjects::Base) -- the object to print.
def print_object(object)
  if object.type == :method && object.is_alias?
    tmp = P(object.namespace, (object.scope == :instance ? "#" : "") +
      object.namespace.aliases[object].to_s)
    object = tmp unless YARD::CodeObjects::Proxy === tmp
  end
  object.format(:serializer => @serializer)
end

def print_usage

Other tags:
    Since: - 0.5.6

Returns:
  • (void) -
def print_usage
  log.puts "Usage: yri [options] <Path to object>"
  log.puts "See yri --help for more options."
end

def run(*args)

Parameters:
  • args (Array) -- each tokenized argument
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

def try_load_object(name, cache_path)

Returns:
  • (void) -

Parameters:
  • cache_path (String) -- the location of the yardoc
  • name (String) -- the object path
def try_load_object(name, cache_path)
  obj = Registry.at(name)
  cache_object(name, cache_path) if obj && cache_path
  obj
end