July 2013
Intermediate to advanced
370 pages
8h 27m
English
Categories apply only within the use blocks, and their effect is fairly limited in scope. If we want the method injection to be effective throughout our application, we can use the ExpandoMetaClass
instead of categories. Let’s use the ExpandoMetaClass to implement the DSL syntax
we saw in the previous section:
| CreatingDSLs/DSLUsingExpandoMetaClass.groovy | |
| | Integer.metaClass{ |
| | getDays = { -> |
| | delegate |
| | } |
| | |
| | getAgo = { -> |
| | def date = Calendar.instance |
| | date.add(Calendar.DAY_OF_MONTH, -delegate) |
| | date |
| | } |
| | } |
| | |
| | Calendar.metaClass.at = { Map time -> |
| | def hour = 0 |
| | def minute = 0 |
| | time.each {key, value -> |
| | hour = key.toInteger() |
| | minute = value.toInteger() |
| | } |
| | |
| | delegate.set(Calendar ... |
Read now
Unlock full access