Skip to content

Instantly share code, notes, and snippets.

@Kimtaro
Created February 8, 2010 06:06
Show Gist options
  • Save Kimtaro/297928 to your computer and use it in GitHub Desktop.
Save Kimtaro/297928 to your computer and use it in GitHub Desktop.
module ToHTML
def self.included(base)
base.send(:include, InstanceMethods)
end
module InstanceMethods
# Recursively builds simple HTML for an arbitrary data structure
# Suited for use with data from ActiveSupport::JSON.decode
def from_json_to_html(context = '')
output = ''
case self
when Hash
self.each do |k,v|
next if k.to_s =~ /id$/
output << "<div class='#{k}>"
output << v.from_json_to_html(k)
output << "</div>"
end
when Array
self.each do |o|
output << o.from_json_to_html(context)
end
when String, Numeric
output << "<p class='#{context}'>#{self}</p>"
else
output << ''
end
output
end
end
end
class Object; include ToHTML; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment