Class: HtmlTagBuilder
Overview
tag.ul do |n|
1.upto(3) do |num|
n.li do |n|
n.i '.arrow'
n.span num
n.id
end
end
end
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.build(attrs, node = nil, text = nil) ⇒ Object
build html node.
-
.call(class_name, &block) ⇒ Object
tag.div(class: ‘klasa’) do -> tag.(‘klasa’) do.
-
.method_missing(tag_name, *args, &block) ⇒ Object
tag.div -> tag.tag :div.
-
.tag(name = nil, opts = {}, data = nil) ⇒ Object
tag :div, { ‘class’=>‘iform’ } do.
Instance Method Summary collapse
-
#call(class_name, &block) ⇒ Object
n.div(class: ‘klasa’) do -> n.(‘klasa’) do.
-
#initialize ⇒ HtmlTagBuilder
constructor
A new instance of HtmlTagBuilder.
-
#method_missing(tag_name, *args, &block) ⇒ Object
forward to class.
-
#push(data) ⇒ Object
push data to stack.
Constructor Details
#initialize ⇒ HtmlTagBuilder
Returns a new instance of HtmlTagBuilder.
73 74 75 |
# File 'lib/common/html_tag_builder.rb', line 73 def initialize @data = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(tag_name, *args, &block) ⇒ Object
forward to class
88 89 90 |
# File 'lib/common/html_tag_builder.rb', line 88 def method_missing tag_name, *args, &block @data.push self.class.tag(tag_name, args[0], args[1], &block) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
71 72 73 |
# File 'lib/common/html_tag_builder.rb', line 71 def data @data end |
Class Method Details
.build(attrs, node = nil, text = nil) ⇒ Object
build html node
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/common/html_tag_builder.rb', line 49 def build attrs, node=nil, text=nil opts = '' attrs.each do |k,v| opts += ' '+k.to_s.gsub(/_/,'-')+'="'+v.to_s.gsub(/"/,'"')+'"' if v.present? end return opts unless node text = yield opts if block_given? text ||= '' unless ['input', 'img', 'meta', 'link', 'hr', 'br'].include?(node.to_s) text ? %{<#{node}#{opts}>#{text}</#{node}>} : %{<#{node}#{opts} />} end |
.call(class_name, &block) ⇒ Object
tag.div(class: ‘klasa’) do -> tag.(‘klasa’) do
63 64 65 |
# File 'lib/common/html_tag_builder.rb', line 63 def call class_name, &block tag(:div, { class: class_name }, &block) end |
.method_missing(tag_name, *args, &block) ⇒ Object
tag.div -> tag.tag :div
14 15 16 |
# File 'lib/common/html_tag_builder.rb', line 14 def method_missing tag_name, *args, &block tag tag_name, args[0], args[1], &block end |
.tag(name = nil, opts = {}, data = nil) ⇒ Object
tag :div, { ‘class’=>‘iform’ } do
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/common/html_tag_builder.rb', line 19 def tag name=nil, opts={}, data=nil # covert tag.a '.foo.bar' to class names # covert tag.a '#id' to id names if opts.is_a?(String) case opts[0,1] when '.' opts = { class: opts.sub('.', '').gsub('.', ' ') } when '#' opts = { id: opts.sub('#', '') } end else end # fix data and opts unless opts is Hash data, opts = opts, {} unless opts.class == Hash if block_given? inline = new data = yield(inline, opts) # if data is pushed to passed node, use that data data = inline.data if inline.data.first end data = data.join('') if data.is_a?(Array) build opts, name, data end |
Instance Method Details
#call(class_name, &block) ⇒ Object
n.div(class: ‘klasa’) do -> n.(‘klasa’) do
83 84 85 |
# File 'lib/common/html_tag_builder.rb', line 83 def call class_name, &block @data.push self.class.tag(:div, { class: class_name }, &block) end |
#push(data) ⇒ Object
push data to stack
78 79 80 |
# File 'lib/common/html_tag_builder.rb', line 78 def push data @data.push data end |