Module: Forme

Included in:
Sequel::Plugins::Forme::SequelInput
Defined in:
lib/forme.rb,
lib/forme/bs3.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/erb_form.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, :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 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.

{}
MAJOR =

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

1
MINOR =

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

9
TINY =

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

0
VERSION =

Version constant, use Forme.version instead.

"#{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

Class Method Summary collapse

Class Attribute Details

.default_add_blank_promptObject

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



44
45
46
# File 'lib/forme.rb', line 44

def default_add_blank_prompt
  @default_add_blank_prompt
end

.default_configObject

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



41
42
43
# File 'lib/forme.rb', line 41

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.



98
99
100
# File 'lib/forme.rb', line 98

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.



93
94
95
# File 'lib/forme.rb', line 93

def self.form(*a, &block)
  Form.form(*a, &block)
end

.merge_classes(*classes) ⇒ Object

Return a string that includes all given class strings



103
104
105
# File 'lib/forme.rb', line 103

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.



109
110
111
# File 'lib/forme.rb', line 109

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.



86
87
88
# File 'lib/forme.rb', line 86

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 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.

Raises:



78
79
80
81
82
# File 'lib/forme.rb', line 78

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/forme.rb', line 115

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 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.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/forme.rb', line 138

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

.versionObject

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