Class: DebugBar::RecipeBook::Base
- Inherits:
-
Object
- Object
- DebugBar::RecipeBook::Base
- 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:
-
The method name must be the recipe name suffixed with ‘_recipe’. So the recipe
:foo
would have method name
foo_recipe
-
Recipe factory methods MUST return a valid callback when no arguments are given, that is
book.foo_recipe()
must work.
-
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. -
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('<','<'), :hidden => (body.length>160)]
end
end
It could then be added to the DebugBar like so:
.add(:params)
.add(:params, :formatter => :pretty_inspect)
Direct Known Subclasses
Instance Method Summary collapse
-
#include?(recipe) ⇒ Boolean
(also: #has_recipe?)
Returns true if the given recipe is known.
-
#recipe(recipe, *args, &block) ⇒ Object
(also: #[])
Generates the given recipe.
-
#recipes ⇒ Object
Returns a list of recipes known to this class.
-
#template_search_paths ⇒ Object
Retrieves the template search paths for this recipe instance as fully expanded Pathname instances.
-
#template_search_paths=(paths) ⇒ Object
Sets the template search paths for this recipe instance, converting to pathname objects as necessary.
Instance Method Details
#include?(recipe) ⇒ Boolean Also known as: has_recipe?
Returns true if the given recipe is known.
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 |
#recipes ⇒ Object
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_paths ⇒ Object
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). } end |