Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created February 12, 2014 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/e07f2d2d3539b7adf5e8 to your computer and use it in GitHub Desktop.
Save nhocki/e07f2d2d3539b7adf5e8 to your computer and use it in GitHub Desktop.
Description Hash in Ruby & Java
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());
}
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