Module: Card::Set::Format
- Included in:
- Card::Set
- Defined in:
- lib/card/set/format.rb,
lib/card/set/format/haml_paths.rb,
lib/card/set/format/abstract_format.rb,
lib/card/set/format/abstract_format/wrapper.rb,
lib/card/set/format/abstract_format/view_opts.rb,
lib/card/set/format/abstract_format/haml_views.rb,
lib/card/set/format/abstract_format/view_definition.rb
Overview
This document explains how to use format blocks within mods. To make use of it, you will need to understand both mods and sets.
Within a card mod, you can define format blocks like the following:
format :html do
def beethoven
:rocks
end
end
The magic that happens here is that the method #beethoven is now applicable (and available) only to the cards in the set specified by the mod, and only when the card is rendering a view in the HTML format.
If you care, you can certainly learn about how all this works. How the set module
creates a module that looks something like Card::Set::Type::MyType::HtmlFormat
.
How the format object for a given card in the set includes this module dynamically
when it's initialized. And so on...
But as monkeys, we don't usually think about all that much, because we work in the set module space, which lets us focus on the card patterns.
Speaking of which, there are a few key patterns to be aware of:
- Just as in sets, format methods for narrower sets will override format methods for more general sets. So if a #beethoven method is defined for all cards and again for a specific card type, then the type method will override the all method when both apply.
Similarly, specific formats inherit from more general formats, and all formats inherit from the base format. If a format is not specified, the format block will define methods on the base format class.
format do def haydn :sucks end end
It is possible to use super to refer to overridden methods. For example
format :html do def haydn "<em>#{super}</em>" end end
Note: Set precedence has a higher priority than Format precedence.
#view and #before can both be called outside of a format block. They will be defined on the base format.
Some very powerful API calls (including view and before) are defined in AbstractFormat. These methods are always available in format blocks.
Defined Under Namespace
Modules: AbstractFormat, HamlPaths
Class Method Summary collapse
-
.layout_method_name(layout) ⇒ Object
name of method for layout used by wrapper.
-
.view_method_name(view) ⇒ Object
name of method for view used by #render.
-
.view_setting_method_name(view, setting_name) ⇒ Object
name of method for setting for a given view.
-
.wrapper_method_name(wrapper) ⇒ Object
name of method for wrapper used by wrapped views.
Instance Method Summary collapse
-
#before(view, &block) ⇒ Object
shortcut for AbstractFormat#before for when #before is called outside of a format block.
-
#format(*format_names, &block) ⇒ Object
define format behavior within a set module.
-
#view(*args, &block) ⇒ Object
shortcut for AbstractFormat#view for when #view is called outside of a format block.
Class Method Details
.layout_method_name(layout) ⇒ Object
name of method for layout used by wrapper
128 129 130 |
# File 'lib/card/set/format.rb', line 128 def layout_method_name layout "_layout_#{layout.to_name.key}" end |
.view_method_name(view) ⇒ Object
name of method for view used by #render
140 141 142 |
# File 'lib/card/set/format.rb', line 140 def view_method_name view "_view_#{view}" end |
.view_setting_method_name(view, setting_name) ⇒ Object
name of method for setting for a given view. used by #view_setting
146 147 148 |
# File 'lib/card/set/format.rb', line 146 def view_setting_method_name view, setting_name "_view_#{view}__#{setting_name}" end |
.wrapper_method_name(wrapper) ⇒ Object
name of method for wrapper used by wrapped views
134 135 136 |
# File 'lib/card/set/format.rb', line 134 def wrapper_method_name wrapper "_wrapper_#{wrapper}" end |
Instance Method Details
#before(view, &block) ⇒ Object
shortcut for Card::Set::Format::AbstractFormat#before for when #before is called outside of a format block
86 87 88 |
# File 'lib/card/set/format.rb', line 86 def before view, &block format { before view, &block } end |
#format(*format_names, &block) ⇒ Object
define format behavior within a set module
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/card/set/format.rb', line 65 def format *format_names, &block format_names.compact! if format_names.empty? format_names = [:base] elsif format_names.first == :all format_names = Card::Format.registered.reject { |f| Card::Format.aliases[f] } end format_names.each do |f| define_on_format f, &block end end |
#view(*args, &block) ⇒ Object
shortcut for Card::Set::Format::AbstractFormat#view for when #view is called outside of a format block
80 81 82 |
# File 'lib/card/set/format.rb', line 80 def view *args, &block format { view(*args, &block) } end |