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

Card::Set::Format is responsible for handling ‘format` blocks within the set module DSL, which is used in Set module files found in mods’ set directories. Monkeys use the DSL to define views that apply to specific sets of cards in specific formats. The views can then be used by Monkeys in code and by Sharks via the UI.

For example, imagine you have a set module file in ‘mod/mymod/type/my_type.rb`. There you can define a view like this:

format :html do
  view :hello do
    greeting
  end
end

Learn more about defining views

This view will now be available to MyType cards in HTML – but not in other formats. Similarly, you can define other methods in format blocks:

format :html do
  def greeting
    :rocks
  end
end

The magic that happens here is that the method #greeting is now applicable (and available) only to the cards in the set specified by the mod, and only when rendering a view of the card in the HTML format. Learn more about formats.

So if, for example, I had a card “MyCard” with the type “MyType”, the following should use the method above:

““ “MyCard”.card.format(:html).greeting ““

…but if the card had a different type, or if I tried to use the method in, say, the JSON format, this #beethoven method wouldn’t be available.

Under the hood, the DSL creates a ruby module that looks something like ‘Card::Set::Type::MyType::HtmlFormat`. This module will then be dynamically included in HTML format objects for MyCard.

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 #greeting 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 farewell
        "goodbye"
      end
    end
    
  3. It is possible to use super to refer to overridden methods. For example

        format :html do
          def goodbye
            "<em>#{super}</em>"
          end
        end
    
    Note: Set precedence has a higher priority than Format precedence.
    
  4. Some very powerful API calls (including view and

before) are defined in AbstractFormat. These methods are always available in format blocks.

  1. #view and #before, however, can ALSO both be called outside of a format

block. They will be defined on the base format.

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



160
161
162
# File 'lib/card/set/format.rb', line 160

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

.view_method_name(view) ⇒ Object

name of method for view used by #render



172
173
174
# File 'lib/card/set/format.rb', line 172

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



178
179
180
# File 'lib/card/set/format.rb', line 178

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



166
167
168
# File 'lib/card/set/format.rb', line 166

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



112
113
114
# File 'lib/card/set/format.rb', line 112

def before view, &block
  format { before view, &block }
end

#format(*format_names, &block) ⇒ Object

define format behavior within a set module



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/card/set/format.rb', line 91

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



106
107
108
# File 'lib/card/set/format.rb', line 106

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