Created
August 13, 2013 15:03
-
-
Save cilquirm/6222114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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