Skip to content

Instantly share code, notes, and snippets.

@pablitar
Created June 6, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablitar/2f17720a78311e239ffc090a2dfcf9df to your computer and use it in GitHub Desktop.
Save pablitar/2f17720a78311e239ffc090a2dfcf9df to your computer and use it in GitHub Desktop.
module TagBuilder
def method_missing(sym, *args, &block)
properties_map = args[0]
child_tag =
create_and_initialize_tag(block,sym,properties_map)
child_tag
end
def p(*args, &block)
create_and_initialize_tag(block, :p, args[0])
end
def create_tag(sym, properties_map)
child_tag = Tag.new(sym, properties_map)
child_tag
end
def create_and_initialize_tag(block, sym, properties_map)
child_tag = create_tag(sym, properties_map)
if (!block.nil?)
child_tag.instance_eval &block
end
child_tag
end
end
class Tag
include TagBuilder
def initialize(name, properties_map)
@name = name
@children = []
@properties_map = properties_map
end
def create_tag(sym, properties_map)
a_tag = super
@children.push(a_tag)
a_tag
end
def formatted_properties_map
if(@properties_map.nil?)
""
else
" " + @properties_map.entries.map{ |anEntry|
"#{anEntry[0]}=\"#{anEntry[1]}\""
}.join(" ")
end
end
def to_xml
"<#{@name}#{formatted_properties_map}>
#{@children.map{ |aChild| aChild.to_xml}.join("\n")}
</#{@name}>"
end
end
module Ejemplo
extend TagBuilder
tag = tagnuevoysupergrosso {
span(:style => "color: blue") {
div {
p {
b {
sarlompa
}
}
}
}
span
cualquier_cosa
}
puts tag.to_xml #"<div><span><div><div></span></div>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment