Skip to content

Instantly share code, notes, and snippets.

View dtanner's full-sized avatar

Dan Tanner dtanner

  • Target
  • Minneapolis MN US
View GitHub Profile
@dtanner
dtanner / groovy-spread-populated.groovy
Last active August 29, 2015 14:09
Groovy spread operators on a populated collection
def things = [ [a: 1], [a: 2] ]
things.collect { it.a } // returns [1, 2]
things*.a // returns [1, 2]
things.a // returns [1, 2]
@dtanner
dtanner / groovy-spread-null-collection.groovy
Last active August 29, 2015 14:09
Groovy spread operators on a null collection
def things = null
things.collect { it.a } // returns []
things*.a // returns null
things.a // throws a NullPointerException
@dtanner
dtanner / TomcatDatasourceUtil.groovy
Last active August 29, 2015 14:20
Grails Tomcat Datasource Utility
package com.foo.util
import groovy.util.logging.Log4j
import org.apache.tomcat.jdbc.pool.ConnectionPool
import org.codehaus.groovy.grails.commons.GrailsApplication
@Log4j
class TomcatDatasourceUtil {
static void ensureCurrentDatasources(GrailsApplication application, List datasourceNames) {
@dtanner
dtanner / RemoteConfigService.groovy
Created April 30, 2015 05:39
Example of a basic utility class to interact with etcd
package com.foo.config
import com.foog.util.TomcatDatasourceUtil
import grails.plugins.rest.client.RestBuilder
import groovy.json.JsonSlurper
/**
* Application service to get and set values from a centralized remote configuration service.
*/
class RemoteConfigService {
@dtanner
dtanner / TestGen.groovy
Created October 30, 2012 18:03
Groovy script to generate addition table quizzes for kids.
def getRandomNumber() {
randomInt = new Random().nextInt(11)
}
def buildCell() {
"""${getRandomNumber()} <br>
+ ${getRandomNumber()} <br>
<hr width=40px>
"""
@dtanner
dtanner / Bootstrap.groovy
Created December 16, 2012 22:54
Example of a grails project using mybatis migrations in the Bootstrap.groovy, automatically nuking and paving the test databases.
def init = { servletContext ->
if (["test", "ci", "functional"].contains(Environment.current.name)) {
// bring some environments down to bare database. 20120609160926 is the base migration script's ID
// limitation: if we add development to this list and run the app in IDEA, it orphans the java processes
executeCommand ("${getMigrationCommand()} --env=${Environment.current.name} version 20120609160926", new File("./db"))
executeCommand ("${getMigrationCommand()} --env=${Environment.current.name} up", new File("./db"))
}
// other init operations...
}
@dtanner
dtanner / UsageTrackingFilters.groovy
Created February 3, 2013 17:00
Sample Grails usage tracking filter. Optimized for splunk logging with its key=value separation.
package myapp
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.apache.log4j.MDC
class UsageTrackingFilters {
private static final Log LOG = LogFactory.getLog('usagetracking')
private static final String REQUEST_ID = "requestId"
@dtanner
dtanner / _field.gsp
Created September 27, 2013 14:43
Allow class attribute on a custom Grails Fields template
<%@ page defaultCodec="none" %>
<div class="form-group ${formGroupClass ?: ''} <g:if test="${invalid}">has-error</g:if>">
<label class="control-label ${labelClass ?: ''}" for="${property}">${label} <%=required ? "*" : ""%></label>
<input type="<%=type ?: "text" %>" name="${prefix ?: ''}${property}" id="${prefix ?: ''}${property}" value="${value}" <%=constraints.maxSize ? "maxlength=${constraints.maxSize}" : ""%> <%=readonly ? "readonly=readonly" : ""%> class="form-control <%= pageScope["class"] ?: '' %>" <%=placeholder ? "placeholder='$placeholder'" : ""%>/>
<g:if test="${helpText}"><span class="help-block">${helpText}</span></g:if>
<g:if test="${invalid}"><span class="help-block">${errors.join('<br>')}</span></g:if>
</div>
@dtanner
dtanner / osx-10.9-setup.md
Last active March 20, 2016 18:15 — forked from kevinelliott/osx-10.9-setup.md
Clean Install – Mac OS X 10.9 Mavericks

Install Homebrew / Cask

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install caskroom/cask/brew-cask

todo - replace this with brewfile

Install common applications via Homebrew

@dtanner
dtanner / LocalAndZonedDates.groovy
Last active April 12, 2016 22:40
LocalAndZonedDates
import java.time.LocalDate
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAdjusters
// If you have a date without time information, use a LocalDate. e.g. someone's birthday
LocalDate localDate = new LocalDate(2016, 4, 12)
println localDate.toString() // 2016-04-12