Created
February 12, 2014 22:22
-
-
Save nhocki/e07f2d2d3539b7adf5e8 to your computer and use it in GitHub Desktop.
Description Hash in Ruby & Java
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
private String descriptionVariable(final String text){ | |
if(isFeatured()){ | |
return "<b>" + text + "</b>"; | |
}else{ | |
return "<b><font color='#FD93B1'>" + text + "</font></b>"; | |
} | |
} | |
private Map<String, String> buildDescriptionContext(final JSONObject descriptionHash) throws JSONException, ParseException{ | |
Map<String, String> context = new HashMap<String, String>(); | |
Iterator keys = descriptionHash.keys(); | |
while(keys.hasNext()){ | |
String key = (String) keys.next(); | |
context.put(key, descriptionVariable(descriptionHash.getString(key))); | |
} | |
return context; | |
} | |
public void setMessageFromHash(JSONObject descriptionHash) throws JSONException, ParseException { | |
if(!descriptionHash.has("template")){ | |
throw new JSONException("Description hash MUST include a template value"); | |
} | |
Template tmpl = Mustache.compiler().escapeHTML(false).compile(descriptionHash.getString("template")); | |
setMessage(tmpl.execute(buildDescriptionContext(descriptionHash))); | |
WhiLog.d("Mustache Parser", getMessage()); | |
} |
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
def description(*args, &block) | |
block ||= TEMPLATE_NOOP | |
hash = description_hash(*args) | |
hash[:template].to_s.gsub(TEMPLATE_VARIABLE) do | |
block.call(hash[$1.to_sym]) | |
end | |
end | |
notification.description # => no format | |
notification.description{|v| "<b>#{v}</b>"} # => bold format |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment