Class: Forma::Html::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/forma/html.rb

Overview

Element class.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



102
103
104
# File 'lib/forma/html.rb', line 102

def attrs
  @attrs
end

#idObject (readonly)

Returns the value of attribute id.



102
103
104
# File 'lib/forma/html.rb', line 102

def id
  @id
end

#tagObject (readonly)

Returns the value of attribute tag.



102
103
104
# File 'lib/forma/html.rb', line 102

def tag
  @tag
end

#textObject

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

#klassObject



118
119
120
# File 'lib/forma/html.rb', line 118

def klass
  @classes
end

#to_sObject



122
123
124
# File 'lib/forma/html.rb', line 122

def to_s
  generate_html.html_safe
end