Created
June 6, 2018 13:34
-
-
Save pablitar/2f17720a78311e239ffc090a2dfcf9df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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