Module: Forme
- Included in:
- Sequel::Plugins::Forme::SequelInput
- Defined in:
- lib/forme.rb,
lib/forme/sinatra.rb,
lib/forme/version.rb,
lib/forme/rails.rb
Defined Under Namespace
Modules: Rails, Raw, Sinatra Classes: Error, ErrorHandler, Form, Formatter, Input, InputsWrapper, Labeler, Serializer, Tag
Constant Summary collapse
- TRANSFORMER_TYPES =
Array of all supported transformer types.
[:formatter, :serializer, :wrapper, :error_handler, :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. '0.10.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.
-
.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).
18 19 20 |
# File 'lib/forme.rb', line 18 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).
15 16 17 |
# File 'lib/forme.rb', line 15 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
.
71 72 73 |
# File 'lib/forme.rb', line 71 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.
66 67 68 |
# File 'lib/forme.rb', line 66 def self.form(*a, &block) Form.form(*a, &block) end |
.merge_classes(*classes) ⇒ Object
Return a string that includes all given class strings
76 77 78 |
# File 'lib/forme.rb', line 76 def self.merge_classes(*classes) classes.compact.join(' ') 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.
59 60 61 |
# File 'lib/forme.rb', line 59 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.
51 52 53 54 55 |
# File 'lib/forme.rb', line 51 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/forme.rb', line 82 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 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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/forme.rb', line 105 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{|k| k.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 |