Module: Forme
- Included in:
- Sequel::Plugins::Forme::SequelInput
- Defined in:
- lib/forme.rb,
lib/forme/erb.rb,
lib/forme/raw.rb,
lib/forme/tag.rb,
lib/forme/form.rb,
lib/forme/input.rb,
lib/forme/sinatra.rb,
lib/forme/version.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, Sinatra 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]
- 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
Form
and 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
. {}
- VERSION =
Version constant, use
Forme.version
instead. '1.3.0'.freeze
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
:class
entry in theattr
hash with the givenclasses
. -
.form(*a, &block) ⇒ Object
Call
Forme::Form.form
with 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
args
andblock
. -
.transformer(type, trans, default_opts) ⇒ 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).
17 18 19 |
# File 'lib/forme.rb', line 17 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).
14 15 16 |
# File 'lib/forme.rb', line 14 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
.
70 71 72 |
# File 'lib/forme.rb', line 70 def self.attr_classes(attr, *classes) attr[:class] = merge_classes(attr[:class], *classes) end |
.form(*a, &block) ⇒ Object
Call Forme::Form.form
with the given arguments and block.
65 66 67 |
# File 'lib/forme.rb', line 65 def self.form(*a, &block) Form.form(*a, &block) end |
.merge_classes(*classes) ⇒ Object
Return a string that includes all given class strings
75 76 77 |
# File 'lib/forme.rb', line 75 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.
81 82 83 |
# File 'lib/forme.rb', line 81 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.
58 59 60 |
# File 'lib/forme.rb', line 58 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
obj
orblock
, but not both. Ifobj
is given, should be either aClass
instance or it should respond tocall
. If aClass
instance is given, instances of that class should respond tocall
, and a new instance of that class should be used for each transformation.
50 51 52 53 54 |
# File 'lib/forme.rb', line 50 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.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/forme.rb', line 87 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 args.first else raise Error, "No matching #{type}: #{trans_name.inspect}" end end end |
.transformer(type, trans, default_opts) ⇒ 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
TRANSFORRMERS
hash. Hash
-
If
type
is also a key intrans
, return the related value fromtrans
, unless the related value isnil
, in which case, returnnil
. Iftype
is not a key intrans
, use the default transformer for the receiver. nil
-
Assume the default transformer for this receiver.
- otherwise
-
return
trans
directly if it responds tocall
, and raise anError
if not.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/forme.rb', line 110 def self.transformer(type, trans, default_opts) case trans when Symbol 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], nil) 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’)
6 7 8 |
# File 'lib/forme/version.rb', line 6 def self.version VERSION end |