123456789_123456789_123456789_123456789_123456789_

Class: YARD::Server::Commands::SearchCommand

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: YARD::Server::Commands::LibraryCommand
Defined in: lib/yard/server/commands/search_command.rb

Overview

Performs a search over the objects inside of a library and returns the results as HTML or plaintext

Since:

  • 0.6.0

Class Method Summary

LibraryCommand - Inherited

Base - Inherited

.new

Creates a new command object, setting attributes named by keys in the options hash.

Instance Attribute Summary

Instance Method Summary

::YARD::Server::DocServerHelper - Included

#abs_url, #base_path, #mtime, #mtime_url, #router,
#url_for

Modifies Templates::Helpers::HtmlHelper#url_for to return a URL instead of a disk location.

#url_for_file

Modifies Templates::Helpers::HtmlHelper#url_for_file to return a URL instead of a disk location.

#url_for_frameset

Returns the frames URL for the page.

#url_for_index

Returns the URL for the alphabetic index page.

#url_for_list

Modifies Templates::Helpers::HtmlHelper#url_for_list to return a URL based on the list prefix instead of a HTML filename.

#url_for_main

Returns the main URL, first checking a readme and then linking to the index.

::YARD::Templates::Helpers::ModuleHelper - Included

#prune_method_listing

Prunes the method listing by running the verifier and removing attributes/aliases.

::YARD::Templates::Helpers::BaseHelper - Included

#format_object_title, #format_object_type,
#format_source

Indents and formats source code.

#format_types

Formats a list of return types for output and links each type.

#globals

An object that keeps track of global state throughout the entire template rendering process (including any sub-templates).

#h

Escapes text.

#link_file

Links to an extra file.

#link_include_file

Include a file as a docstring in output.

#link_include_object

Includes an object's docstring into output.

#link_object

Links to an object with an optional title.

#link_url

Links to a URL.

#linkify

Links objects or URLs.

#run_verifier

Runs a list of objects against the ::YARD::Verifier object passed into the template and returns the subset of verified objects.

LibraryCommand - Inherited

#call, #call_with_fork, #call_without_fork,
#fulldoc_template

Hack to load a custom fulldoc template object that does not do any rendering/generation.

#load_yardoc, #not_prepared, #restore_template_info, #save_default_template_info, #setup_library, #setup_yardopts

Base - Inherited

#call

The main method called by a router with a request object.

#run

Subclass this method to implement a custom command.

#add_cache_control

Add a conservative cache control policy to reduce load on requests served with "?1234567890" style timestamp query strings.

Constructor Details

This class inherits a constructor from YARD::Server::Commands::LibraryCommand

Instance Attribute Details

#query (rw)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 12

attr_accessor :results, :query

#results (rw)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 12

attr_accessor :results, :query

Instance Method Details

#run

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 14

def run
  Registry.load_all
  self.query = request.query['q']
  redirect(abs_url(adapter.router.docs_prefix, single_library ? library : '')) if query.nil? || query =~ /\A\s*\Z/

  found = Registry.at(query)
  redirect(url_for(found)) if found

  search_for_object
  request.xhr? ? serve_xhr : serve_normal
end

#search_for_object (private)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 58

def search_for_object
  # rubocop:disable Style/MultilineBlockChain
  self.results = run_verifier(Registry.all).select do |o|
    o.path.downcase.include?(query.downcase)
  end.reject do |o|
    name = (o.type == :method ? o.name(true) : o.name).to_s.downcase
    !name.include?(query.downcase) ||
      case o.type
      when :method
        !(query =~ /[#.]/) && query.include?("::")
      when :class, :module, :constant, :class_variable
        query =~ /[#.]/
      end
  end.sort_by do |o|
    name = (o.type == :method ? o.name(true) : o.name).to_s
    name.length.to_f / query.length.to_f
  end
end

#serve_normal (private)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 47

def serve_normal
  options.update(
    :visible_results => visible_results,
    :query => query,
    :results => results,
    :template => :doc_server,
    :type => :search
  )
  self.body = Templates::Engine.render(options)
end

#serve_xhr (private)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 37

def serve_xhr
  headers['Content-Type'] = 'text/plain'
  self.body = visible_results.map {|o|
    [(o.type == :method ? o.name(true) : o.name).to_s,
     o.path,
     o.namespace.root? ? '' : o.namespace.path,
     url_for(o)].join(",")
  }.join("\n")
end

#url_for(object) (private)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 32

def url_for(object)
  abs_url(base_path(router.docs_prefix),
    serializer.serialized_path(object))
end

#visible_results

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/search_command.rb', line 26

def visible_results
  results[0, 10]
end