Class: Forma::Html::Element
- Inherits:
-
Object
- Object
- Forma::Html::Element
- Defined in:
- lib/forma/html.rb
Overview
Element class.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #attrs_by_name(name) ⇒ Object
-
#initialize(tag, h) ⇒ Element
constructor
A new instance of Element.
- #klass ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(tag, h) ⇒ Element
Returns a new instance of Element.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/forma/html.rb', line 105 def initialize(tag, h) @tag = tag.to_s if h[:text]; @text = h[:text] elsif h[:html]; @text = h[:html].html_safe end @attrs = h[:attrs] || [] @children = [] h[:children].each { |c| @children << c } if h[:children] ids = @attrs.select { |x| x.is_a?(SimpleAttr) and x.name == "id" }.map { |x| x.value } @id = ids[0] if ids.length > 0 @classes = @attrs.select { |x| x.is_a?(ClassAttr) }.map{ |x| x.values }.flatten end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
102 103 104 |
# File 'lib/forma/html.rb', line 102 def attrs @attrs end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
102 103 104 |
# File 'lib/forma/html.rb', line 102 def id @id end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
102 103 104 |
# File 'lib/forma/html.rb', line 102 def tag @tag end |
#text ⇒ Object
Returns the value of attribute text.
103 104 105 |
# File 'lib/forma/html.rb', line 103 def text @text end |
Instance Method Details
#attrs_by_name(name) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/forma/html.rb', line 126 def attrs_by_name(name) if name.to_s == 'class' then attrs.select { |x| x.is_a?(ClassAttr) } elsif name.to_s == 'style' then attrs.select { |x| x.is_a?(SimpleAttr) } else attrs.select { |x| (x.respond_to?(:name) and x.name == name.to_s) } end end |
#klass ⇒ Object
118 119 120 |
# File 'lib/forma/html.rb', line 118 def klass @classes end |
#to_s ⇒ Object
122 123 124 |
# File 'lib/forma/html.rb', line 122 def to_s generate_html.html_safe end |