Module Hirb::DynamicView

  1. lib/hirb/dynamic_view.rb

This module extends a Helper with the ability to have dynamic views for configured output classes. After a Helper has extended this module, it can use it within a render() by calling dynamic_options() to get dynamically generated options for the object it’s rendering. See Hirb::Helpers::AutoTable as an example.

Dynamic Views

Whereas normal views are generated from helpers with static helper options, dynamic views are generated from helpers and dynamically generated helper options. Let’s look at an example for Rails’ ActiveRecord classes:

Hirb.add_dynamic_view("ActiveRecord::Base", :helper=>:auto_table) {|obj|
 {:fields=>obj.class.column_names} }

From this dynamic view definition, any ActiveRecord model class will render a table with the correct fields, since the fields are extracted from the output object’s class at runtime. Note that dynamic view definitions should return a hash of helper options.

To define multiple dynamic views, create a Views module where each method ending in ‘_view’ maps to a class/module:

module Hirb::Views::ORM
  def data_mapper__resource_view(obj)
    {:fields=> {|e| }}

  def sequel__model_view(obj)

Hirb.add_dynamic_view Hirb::Views::ORM, :helper=>:auto_table

In this example, ‘data_mapper__resource_view’ maps to DataMapper::Resource and ‘sequel__model_view’ maps to Sequel::Model. Note that when mapping method names to class names, ‘__’ maps to ’::’ and ‘_’ signals the next letter to be capitalized.


public class

  1. add

public instance

  1. dynamic_options
  2. view_methods

Public class methods

add (view, options, &block)

Add dynamic views to output class(es) for a given helper. If defining one view, the first argument is the output class and a block defines the dynamic view. If defining multiple views, the first argument should be a Views::* module where each method in the module ending in _view defines a view for an output class. To map output classes to method names in a Views module, translate’::’ to ‘__’ and a capital letter translates to a ‘_’ and a lowercase letter.


Required option. Helper class that view(s) use to format. Hirb::Helpers::AutoTable is the only valid helper among default helpers. Can be given in aliased form i.e. :auto_table -> Hirb::Helpers::AutoTable.


Hirb.add_dynamic_view Hirb::Views::ORM, :helper=>:auto_table
Hirb.add_dynamic_view("ActiveRecord::Base", :helper=>:auto_table) {|obj| {:fields=>obj.class.column_names} }
[show source]
# File lib/hirb/dynamic_view.rb, line 44
    def self.add(view, options, &block)
      raise ArgumentError, ":helper option is required" unless options[:helper]
      helper = Helpers.helper_class options[:helper]
      unless helper.is_a?(Module) && class << helper; self.ancestors; end.include?(self)
        raise ArgumentError, ":helper option must be a helper that has extended DynamicView"
      mod = block ? generate_single_view_module(view, &block) : view
      raise ArgumentError, "'#{mod}' must be a module" unless mod.is_a?(Module)
      helper.add_module mod

Public instance methods

dynamic_options (obj)

Returns a hash of options based on dynamic views defined for the object’s ancestry. If no config is found returns nil.

[show source]
# File lib/hirb/dynamic_view.rb, line 69
    def dynamic_options(obj)
      view_methods.each do |meth|
        if {|e| e.to_s }.include?(method_to_class(meth))
            return send(meth, obj)
            raise "View failed to generate for '#{method_to_class(meth)}' "+
              "while in '#{meth}' with error:\n#{$!.message}"
view_methods ()

Stores view methods that a Helper has been given via DynamicView.add

[show source]
# File lib/hirb/dynamic_view.rb, line 109
    def view_methods
      @view_methods ||= []