Sto cercando di analizzare un file YAML come questo:trasformare un hash rubino nella lista html
a:
a1:
a2:
b:
b1:
b11:
b2:
ottengo un hash come questo:
{"a"=>{"a1"=>nil, "a2"=>nil}, "b"=>{"b1"=>{"b11"=>nil}, "b2"=>nil}}
e voglio girarlo al un elenco:
%ul
%li a
%ul
%li a1
%li a2
%li b
%ul
%li b1
%ul
%li b11
%li b2
sto cercando di cercare il modo più efficiente non importa quanto in profondità è l'hash
Infine ho fatto in questo modo:
KeyWords = %w(url)
# Convert a multilevel hash into haml multilevel tree
# Special KeyWords
# url : item url
def hash_to_haml(hash, url = nil)
haml_tag(:ul) do
hash.each do |key, value|
unless KeyWords.include?(key)
url = get_url(key, value)
haml_tag(:li) do
haml_tag(:a, :href => url) do
haml_concat(key)
end
hash_to_haml(value) if value.is_a?(Hash) && !value.empty?
end
end
end
end
end
private
def get_url(key, hash)
# TODO: get full url from hash
if hash.nil?
"/#{key}"
else
hash.include?("url") ? hash.delete("url") : "/#{key}"
end
end
Ora è pronto per analizzare le opzioni troppo.
è possibile correggere la formattazione della domanda? – cam
Questo è un bel problema per un codice kata. – kikito