Skip to content

Instantly share code, notes, and snippets.

@dmitric
Created September 29, 2011 07:46
Show Gist options
  • Save dmitric/1250213 to your computer and use it in GitHub Desktop.
Save dmitric/1250213 to your computer and use it in GitHub Desktop.
MapBackedBeanUsage
def mappings = [
[csvProperty: "item name", classProperty: "name"],
[csvProperty: "cost in $", classProperty: "cost", conversion: CSVBeanBuilder.STRING_TO_DOUBLE],
[csvProperty: "count", classProperty: "itemCount", conversion: CSVBeanBuilder.STRING_TO_INT],
[csvProperty: "date", classProperty: "purchaseDate", conversion: CSVBeanBuilder.STRING_TO_DATE],
]
CSVBeanBuilder.loadAs(MapBackedBean.class, csvText, mappings).each {
transaction ->
assert(transaction.name == transaction.getName() && transaction.getName() == transaction["name"] )
assert(transaction.cost == transaction.getCost() && transaction.getCost() == transaction["cost"] )
assert(transaction.itemCount == transaction.getItemCount() && transaction.getItemCount() == transaction["itemCount"] )
assert(transaction.purchaseDate == transaction.getPurchaseDate() && transaction.purchaseDate == transaction["purchaseDate"] )
}
//And now we have a list of objects, where all of the fields/method call points/property call points were created
//dynamically, based solely on the classProperty fields in the mappings list!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment