Skip to content

Instantly share code, notes, and snippets.

@hakanai
Created October 22, 2009 22:30
Show Gist options
  • Save hakanai/216409 to your computer and use it in GitHub Desktop.
Save hakanai/216409 to your computer and use it in GitHub Desktop.
#
# 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