Created
October 22, 2009 22:30
-
-
Save hakanai/216409 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
# | |
# So I'm looking for a nifty way of expanding this to support outputting to indented plain text... | |
# | |
module AuditReportsHelper | |
def format_yaml(yaml) | |
obj = YAML.load(yaml) | |
format_object(obj) | |
end | |
def format_object(obj) | |
case obj | |
when Hash | |
format_hash(obj) | |
when YAML::DomainType | |
format_domain_object(obj) | |
when String | |
format_as_string(obj) | |
when Integer | |
format_integer_as_string(obj) | |
when nil | |
'' | |
else | |
html_escape("Unknown type: #{obj.class}") | |
end | |
end | |
def format_hash(hash) | |
"<ul>" + hash.map { |key, value| "<li>#{format_object(key)}: #{format_object(value)}</li>" }.join("\n") + "</ul>" | |
end | |
def format_domain_object(obj) | |
"#{html_escape(obj.type_id)}: #{format_hash(obj.value)}" | |
end | |
def format_as_string(string_like) | |
html_escape(string_like.to_s) | |
end | |
def format_integer_as_string(integer) | |
# Warning: Voodoo. Don't touch unless you want a curse. | |
integer.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment