Class: YARD::CLI::YRI

Inherits:
Command show all
Defined in:
lib/yard/cli/yri.rb

Overview

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

Constant Summary collapse

CACHE_FILE =

The location in YARD::CONFIG_DIR where the YRI cache file is loaded from.

File.expand_path('~/.yard/yri_cache')
SEARCH_PATHS_FILE =

A file containing all paths, delimited by newlines, to search for yardoc databases.

Since:

  • 0.5.1

File.expand_path('~/.yard/yri_search_paths')
DEFAULT_SEARCH_PATHS =

Default search paths that should be loaded dynamically into YRI. These paths take precedence over all other paths (SEARCH_PATHS_FILE and RubyGems paths). To add a path, call:

DEFAULT_SEARCH_PATHS.push("/path/to/.yardoc")

Returns:

Since:

  • 0.6.0

[]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#common_options, #parse_options

Constructor Details

#initializeYRI

Returns a new instance of YRI.



30
31
32
33
34
35
36
37
38
# File 'lib/yard/cli/yri.rb', line 30

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

Class Method Details

.run(*args) ⇒ Object

Helper method to run the utility on an instance.

See Also:



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

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

Instance Method Details

#cache_object(name, path) ⇒ void (protected)

This method returns an undefined value.

Caches the .yardoc file where an object can be found in the CACHE_FILE



81
82
83
84
85
86
87
88
89
90
# File 'lib/yard/cli/yri.rb', line 81

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

#descriptionObject



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

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

#find_object(name) ⇒ CodeObjects::Base? (protected)

Locates an object by name starting in the cached paths and then searching through any search paths.

Parameters:

  • name (String)

    the full name of the object

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/yard/cli/yri.rb', line 109

def find_object(name)
  @search_paths.unshift(@cache[name]) if @cache[name]
  @search_paths.unshift(Registry.yardoc_file)
  
  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 = Registry.at(name)
    if obj
      cache_object(name, path)
      return obj
    end
  end
  nil
end

Returns the formatted output for an object.

Parameters:

Returns:

  • (String)

    the formatted output for an object.



94
95
96
97
98
99
100
101
# File 'lib/yard/cli/yri.rb', line 94

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

This method returns an undefined value.

Prints the command usage

Since:

  • 0.5.6



74
75
76
77
# File 'lib/yard/cli/yri.rb', line 74

def print_usage
  puts "Usage: yri [options] <Path to object>"
  puts "See yri --help for more options."
end

#run(*args) ⇒ Object

Runs the command-line utility.

Examples:

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

Parameters:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yard/cli/yri.rb', line 49

def run(*args)
  optparse(*args)
  
  if Config::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
    exit(1)
  elsif object = find_object(@name)
    print_object(object)
  else
    STDERR.puts "No documentation for `#{@name}'"
    exit(1)
  end
end