module Forme

Constants

CGI
CONFIGURATIONS

Hash storing all configurations. Configurations are groups of related transformers, so that you can specify a single :config option when creating a Form and have all of the transformers set from that.

ESCAPE_TABLE
MAJOR

The major version of Forme, updated only for major changes that are likely to require modification to apps using Forme.

MINOR

The minor version of Forme, updated for new feature releases of Forme.

SHARED_WRAPPERS

Transformer symbols shared by wrapper and inputs_wrapper

TINY

The patch version of Forme, updated only for bug fixes from the last feature release.

TRANSFORMERS

Main hash storing the registered transformers. Maps transformer type symbols to subhashes containing the registered transformers for that type. Those subhashes should have symbol keys and values that are either classes or objects that respond to call.

TRANSFORMER_TYPES

Array of all supported transformer types.

VERSION

Version constant, use Forme.version instead.

VERSION_NUMBER

The full version of Forme as a number (1.8.0 => 10800)

Attributes

default_add_blank_prompt[RW]

The default prompt to use for the :add_blank option (default: nil).

default_config[RW]

Set the default configuration to use if none is explicitly specified (default: :default).

Public Class Methods

attr_classes(attr, *classes) click to toggle source

Update the :class entry in the attr hash with the given classes, adding the classes after any existing classes.

    # File lib/forme.rb
100 def self.attr_classes(attr, *classes)
101   attr[:class] = merge_classes(attr[:class], *classes)
102 end
attr_classes_after(attr, *classes) click to toggle source

Update the :class entry in the attr hash with the given classes, adding the classes before any existing classes.

   # File lib/forme/bs5.rb
 8 def self.attr_classes_after(attr, *classes)
 9   attr[:class] = merge_classes(*classes, attr[:class])
10 end
form(*a, &block) click to toggle source

Call Forme::Form.form with the given arguments and block.

   # File lib/forme.rb
94 def self.form(*a, &block)
95   Form.form(*a, &block)
96 end
h(value) click to toggle source
   # File lib/forme.rb
22 def self.h(value)
23   CGI.escapeHTML(value.to_s)
24 end
merge_classes(*classes) click to toggle source

Return a string that includes all given class strings

    # File lib/forme.rb
105 def self.merge_classes(*classes)
106   classes.compact.join(' ')
107 end
raw(s) click to toggle source

Create a RawString using the given string, which will disable automatic escaping for that string.

    # File lib/forme.rb
111 def self.raw(s)
112   RawString.new(s)
113 end
register_config(type, hash) click to toggle source

Register a new configuration. Type is the configuration name symbol, and hash maps transformer type symbols to transformer name symbols.

   # File lib/forme.rb
87 def self.register_config(type, hash)
88   CONFIGURATIONS[type] = CONFIGURATIONS[hash.fetch(:base, :default)].merge(hash)
89 end
register_transformer(type, sym, obj=nil, &block) click to toggle source

Register a new transformer with this library. Arguments:

type

Transformer type symbol

sym

Transformer name symbol

obj/block

Transformer to associate with this symbol. Should provide either obj or block, but not both. If obj is given, should be either a Class instance or it should respond to call. If a Class instance is given, instances of that class should respond to call, and a new instance of that class should be used for each transformation.

   # File lib/forme.rb
79 def self.register_transformer(type, sym, obj=nil, &block)
80   raise Error, "Not a valid transformer type" unless TRANSFORMERS.has_key?(type)
81   raise Error, "Must provide either block or obj, not both" if obj && block
82   TRANSFORMERS[type][sym] = obj||block
83 end
transform(type, trans_name, default_opts, *args) { || ... } click to toggle source

If there is a related transformer, call it with the given args and block. Otherwise, attempt to return the initial input without modifying it.

    # File lib/forme.rb
117 def self.transform(type, trans_name, default_opts, *args, &block)
118   if trans = transformer(type, trans_name, default_opts)
119     trans.call(*args, &block)
120   else
121     case type
122     when :inputs_wrapper
123       yield
124     when :labeler, :error_handler, :wrapper, :helper, :set_wrapper, :tag_wrapper
125       args.first
126     else
127       raise Error, "No matching #{type}: #{trans_name.inspect}"
128     end
129   end
130 end
transformer(type, trans, default_opts=nil) click to toggle source

Get the related transformer for the given transformer type. Output depends on the type of trans:

Symbol

Assume a request for a registered transformer, so look it up in the TRANSFORRMERS hash.

Hash

If type is also a key in trans, return the related value from trans, unless the related value is nil, in which case, return nil. If type is not a key in trans, use the default transformer for the receiver.

nil

Assume the default transformer for this receiver.

otherwise

return trans directly if it responds to call, and raise an Error if not.

    # File lib/forme.rb
140 def self.transformer(type, trans, default_opts=nil)
141   case trans
142   when Symbol
143     type = :wrapper if type == :set_wrapper || type == :tag_wrapper
144     TRANSFORMERS[type][trans] || raise(Error, "invalid #{type}: #{trans.inspect} (valid #{type}s: #{TRANSFORMERS[type].keys.map(&:inspect).join(', ')})")
145   when Hash
146     if trans.has_key?(type)
147       if v = trans[type]
148         transformer(type, v, default_opts)
149       end
150     else
151       transformer(type, nil, default_opts)
152     end
153   when nil
154     transformer(type, default_opts[type]) if default_opts
155   else
156     if trans.respond_to?(:call)
157       trans
158     else
159       raise Error, "#{type} #{trans.inspect} must respond to #call"
160     end
161   end
162 end
version() click to toggle source

Returns the version as a frozen string (e.g. ‘0.1.0’)

   # File lib/forme/version.rb
22 def self.version
23   VERSION
24 end