Class: DebugBar::Base
- Inherits:
-
Object
- Object
- DebugBar::Base
- Defined in:
- lib/debug-bar/base.rb
Overview
Overview
DebugBar::Base provides the base methods for all debug bars.
At it’s core, a DebugBar is instantiated with initialize
, gets callbacks added with add_callback
, and then is rendered with render
.
Additionally, RecipeBook classes or instance may be added to the DebugBar via add_recipe_book
so that pre-made callbacks may be easily added to the DebugBar instance via add_callbacks.
See the README for example usage.
Subclassing
This class is often subclassed to give DebugBars with special behaviors. If you make a subclass, define private overrides to these methods:
default_recipe_books
-
Provide a list of recipe books to make available to all instances.
default_recipes
-
Add a list of recipe callbacks to all instances.
template_search_paths
-
Override the default formatting template search path.
Direct Known Subclasses
Constant Summary collapse
- TEMPLATE_SEARCH_PATHS =
The search path for formatting templates, such as the layout and callback box. NOTE: This is separate from templates that are used in recipes!
[ (Pathname.new(__FILE__).dirname + '../templates') ].map {|path| path.}
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns a copy of the raw list of callbacks.
-
#recipe_books ⇒ Object
readonly
Returns a copy of the list of recipe book instances.
Instance Method Summary collapse
-
#add_callback(recipe = nil, *args, &callback) ⇒ Object
(also: #add_recipe, #add)
Adds a callback.
-
#add_recipe_book(book) ⇒ Object
(also: #add_book)
Adds a recipe book class or instance to the recipe book list for this debug bar.
-
#initialize(*recipes) {|_self| ... } ⇒ Base
constructor
Initialize a new debug bar.
-
#recipe_callback(recipe, *args, &block) ⇒ Object
Returns the most recently added occurance of the given recipe.
-
#recipes ⇒ Object
Returns the list of recipes recognized by this debug bar.
-
#render(eval_binding) ⇒ Object
Renders the debug bar with the given binding.
Constructor Details
#initialize(*recipes) {|_self| ... } ⇒ Base
Initialize a new debug bar. This may optionally take one or more recipe symbols as arguments.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/debug-bar/base.rb', line 41 def initialize(*recipes) #Initialize registration variables. @callbacks = [] @recipe_books = [] # Register defaults. default_recipe_books.each {|book| add_recipe_book(book)} default_recipes.each {|recipe| add_recipe(recipe)} # Give a chance for custom configuration, including addition of books. yield self if block_given? # Now we can add user listed recipes. recipes.each {|recipe| add_recipe(recipe)} end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns a copy of the raw list of callbacks.
55 56 57 |
# File 'lib/debug-bar/base.rb', line 55 def callbacks @callbacks end |
#recipe_books ⇒ Object (readonly)
Returns a copy of the list of recipe book instances.
58 59 60 |
# File 'lib/debug-bar/base.rb', line 58 def recipe_books @recipe_books end |
Instance Method Details
#add_callback(recipe = nil, *args, &callback) ⇒ Object Also known as: add_recipe, add
Adds a callback.
Takes either a recipe (by symbol) or a block.
The block takes a single argument, the binding of the render context, and should return either a string, or an array of [title, content, opts].
Advanced users can call a recipe by name, and provide additional arguments to configure the recipe further. These arguments are defined by the recipe factory method, but usually are via an options hash and/or a block.
Returns self to support functional programming styles.
94 95 96 97 98 99 |
# File 'lib/debug-bar/base.rb', line 94 def add_callback(recipe=nil, *args, &callback) callback_proc = recipe.nil? ? callback : recipe_callback(recipe, *args, &callback) raise ArgumentError, "Expected callback to respond to `call': #{callback_proc.inspect}", caller unless callback_proc.respond_to?(:call) @callbacks << callback_proc return self end |
#add_recipe_book(book) ⇒ Object Also known as: add_book
Adds a recipe book class or instance to the recipe book list for this debug bar.
Returns self to support functional programming styles.
64 65 66 67 |
# File 'lib/debug-bar/base.rb', line 64 def add_recipe_book(book) @recipe_books << (book.kind_of?(Class) ? book.new : book) return self end |
#recipe_callback(recipe, *args, &block) ⇒ Object
Returns the most recently added occurance of the given recipe.
76 77 78 79 80 |
# File 'lib/debug-bar/base.rb', line 76 def recipe_callback(recipe, *args, &block) book = @recipe_books.reverse.find {|book| book.include?(recipe)} raise ArgumentError, "Could not find recipe #{recipe.inspect}", caller if book.nil? return book.recipe(recipe, *args, &block) end |
#recipes ⇒ Object
Returns the list of recipes recognized by this debug bar.
71 72 73 |
# File 'lib/debug-bar/base.rb', line 71 def recipes return @recipe_books.inject([]) {|list,book| list | book.recipes} end |
#render(eval_binding) ⇒ Object
Renders the debug bar with the given binding.
104 105 106 107 108 |
# File 'lib/debug-bar/base.rb', line 104 def render(eval_binding) # Decorate the binding here (NOT in private methods where we don't want automatic behavior)! eval_binding.extend(DebugBar::Ext::Binding) return render_layout(eval_binding) end |