Class: Bootstrap5Helper::Component
- Inherits:
-
Object
- Object
- Bootstrap5Helper::Component
- Defined in:
- lib/bootstrap5_helper/component.rb
Overview
Every component that inherits from this class, needs to call the parent initialization method! In order to properly render erb blocks within the proper context, we need the template. The only way to get this, is to pass in the template.
the ‘context` mentioned above, refers to the context of `@template` and not to be confused with `@context` that can be found in the sub classes. `@context` refers to the Bootstrap class context of the component.
This super class is meant to contain commonly used methods that all sub classes can leverage.
Direct Known Subclasses
Accordion, Accordion::Item, Alert, Badge, Callout, Card, CardWithNavTab, InputGroup, Modal, Nav, Offcanvas, Offcanvas::Content, Overlay, Overlay::Menu, PageHeader, Spinner, Tab, Tab::Content, Toast
Instance Method Summary collapse
-
#capture(*args) ⇒ String
Used to pass all context of the capture tag to then template.
-
#concat(text) ⇒ String
Used to pass all concat references to the template.
-
#config(setting, fallback) ⇒ Mixed
Used to get config settings inside of components quicker.
-
#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ String
Used to pass all context of content_tag to the template.
-
#initialize(template) ⇒ Component
constructor
Used to ensure that the helpers always have the propert context for rendering and bindings.
-
#parse_arguments(*args, default) ⇒ Array
Used to parse method arguments.
-
#parse_context_or_options(*args) ⇒ Array
Used to parse method arguments.
-
#parse_tag_or_options(*args) ⇒ Array
Used to parse method arguments.
-
#parse_text_or_options(*args) ⇒ Array
Used to parse method arguments.
-
#uuid ⇒ String
Used to generate a (hopefully) unique ID for DOM elements.
Constructor Details
#initialize(template) ⇒ Component
Used to ensure that the helpers always have the propert context for rendering and bindings.
20 21 22 |
# File 'lib/bootstrap5_helper/component.rb', line 20 def initialize(template) @template = template end |
Instance Method Details
#capture(*args) ⇒ String
Used to pass all context of the capture tag to then template. This ensures proper template binding of variables and methods!
58 59 60 |
# File 'lib/bootstrap5_helper/component.rb', line 58 def capture(*args) @template.capture(*args) end |
#concat(text) ⇒ String
Used to pass all concat references to the template. This ensures proper binding. Concat adds a String to the template Output buffer. Useful when trying to add a String with no block.
69 70 71 |
# File 'lib/bootstrap5_helper/component.rb', line 69 def concat(text) @template.concat(text) end |
#config(setting, fallback) ⇒ Mixed
Used to get config settings inside of components quicker.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/bootstrap5_helper/component.rb', line 141 def config(setting, fallback) object = Bootstrap5Helper.config value = ( case setting when Hash object.send(setting.keys[0])[setting.values[0]] if object.send(setting.keys[0]) when Symbol, String object.send(setting) if object.respond_to?(setting) end ) value || fallback end |
#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ String
Used to pass all context of content_tag to the template. This ensures proper template binding of variables and methods!
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bootstrap5_helper/component.rb', line 35 def content_tag( name, = nil, = nil, escape = true, &block ) @template.content_tag( name, , , escape, &block ) end |
#parse_arguments(*args, default) ⇒ Array
Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user skipped the default argument. So we will assign it to ‘default` provided and return the Hash to be used as options.
117 118 119 120 121 122 123 124 125 |
# File 'lib/bootstrap5_helper/component.rb', line 117 def parse_arguments(*args, default) first, second = *args case first when Hash, NilClass [default, first || second] when Symbol, String [first, second] end end |
#parse_context_or_options(*args) ⇒ Array
Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left off the bootstrap contectual class. So we will assign it to ‘secondary` and return the Hash to be used as options.
81 82 83 |
# File 'lib/bootstrap5_helper/component.rb', line 81 def (*args) parse_arguments(*args, 'secondary') end |
#parse_tag_or_options(*args) ⇒ Array
Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left off the tag element. So we will assign it to NilClass
and return the Hash to be used as options.
93 94 95 |
# File 'lib/bootstrap5_helper/component.rb', line 93 def (*args) parse_arguments(*args, nil) end |
#parse_text_or_options(*args) ⇒ Array
Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left out the text string. So we will assign it to NilClass
and return the Hash to be used as options.
105 106 107 |
# File 'lib/bootstrap5_helper/component.rb', line 105 def (*args) parse_arguments(*args, nil) end |
#uuid ⇒ String
Used to generate a (hopefully) unique ID for DOM elements. Used as a fallback if the user doesn’t specify one.
132 133 134 |
# File 'lib/bootstrap5_helper/component.rb', line 132 def uuid (0...10).map { rand(65..90).chr }.join end |