Class: Formular::Element

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Defined in:
lib/formular/element.rb,
lib/formular/elements.rb,
lib/formular/element/module.rb,
lib/formular/element/bootstrap3.rb,
lib/formular/element/bootstrap4.rb,
lib/formular/element/foundation6.rb,
lib/formular/element/modules/hint.rb,
lib/formular/element/modules/error.rb,
lib/formular/element/modules/label.rb,
lib/formular/element/modules/control.rb,
lib/formular/element/modules/wrapped.rb,
lib/formular/element/modules/checkable.rb,
lib/formular/element/modules/container.rb,
lib/formular/element/modules/collection.rb,
lib/formular/element/foundation6/wrapped.rb,
lib/formular/element/modules/escape_value.rb,
lib/formular/element/bootstrap3/horizontal.rb,
lib/formular/element/bootstrap4/horizontal.rb,
lib/formular/element/bootstrap3/input_group.rb,
lib/formular/element/bootstrap4/input_group.rb,
lib/formular/element/foundation6/input_group.rb,
lib/formular/element/bootstrap3/column_control.rb,
lib/formular/element/bootstrap4/custom_control.rb,
lib/formular/element/bootstrap3/checkable_control.rb,
lib/formular/element/bootstrap4/checkable_control.rb,
lib/formular/element/foundation6/checkable_control.rb

Overview

The Element class is responsible for defining what the html should look like. This includes default attributes, and the function to use to render the html actual rendering is done via a HtmlBlock class

Direct Known Subclasses

ErrorNotification, Submit

Defined Under Namespace

Modules: Bootstrap3, Bootstrap4, Foundation6, Module, Modules Classes: Button, Checkbox, Error, ErrorNotification, Form, Hidden, Input, Label, OptGroup, Option, Radio, Select, Submit, Textarea

Constant Summary collapse

Container =

These three are really just provided for convenience when creating other elements

Class.new(Formular::Element) { include Formular::Element::Modules::Container }
Control =
Class.new(Formular::Element) { include Formular::Element::Modules::Control }
Wrapped =
Class.new(Formular::Element) { include Formular::Element::Modules::Wrapped }
Fieldset =

define some base classes to build from or easily use elsewhere

Class.new(Container) { tag :fieldset }
Legend =
Class.new(Container) { tag :legend }
Div =
Class.new(Container) { tag :div }
P =
Class.new(Container) { tag :p }
Span =
Class.new(Container) { tag :span }
Small =
Class.new(Container) { tag :small }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options, &block) ⇒ Element

Returns a new instance of Element.



77
78
79
80
81
82
83
84
85
# File 'lib/formular/element.rb', line 77

def initialize(**options, &block)
  @builder = options.delete(:builder)
  @options = options
  normalize_options
  process_options
  @block = block
  @tag = self.class.tag_name
  @html_blocks = define_html_blocks
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#html_blocksObject (readonly)

Returns the value of attribute html_blocks.



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

def html_blocks
  @html_blocks
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

Class Method Details

.add_option_keys(*keys) ⇒ Object

blacklist the keys that should NOT end up as html attributes



58
59
60
# File 'lib/formular/element.rb', line 58

def self.add_option_keys(*keys)
  self.option_keys += keys
end

.call(**options, &block) ⇒ Object



73
74
75
# File 'lib/formular/element.rb', line 73

def self.call(**options, &block)
  new(**options, &block)
end

.html(context = :default, &block) ⇒ Object

define what your html should look like this block is executed in the context of an HtmlBlock instance



46
47
48
# File 'lib/formular/element.rb', line 46

def self.html(context = :default, &block)
  self.html_blocks[context] = block
end

.process_option(key, processor, condition = {}) ⇒ Object

process an option value (i.e. escape html) This occurs after the value has been set (either by default or by user input) you can make this conditional by providing a condition e.g. if: :some_method or unless: :some_method



40
41
42
# File 'lib/formular/element.rb', line 40

def self.process_option(key, processor, condition = {})
  self.processing_hash[key] = { processor: processor, condition: condition }
end

.rename_html_context(old_context, new_context) ⇒ Object

a convenient way of changing the key for a context useful for inheritance if you want to replace a context but still access the original function



53
54
55
# File 'lib/formular/element.rb', line 53

def self.rename_html_context(old_context, new_context)
  self.html_blocks[new_context] = self.html_blocks.delete(old_context)
end

.set_default(key, value, condition = {}) ⇒ Object

set the default value of an option or attribute you can make this conditional by providing a condition e.g. if: :some_method or unless: :some_method to respect the order defaults are declared, rather than overriting existing defaults we should delete the existing and create a new k/v pair



31
32
33
34
# File 'lib/formular/element.rb', line 31

def self.set_default(key, value, condition = {})
  self.default_hash.delete(key) # attempt to delete an existing key
  self.default_hash[key] = { value: value, condition: condition }
end

.tag(name) ⇒ Object

define the name of the html tag for the element e.g. tag :span tag ‘input’ Note that if you leave this out, the tag will be inferred based on the name of your class Also, this is not inherited



69
70
71
# File 'lib/formular/element.rb', line 69

def self.tag(name)
  self.tag_name = name
end

Instance Method Details

#attributesObject



88
89
90
91
# File 'lib/formular/element.rb', line 88

def attributes
  attrs = @options.select { |k, v| @options[k] || true unless option_key?(k) }
  Attributes[attrs]
end

#to_html(context: nil) ⇒ Object Also known as: to_s



93
94
95
96
# File 'lib/formular/element.rb', line 93

def to_html(context: nil)
  context ||= self.class.html_context
  html_blocks[context].call
end