class DebugBar::RecipeBook::Base

The base class for all recipe subclasses. Provides common convenience methods for recipe use. Essentially, these are factory methods that lazy generate common configurable callbacks on demand.

Subclasses need only to define factory instance methods that meet the following rules:

  1. The method name must be the recipe name suffixed with ‘_recipe’. So the recipe

    :foo
    

    would have method name

    foo_recipe
    
  2. Recipe factory methods MUST return a valid callback when no arguments are given, that is

    book.foo_recipe()
    

    must work.

  3. The result of a recipe factory method must be a Proc object that conforms to the requirements of the Procs registered with add_callback on the DebugBar::Base class.

  4. Recipe methods may take an additional argument, which is an options hash for special configuration when using add_callback on DebugBar::Base instances. For example, one can then us

For example, the following recipe renders the params hash from the given binding:

def params_recipe(opts={})
  Proc.new do |b|
    body = (opts[:formatter] == :pretty_inspect) ? b[:params].pretty_inspect : b[:params].inspect
    ['Params', body.gsub('<','&lt;'), :hidden => (body.length>160)]
  end
end

It could then be added to the DebugBar like so:

debug_bar.add(:params)
debug_bar.add(:params, :formatter => :pretty_inspect)

Public Instance Methods

[](recipe, *args, &block)
Alias for: recipe
has_recipe?(recipe)
Alias for: include?
include?(recipe) click to toggle source

Returns true if the given recipe is known.

# File lib/debug-bar/recipe_book/base.rb, line 51
def include?(recipe)
  return self.respond_to?("#{recipe}_recipe")
end
Also aliased as: has_recipe?
recipe(recipe, *args, &block) click to toggle source

Generates the given recipe. All recipes are expected to accept no arguments, but may optionally take more. Optional arguments given to this method are passed through to the recipe method.

# File lib/debug-bar/recipe_book/base.rb, line 60
def recipe(recipe, *args, &block)
  return self.send("#{recipe}_recipe", *args, &block)
end
Also aliased as: []
recipes() click to toggle source

Returns a list of recipes known to this class.

# File lib/debug-bar/recipe_book/base.rb, line 46
def recipes
  return self.methods.select {|m| m.to_s =~ /_recipe$/}.map {|m| m.to_s.gsub(/_recipe$/,'').to_sym}
end
template_search_paths() click to toggle source

Retrieves the template search paths for this recipe instance as fully expanded Pathname instances.

While subclasses may override this method, it is preferrable for them to use the setter (template_search_paths=) during instance initialization, as the setter sanitizes the input.

# File lib/debug-bar/recipe_book/base.rb, line 71
def template_search_paths
  return @template_search_paths ||= []
end
template_search_paths=(paths) click to toggle source

Sets the template search paths for this recipe instance, converting to pathname objects as necessary.

# File lib/debug-bar/recipe_book/base.rb, line 77
def template_search_paths=(paths)
  @template_search_paths = paths.map {|path| Pathname.new(path.to_s).expand_path }
end

Private Instance Methods

read_template(template) click to toggle source

Reads the given template and returns the string of its contents. The template name may be either a symbol or string.

# File lib/debug-bar/recipe_book/base.rb, line 95
def read_template(template)
  template_name = "#{template}.html.erb"
  template_path = template_search_paths.map {|base_path| (base_path + template_name).expand_path}.find {|p| p.exist? && p.file?}
  raise ArgumentError, "Unknown template #{template_name.inspect}.  Not in #{template_search_paths.inspect}", caller if template_path.nil?
  return template_path.read
end
render_template(template, opts={}) click to toggle source

Renders the first matching template found in the search paths. Passed symbols/names are automatically suffixed with ‘html.erb’. The template name may be a symbol or string.

Optionally, one can pass in :locals, which is a hash of local variables to render in the template.

# File lib/debug-bar/recipe_book/base.rb, line 89
def render_template(template, opts={})
  return Erubis::Eruby.new( read_template(template) ).result( opts.fetch(:locals, {}) ).html_safe
end