Skip to content

Instantly share code, notes, and snippets.

@npras
Created June 16, 2016 04:20
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 npras/cea4883ee54cdb48f3fccfa8216ad6fb to your computer and use it in GitHub Desktop.
Save npras/cea4883ee54cdb48f3fccfa8216ad6fb to your computer and use it in GitHub Desktop.
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