Class: DebugBar::RecipeBook::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/debug-bar/recipe_book/base.rb

Overview

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)

Direct Known Subclasses

Default

Instance Method Summary collapse

Instance Method Details

#include?(recipe) ⇒ Boolean Also known as: has_recipe?

Returns true if the given recipe is known.

Returns:

  • (Boolean)


51
52
53
# File 'lib/debug-bar/recipe_book/base.rb', line 51

def include?(recipe)
  return self.respond_to?("#{recipe}_recipe")
end

#recipe(recipe, *args, &block) ⇒ Object Also known as: []

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.



60
61
62
# File 'lib/debug-bar/recipe_book/base.rb', line 60

def recipe(recipe, *args, &block)
  return self.send("#{recipe}_recipe", *args, &block)
end

#recipesObject

Returns a list of recipes known to this class.



46
47
48
# 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_pathsObject

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.



71
72
73
# File 'lib/debug-bar/recipe_book/base.rb', line 71

def template_search_paths
  return @template_search_paths ||= []
end

#template_search_paths=(paths) ⇒ Object

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



77
78
79
# 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