Skip to content

Instantly share code, notes, and snippets.

@cilquirm
Created August 13, 2013 15:03
Show Gist options
  • Save cilquirm/6222120 to your computer and use it in GitHub Desktop.
Save cilquirm/6222120 to your computer and use it in GitHub Desktop.
import groovy.json.*
def structure = '''
{
"name": "Unit 1 Keyname",
"structuralName": "unit",
"businessKey": "8a9d81d54017c6b1014017c6d4250021",
"revision": 0,
"meta": {
"type": "UNIT"
},
"attributes": [
{
"name": "Title",
"value": "Unit 1 Title"
}
],
"children": [
{
"name": "Lesson 1",
"structuralName": "lesson",
"businessKey": "8a9d81d54017c6b1014017c6d4b10024",
"revision": 0,
"meta": {
"type": "COMPONENT"
},
"attributes": [
{
"name": "Title",
"value": "Lesson 1 Title"
}
],
"children": [
{
"name": "Activity Keyname",
"structuralName": "activity",
"businessKey": "8a9d81d54017c6b1014017c6d580002a",
"revision": 0,
"meta": {
"type": "ACTIVITY"
},
"attributes": [
{
"name": "Title",
"value": "Activity Title"
},
{
"name": "Teacher Guide",
"value": {
"name": "whatever",
"structuralName": "teacherGuide",
"businessKey": "8a9d81d54017c6b1014017c6d5e9002d",
"revision": 0,
"meta": {
"type": "COMPONENT"
},
"attributes": [
{
"name": "Teacher Instruction",
"value": "Some Instruction for the Teacher..."
}
],
"children": [
],
"addenda": {
"assets": [
]
}
}
},
{
"name": "A Number",
"value": 6
},
{
"name": "A Decimal",
"value": 3.1470000000
},
{
"name": "A Date",
"value": "0008-11-29T05:00:00+0000"
},
{
"name": "A Boolean",
"value": true
},
{
"name": "activityConfiguration",
"value": "{'type':'association','stimulus_list':['blue','green','red'],'possible_responses':['apple, grape, crayon']}"
}
],
"children": [
],
"addenda": {
"assets": [
]
}
}
],
"addenda": {
"assets": [
]
}
},
{
"name": "Lesson 2",
"structuralName": "lesson",
"businessKey": "8a9d81d54017c6b1014017c6d5160027",
"revision": 0,
"meta": {
"type": "COMPONENT"
},
"attributes": [
{
"name": "Title",
"value": "Lesson 2 Title"
}
],
"children": [
],
"addenda": {
"assets": [
]
}
}
],
"addenda": {
"assets": [
]
}
}
'''
def json = new JsonSlurper().parseText(structure)
class CurriculumTransformer {
static rules = []
static def leftShift( Map rule ) {
rules << rule
}
public CurriculumTransformer( Map json ) {
}
}
class CurriculumStructureVisitor {
def visit( element ) {
def el = process(element) {
el.children.each {
visit( it )
}
}
def process( el ) {
rules.each { rule ->
if ( rule.if( el ) ( {
rule.then( el )
}
}
}
}
CurriculumTransformer.rules << [ when: { it.meta.type == 'ACTIVITY' } , then : { it.someValue = 'test' } ]
CurriculumTransformer.rules << [ when: { it.structuralName = 'cardStack' }, then : { it.children.all( it.meta.type == "ACTIVITY" ).*teacherGuide ) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment