Groovy web console

subscribe to the feed Subscribe
to this
site

Recipe DSL example using GEP-3 extended command expressions

Published 9 years ago by glaforge with tags gep3 dsl
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import static Container.*
import static Ingredient.*
import static CookingAction.*
import static Temperature.*

enum Container { medium_bowl }
enum Ingredient { soy_sauce, vinegar, chili_powder, garlic, chicken, sauce }
enum CookingAction { coat }
enum Temperature { room_temperature }

class Recipe {
    static instructions(Closure c) {
        def clone = c.clone()
        clone.delegate = new Recipe()
        clone.resolveStrategy = Closure.DELEGATE_ONLY
        clone()
    }
    
    void take(Container cont) {
        assert cont == medium_bowl
    }
    
    void combine(Ingredient... ingr) {
        assert ingr.every { it in Ingredient.values() }
    }
    
    def place(Ingredient ingr) {
        assert ingr == Ingredient.chicken
        [into: { Ingredient otherIngr -> assert otherIngr == Ingredient.sauce}]
    }
    
    def turn(Integer i) {
        assert i == 1
        [to: { CookingAction cAct -> assert cAct == CookingAction.coat }]
    }
    
    def marinate(Integer minutes) {
        assert minutes == 30
        [at: { Temperature temp -> assert temp == Temperature.room_temperature }]
    }
}

def (once, twice) = [1, 2]

Integer.metaClass.getMinutes { delegate }

Recipe.instructions {
    take medium_bowl
    combine soy_sauce, vinegar, chili_powder, garlic
    place chicken into sauce
    turn once to coat
    marinate 30.minutes at room_temperature
}

Integer.metaClass = null