Class: BBLib::HTML::Tag
- Inherits:
-
Object
- Object
- BBLib::HTML::Tag
- Includes:
- Effortless, Builder
- Defined in:
- lib/bblib/html/tag.rb
Direct Known Subclasses
Constant Summary collapse
- APPEND_ATTRIBUTES =
[:class, :style].freeze
Instance Method Summary collapse
- #add(*childs) ⇒ Object
- #append_attribute(attribute, value) ⇒ Object
- #merge(attributes) ⇒ Object
- #render(pretty: false, tabs: 0) ⇒ Object (also: #to_html)
- #render_attributes ⇒ Object
- #render_content(pretty: false, tabs: 0) ⇒ Object
- #set_attribute(attribute, value) ⇒ Object
- #to_s(*args) ⇒ Object
- #to_str ⇒ Object
Methods included from Builder
Methods included from Effortless
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (protected)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bblib/html/tag.rb', line 91 def method_missing(method, *args, &block) if context && context.respond_to?(method) context.send(method, *args, &block) elsif method != :to_ary if method.to_s.encap_by?('_') self.set_attribute(:id, method.to_s.uncapsulate('_')) else klass = method.to_s.gsub(/(?<=[^\_])\_(?=[^\_])/, '-').gsub('__', '_') self.append_attribute(:class, klass) end self._initialize(type, *args, &block) self else super end end |
Instance Method Details
#add(*childs) ⇒ Object
30 31 32 33 |
# File 'lib/bblib/html/tag.rb', line 30 def add(*childs) [childs].flatten.each { |child| children.push(child) } nil end |
#append_attribute(attribute, value) ⇒ Object
47 48 49 |
# File 'lib/bblib/html/tag.rb', line 47 def append_attribute(attribute, value) attributes[attribute] = [attributes[attribute], value.to_s].compact.join(' ') end |
#merge(attributes) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bblib/html/tag.rb', line 70 def merge(attributes) raise ArgumentError, "Expected a Hash, got a #{attributes.class}" unless attributes.is_a?(Hash) attributes.each do |k, v| if APPEND_ATTRIBUTES.include?(k.to_sym) append_attribute(k, v) else set_attribute(k, v) end end self end |
#render(pretty: false, tabs: 0) ⇒ Object Also known as: to_html
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/bblib/html/tag.rb', line 17 def render(pretty: false, tabs: 0) cont = render_content(pretty: pretty, tabs: tabs) tabbing = pretty ? ("\n" + ("\t" * tabs)) : '' "#{tabbing}<#{type}#{render_attributes}" + if cont || !BBLib::HTML.self_close?(type) ">#{cont}#{tabbing}</#{type}>" else "/>" end end |
#render_attributes ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/bblib/html/tag.rb', line 51 def render_attributes return nil if attributes.empty? attributes[:style] = attributes[:style].map { |k, v| "#{k}: #{v}" }.join('; ') if attributes[:style] && attributes[:style].is_a?(Hash) ' ' + attributes.map do | k, v| v = v.join(' ') if v.is_a?(Array) "#{k}=\"#{v.to_s.gsub('"', '"')}\"" end.join(' ') end |
#render_content(pretty: false, tabs: 0) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/bblib/html/tag.rb', line 60 def render_content(pretty: false, tabs: 0) return nil if (content.nil? || content.empty?) && children.empty? tabbing = pretty ? ("\n" + ("\t" * (tabs + 1))) : '' text = if content && !content.empty? "#{tabbing}#{content.gsub("\n", pretty ? tabbing : "\n")}" end html = children.map { |tag| tag.render(pretty: pretty, tabs: tabs + 1) }.join [text, html].compact.join end |
#set_attribute(attribute, value) ⇒ Object
43 44 45 |
# File 'lib/bblib/html/tag.rb', line 43 def set_attribute(attribute, value) attributes[attribute] = value end |
#to_s(*args) ⇒ Object
35 36 37 |
# File 'lib/bblib/html/tag.rb', line 35 def to_s(*args) render(*args) end |
#to_str ⇒ Object
39 40 41 |
# File 'lib/bblib/html/tag.rb', line 39 def to_str to_s end |