Module: Card::Set::Format

Included in:
Card::Set
Defined in:
card/lib/card/set/format.rb,
card/lib/card/set/format/haml_paths.rb,
card/lib/card/set/format/abstract_format.rb,
card/lib/card/set/format/abstract_format/wrapper.rb,
card/lib/card/set/format/abstract_format/view_opts.rb,
card/lib/card/set/format/abstract_format/haml_views.rb,
card/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:

  1. 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.
  2. 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
    
  3. 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.

  4. #view and #before can both be called outside of a format block. They will be defined on the base format.

  5. 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

Instance Method Summary collapse

Class Method Details

.layout_method_name(layout) ⇒ Object

name of method for layout used by wrapper



135
136
137
# File 'card/lib/card/set/format.rb', line 135

def layout_method_name layout
  "_layout_#{layout.to_name.key}"
end

.view_method_name(view) ⇒ Object

name of method for view used by #render



147
148
149
# File 'card/lib/card/set/format.rb', line 147

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



153
154
155
# File 'card/lib/card/set/format.rb', line 153

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



141
142
143
# File 'card/lib/card/set/format.rb', line 141

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 'card/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 'card/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 'card/lib/card/set/format.rb', line 80

def view *args, &block
  format { view(*args, &block) }
end