Skip to content

Instantly share code, notes, and snippets.

@npras
Created Jun 16, 2016
Embed
What would you like to do?
class XMLBuilder
def initialize
@d = {}
end
def to_xml
@d
end
def tag tag_name, content
"<#{tag_name}>#{content}</#{tag_name}>"
end
def tag1 &blk
parent_st = caller.find_all do |s|
(s =~ /#{__FILE__}/) &&
(s =~ /:in `tag.*?'$/)
end
parents = parent_st.map { |s| s.match(/`(tag.*?)'$/)[1] }
@d[__callee__] = {parent: parents.first}
@d[parents.first].merge children
blk.call
end
def tag2 &blk
parent_st = caller.find_all do |s|
(s =~ /#{__FILE__}/) &&
(s =~ /:in `tag.*?'$/)
end
parents = parent_st.map { |s| s.match(/`(tag.*?)'$/)[1] }
@d[__callee__] = {parent: parents.first}
blk.call
end
def tag3 &blk
parent_st = caller.find_all do |s|
(s =~ /#{__FILE__}/) &&
(s =~ /:in `tag.*?'$/)
end
parents = parent_st.map { |s| s.match(/`(tag.*?)'$/)[1] }
@d[__callee__] = {parent: parents.first}
blk.call
end
def tag4 &blk
parent_st = caller.find_all do |s|
(s =~ /#{__FILE__}/) &&
(s =~ /:in `tag.*?'$/)
end
parents = parent_st.map { |s| s.match(/`(tag.*?)'$/)[1] }
@d[__callee__] = {parent: parents.first}
blk.call
end
end
xb = XMLBuilder.new
xb.tag1 do
xb.tag2 do
end
xb.tag3 do
xb.tag4 { 'hi hello' }
end
end
puts xb.to_xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment