class YARD::Server::Commands::Base
@see #run
@abstract
consider subclassing the more specific {LibraryCommand} class instead.
Note that if your command deals directly with libraries, you should
See details in the #run
method documentation.
In your implementation, you should set the body and status for requests.
To implement a custom command, override the {#run} method, not {#call}.
== Subclassing Notes
# when a request comes in, cmd.myattr == ‘foo’
Adapter.new(libs, {:myattr => ‘foo’}).start
end
attr_accessor :myattr
class MyCommand < Base
automatically be mapped to attributes by the same name on your class.
method. When creating a custom command, the {Adapter#options} will
All attributes can be initialized via options passed into the {#initialize}
== Attribute Initializers
a Rack-style response.
a server. A command will be routed to by the {Router} class and return
This is the base command class used to implement custom commands for
def add_cache_control
Add a conservative cache control policy to reduce load on
def add_cache_control return if request.query_string.to_i == 0 headers['Cache-Control'] = 'private, max-age=300' end
def cache(data)
- See: StaticCaching -
Returns:
-
(String)
- the same cached data (for chaining)
Parameters:
-
data
(String
) -- the data to cache
Other tags:
- Example: Caching to memory -
def cache(data) if caching && adapter.document_root path = File.join(adapter.document_root, request.path_info.sub(/\.html$/, '') + '.html') path = path.sub(%r{/\.html$}, '.html') FileUtils.mkdir_p(File.dirname(path)) log.debug "Caching data to #{path}" File.open(path, 'wb') {|f| f.write(data) } end self.body = data end
def call(request)
-
(Array(Numeric,Hash,Array
- a Rack-style response))
Parameters:
-
request
(Adapter Dependent
) -- the request object
Other tags:
- Note: - This command should not be overridden by subclasses. Implement
def call(request) self.request = request self.path ||= request.path_info[1..-1] self.headers = {'Content-Type' => 'text/html'} self.body = '' self.status = 200 add_cache_control begin run rescue FinishRequest nil # noop rescue NotFoundError => e self.body = e.message if e.message != e.class.to_s not_found end # keep this to support commands setting status manually. not_found if status == 404 [status, headers, body.is_a?(Array) ? body : [body]] end
def initialize(opts = {})
-
opts
(Hash
) -- the options hash, saved to {#command_options}
Other tags:
- Example: Creating a Command -
def initialize(opts = {}) opts.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end self.command_options = opts end
def not_found
-
(void)
-
def not_found self.status = 404 return unless body.empty? self.body = "Not found: #{request.path}" headers['Content-Type'] = 'text/plain' headers['X-Cascade'] = 'pass' headers.delete('Cache-Control') end
def redirect(url)
-
(FinishRequest)
- causes the request to terminate.
Parameters:
-
url
(String
) -- the URL to redirect to
def redirect(url) headers['Location'] = url self.status = 302 raise FinishRequest end
def render(object = nil)
-
(String)
- the resulting output to display
Parameters:
-
object
(CodeObjects::Base, nil
) -- calls {CodeObjects::Base#format} if
Other tags:
- Todo: - This method is dependent on +#options+, it should be in {LibraryCommand}.
def render(object = nil) case object when CodeObjects::Base cache object.format(options) when nil cache Templates::Engine.render(options) else cache object end end
def run
-
(void)
-
Other tags:
- Abstract: -
Other tags:
- Example: A custom command -
def run raise NotImplementedError end