Created
February 8, 2010 06:06
-
-
Save Kimtaro/297928 to your computer and use it in GitHub Desktop.
This file contains 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 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