Module: Forme
- Included in:
- Sequel::Plugins::Forme::SequelInput
- Defined in:
- lib/forme.rb,
lib/forme/bs3.rb,
lib/forme/bs5.rb,
lib/forme/erb.rb,
lib/forme/raw.rb,
lib/forme/tag.rb,
lib/forme/form.rb,
lib/forme/input.rb,
lib/forme/version.rb,
lib/forme/template.rb,
lib/forme/transformers/helper.rb,
lib/forme/transformers/labeler.rb,
lib/forme/transformers/wrapper.rb,
lib/forme/transformers/formatter.rb,
lib/forme/transformers/serializer.rb,
lib/forme/transformers/error_handler.rb,
lib/forme/transformers/inputs_wrapper.rb,
lib/forme/rails.rb
Defined Under Namespace
Modules: ERB, Rails, Raw, Template Classes: Error, ErrorHandler, Form, Formatter, Helper, Input, InputsWrapper, Labeler, RawString, Serializer, Tag, Wrapper
Constant Summary collapse
- TRANSFORMER_TYPES =
Array of all supported transformer types.
[:formatter, :serializer, :wrapper, :error_handler, :helper, :labeler, :inputs_wrapper, :tag_wrapper, :set_wrapper]
- SHARED_WRAPPERS =
Transformer symbols shared by wrapper and inputs_wrapper
[:tr, :table, :ol, :fieldset_ol]
- CONFIGURATIONS =
Hash storing all configurations. Configurations are groups of related transformers, so that you can specify a single :config option when creating a
Formand have all of the transformers set from that. {:default=>{}}
- 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. {}
- MAJOR =
The major version of Forme, updated only for major changes that are likely to require modification to apps using Forme.
2- MINOR =
The minor version of Forme, updated for new feature releases of Forme.
4- TINY =
The patch version of Forme, updated only for bug fixes from the last feature release.
0- VERSION =
Version constant, use
Forme.versioninstead. "#{MAJOR}.#{MINOR}.#{TINY}".freeze
- VERSION_NUMBER =
The full version of Forme as a number (1.8.0 => 10800)
MAJOR*10000 + MINOR*100 + TINY
Class Attribute Summary collapse
-
.default_add_blank_prompt ⇒ Object
The default prompt to use for the :add_blank option (default: nil).
-
.default_config ⇒ Object
Set the default configuration to use if none is explicitly specified (default: :default).
Class Method Summary collapse
-
.attr_classes(attr, *classes) ⇒ Object
Update the
:classentry in theattrhash with the givenclasses, adding the classes after any existing classes. -
.attr_classes_after(attr, *classes) ⇒ Object
Update the
:classentry in theattrhash with the givenclasses, adding the classes before any existing classes. -
.form(*a, &block) ⇒ Object
Call
Forme::Form.formwith the given arguments and block. -
.merge_classes(*classes) ⇒ Object
Return a string that includes all given class strings.
-
.raw(s) ⇒ Object
Create a RawString using the given string, which will disable automatic escaping for that string.
-
.register_config(type, hash) ⇒ Object
Register a new configuration.
-
.register_transformer(type, sym, obj = nil, &block) ⇒ Object
Register a new transformer with this library.
-
.transform(type, trans_name, default_opts, *args, &block) ⇒ Object
If there is a related transformer, call it with the given
argsandblock. -
.transformer(type, trans, default_opts = nil) ⇒ Object
Get the related transformer for the given transformer type.
-
.version ⇒ Object
Returns the version as a frozen string (e.g. ‘0.1.0’).
Class Attribute Details
.default_add_blank_prompt ⇒ Object
The default prompt to use for the :add_blank option (default: nil).
51 52 53 |
# File 'lib/forme.rb', line 51 def default_add_blank_prompt @default_add_blank_prompt end |
.default_config ⇒ Object
Set the default configuration to use if none is explicitly specified (default: :default).
48 49 50 |
# File 'lib/forme.rb', line 48 def default_config @default_config end |
Class Method Details
.attr_classes(attr, *classes) ⇒ Object
Update the :class entry in the attr hash with the given classes, adding the classes after any existing classes.
106 107 108 |
# File 'lib/forme.rb', line 106 def self.attr_classes(attr, *classes) attr[:class] = merge_classes(attr[:class], *classes) end |
.attr_classes_after(attr, *classes) ⇒ Object
Update the :class entry in the attr hash with the given classes, adding the classes before any existing classes.
8 9 10 |
# File 'lib/forme/bs5.rb', line 8 def self.attr_classes_after(attr, *classes) attr[:class] = merge_classes(*classes, attr[:class]) end |
.form(*a, &block) ⇒ Object
Call Forme::Form.form with the given arguments and block.
100 101 102 |
# File 'lib/forme.rb', line 100 def self.form(*a, &block) Form.form(*a, &block) end |
.merge_classes(*classes) ⇒ Object
Return a string that includes all given class strings
111 112 113 |
# File 'lib/forme.rb', line 111 def self.merge_classes(*classes) classes.compact.join(' ') end |
.raw(s) ⇒ Object
Create a RawString using the given string, which will disable automatic escaping for that string.
117 118 119 |
# File 'lib/forme.rb', line 117 def self.raw(s) RawString.new(s) end |
.register_config(type, hash) ⇒ Object
Register a new configuration. Type is the configuration name symbol, and hash maps transformer type symbols to transformer name symbols.
93 94 95 |
# File 'lib/forme.rb', line 93 def self.register_config(type, hash) CONFIGURATIONS[type] = CONFIGURATIONS[hash.fetch(:base, :default)].merge(hash) end |
.register_transformer(type, sym, obj = nil, &block) ⇒ Object
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
objorblock, but not both. Ifobjis given, should be either aClassinstance or it should respond tocall. If aClassinstance is given, instances of that class should respond tocall, and a new instance of that class should be used for each transformation.
85 86 87 88 89 |
# File 'lib/forme.rb', line 85 def self.register_transformer(type, sym, obj=nil, &block) raise Error, "Not a valid transformer type" unless TRANSFORMERS.has_key?(type) raise Error, "Must provide either block or obj, not both" if obj && block TRANSFORMERS[type][sym] = obj||block end |
.transform(type, trans_name, default_opts, *args, &block) ⇒ Object
If there is a related transformer, call it with the given args and block. Otherwise, attempt to return the initial input without modifying it.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/forme.rb', line 123 def self.transform(type, trans_name, default_opts, *args, &block) if trans = transformer(type, trans_name, default_opts) trans.call(*args, &block) else case type when :inputs_wrapper yield when :labeler, :error_handler, :wrapper, :helper, :set_wrapper, :tag_wrapper args.first else raise Error, "No matching #{type}: #{trans_name.inspect}" end end end |
.transformer(type, trans, default_opts = nil) ⇒ Object
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
TRANSFORRMERShash. Hash-
If
typeis also a key intrans, return the related value fromtrans, unless the related value isnil, in which case, returnnil. Iftypeis not a key intrans, use the default transformer for the receiver. nil-
Assume the default transformer for this receiver.
- otherwise
-
return
transdirectly if it responds tocall, and raise anErrorif not.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/forme.rb', line 146 def self.transformer(type, trans, default_opts=nil) case trans when Symbol type = :wrapper if type == :set_wrapper || type == :tag_wrapper TRANSFORMERS[type][trans] || raise(Error, "invalid #{type}: #{trans.inspect} (valid #{type}s: #{TRANSFORMERS[type].keys.map(&:inspect).join(', ')})") when Hash if trans.has_key?(type) if v = trans[type] transformer(type, v, default_opts) end else transformer(type, nil, default_opts) end when nil transformer(type, default_opts[type]) if default_opts else if trans.respond_to?(:call) trans else raise Error, "#{type} #{trans.inspect} must respond to #call" end end end |
.version ⇒ Object
Returns the version as a frozen string (e.g. ‘0.1.0’)
22 23 24 |
# File 'lib/forme/version.rb', line 22 def self.version VERSION end |