concept Groovy in category dsl

This is an excerpt from Manning's book DSLs in Action.
The most popular way to classify DSLs is related to the way you implement them. Martin Fowler made this broad classification some time back and it’s recognized and followed by almost all practitioners in the industry today. He classifies a DSL as internal or external, depending on whether it’s been implemented on top of an existing host language. Internal DSLs are also known as embedded DSLs because they’re implemented as an embedding within a host language. (Internal DSLs will be discussed further in chapters 5 and 6 where you’ll implement DSLs using JVM languages like Ruby, Groovy, Scala, and Clojure.) External DSLs are also called standalone DSLs because they’re developed ground-up as an independent language, without using the infrastructure of an existing host language. Chapters 7 and 8 deal more with external DSLs.
Listing 3.1. ClientOrder.groovy: order-processing DSL in Groovy
ExpandoMetaClass.enableGlobally() class Order { def security def quantity def limitPrice def allOrNone def value def bs def buy(su, closure) { bs = 'Bought' buy_sell(su, closure) } def sell(su, closure) { bs = 'Sold' buy_sell(su, closure) } def getTo() { this } private buy_sell(su, closure) { security = su[0] quantity = su[1] closure() } } def methodMissing(String name, args) { order.metaClass.getMetaProperty(name).setProperty(order, args) } def getNewOrder() { order = new Order() } def valueAs(closure) { order.value = closure(order.quantity, order.limitPrice[0]) order } Integer.metaClass.getShares = { -> delegate } Integer.metaClass.of = { instrument -> [instrument, delegate] }
The languages I’ve selected for this chapter are dynamically typed. I start the chapter by discussing some of the attributes of dynamically typed languages that make good DSLs and why I selected Ruby, Groovy, and Clojure for our discussion. Then we’ll jump right into the implementation part and take you successively through the process of implementing complete DSLs using each of the three languages. We discuss the main features that these languages offer that you’ll frequently use in designing DSLs and some of the rationale for when to select which pattern for your implementation.