Class: FunHtml::Attribute
- Inherits:
-
Object
- Object
- FunHtml::Attribute
- Includes:
- SpecAttributes
- Defined in:
- lib/fun_html/attribute.rb
Overview
Attribute generates HTML attributes. It is designed to prevent printing the same attribute twice. The Template elements expect an Attribute object.
All attribute values are HTML escaped via ERB::Escape.
It is typed to only allow the correct types to be passed to the attribute, if Sorbet is enabled.
Attributes can be generated via a block, or chained.
Block
Attribute.new { _1.id "one" ; _1.klass "big" }
Chained
Attribute.new.id("one").klass("big")
Boolean attributes Some attributes (async, autoplay, disabled) are boolean. When true is passed, the attribute prints, when false, the attribute will be blank.
Attribute.new.disabled(true) -> “ disabled” Attribute.new.disabled(false) -> “ ”
Class Method Summary collapse
-
.to_html(attr) ⇒ Object
only allow nil or objects that respond to ‘safe_attribute`.
Instance Method Summary collapse
-
#classes(list) ⇒ Object
classes takes a hash of class names and boolean values only the names with a true value will be printed classes(=> true, ‘b’ => false, ‘c’ => true) -> ‘ class=“a c”’.
-
#data(suffix, value) ⇒ Object
Custom data attributes The data attribute takes a suffix and the string value.
-
#initialize(buffer = {}) {|_self| ... } ⇒ Attribute
constructor
create a new Attribute object to create reuseable attributes.
-
#klass(value) ⇒ Object
CSS class name(s) for styling, the name changed to protect the Ruby.
-
#klasses(list) ⇒ Object
alias so klass has classes.
-
#merge(other) ⇒ Object
Merge another Attribute to create a new, combined, Attribute.
-
#safe_attribute ⇒ Object
output the attributes as string.
Methods included from SpecAttributes
#accept, #accept_charset, #accesskey, #action, #align, #alt, #async, #autocomplete, #autofocus, #autoplay, #bgcolor, #border, #charset, #checked, #cols, #colspan, #content, #contenteditable, #controls, #coords, #datetime, #default, #defer, #dir, #disabled, #download, #draggable, #enctype, #for, #form, #formaction, #headers, #height, #hidden, #high, #href, #hreflang, #id, #integrity, #ismap, #kind, #label, #lang, #list, #loop, #low, #max, #maxlength, #media, #method, #min, #multiple, #muted, #name, #novalidate, #onabort, #onauxclick, #onbeforeinput, #onbeforematch, #onbeforetoggle, #oncancel, #oncanplay, #oncanplaythrough, #onchange, #onclick, #onclose, #oncontextlost, #oncontextmenu, #oncontextrestored, #oncopy, #oncuechange, #oncut, #ondblclick, #ondrag, #ondragend, #ondragenter, #ondragleave, #ondragover, #ondragstart, #ondrop, #ondurationchange, #onemptied, #onended, #onformdata, #oninput, #oninvalid, #onkeydown, #onkeypress, #onkeyup, #onloadeddata, #onloadedmetadata, #onloadstart, #onmousedown, #onmouseenter, #onmouseleave, #onmousemove, #onmouseout, #onmouseover, #onmouseup, #onpaste, #onpause, #onplay, #onplaying, #onprogress, #onratechange, #onreset, #onscrollend, #onsecuritypolicyviolation, #onseeked, #onseeking, #onselect, #onslotchange, #onstalled, #onsubmit, #onsuspend, #ontimeupdate, #ontoggle, #onvolumechange, #onwaiting, #onwheel, #open, #optimum, #pattern, #placeholder, #poster, #preload, #readonly, #rel, #required, #reversed, #rows, #rowspan, #sandbox, #scope, #selected, #shape, #size, #sizes, #span, #spellcheck, #src, #srcdoc, #srclang, #srcset, #start, #step, #style, #tabindex, #target, #title, #translate, #type, #usemap, #value, #width, #wrap
Constructor Details
#initialize(buffer = {}) {|_self| ... } ⇒ Attribute
create a new Attribute object to create reuseable attributes
39 40 41 42 43 44 |
# File 'lib/fun_html/attribute.rb', line 39 def initialize(buffer = {}, &block) @__buffer = buffer return unless block yield self end |
Class Method Details
.to_html(attr) ⇒ Object
only allow nil or objects that respond to ‘safe_attribute`
34 35 36 |
# File 'lib/fun_html/attribute.rb', line 34 def self.to_html(attr) attr&.safe_attribute.to_s end |
Instance Method Details
#classes(list) ⇒ Object
classes takes a hash of class names and boolean values only the names with a true value will be printed classes(=> true, ‘b’ => false, ‘c’ => true) -> ‘ class=“a c”’
66 67 68 |
# File 'lib/fun_html/attribute.rb', line 66 def classes(list) klass list.select { |_k, v| v == true }.keys.join(' ') end |
#data(suffix, value) ⇒ Object
Custom data attributes The data attribute takes a suffix and the string value. Attribute.new.data(‘turbo’,‘false’) -> ‘ data-turbo=“false”’
49 50 51 52 53 54 55 56 |
# File 'lib/fun_html/attribute.rb', line 49 def data(suffix, value) unless suffix.match?(/\A[a-z-]+\z/) raise ArgumentError, "suffix (#{suffix}) must be lowercase and only contain 'a' to 'z' or hyphens." end write(" data-#{suffix}=\"", value) end |
#klass(value) ⇒ Object
CSS class name(s) for styling, the name changed to protect the Ruby.
59 60 61 |
# File 'lib/fun_html/attribute.rb', line 59 def klass(value) write(' class="', value) end |
#klasses(list) ⇒ Object
alias so klass has classes
71 |
# File 'lib/fun_html/attribute.rb', line 71 def klasses(list) = classes(list) |
#merge(other) ⇒ Object
Merge another Attribute to create a new, combined, Attribute.
74 75 76 |
# File 'lib/fun_html/attribute.rb', line 74 def merge(other) self.class.new(@__buffer.merge(other.instance_variable_get(:@__buffer))) end |
#safe_attribute ⇒ Object
output the attributes as string
79 80 81 |
# File 'lib/fun_html/attribute.rb', line 79 def safe_attribute @__buffer.values.join end |