Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 11

[ 303 ]

Date getMediaExpiry(media) {
if(mediaList[media] != null) {
return mediaList[media]
}
}
}

The Media class is used to describe individual items from the media catalog.
Properties of the class define its price and access points value, along with other
properties that help to categorize it, such as the media type (VIDEO, GAME, or SONG),
the publisher, and whether it is a new release or not.


class Media {
String title
String publisher
String type
boolean newRelease
int points
double price
int daysAccess
}

The BroadbandPlus class implements the backend services that we expect, and
defines the APIs that we need to manage the consumption of media, purchasing,
and account upgrades. These APIs make calls to RewardService as required, in
order to apply the various rewards for onConsume, onPurchase, and onUpgrade.


The consume API will add the consumed media to the account's media list on the
first consumption. The purchase API adds the points value of the purchased media
to the account's points balance. Upgrade takes the current period balance into
account by simply adding the points difference between the original and
upgrade plans.


class BroadbandPlus {
def rewards = new RewardService()

def canConsume = { account, media ->
def now = new Date()
if (account.mediaList[media]?.after(now))
return true

account.points > media.points
}
def consume = { account, media ->
// First consume add media to accounts access list
Free download pdf