define [], () -> dictionary = {} dojo.declare "Automapper", null, createMap: (sourceKey, destinationKey) -> combinedKey = sourceKey + destinationKey dictionary[combinedKey] = {} functions = forMember: (key, e) -> dictionary[combinedKey][key] = e functions forAllMembers: (func) -> dictionary[combinedKey].__forAllMembers = func functions functions map: (sourceKey, destinationKey, sourceValue, destinationValue, lazy) -> return null if not sourceValue && sourceValue isnt false getValue = (item) -> return item() if typeof item is "function" && not lazy return item combinedKey = sourceKey + destinationKey mappings = dictionary[combinedKey] extensions = ignore: () -> mapFrom: (sourceMemberKey) -> throw sourceKey + "." + sourceMemberKey + " has not been defined." if not @.__sourceValue.hasOwnProperty sourceMemberKey value = getValue @.__sourceValue[sourceMemberKey] if mappings.__forAllMembers mappings.__forAllMembers @.__destinationValue, @.__key, value else @.__destinationValue[@.__key] = value throw "Could not find mapping with a source of " + sourceKey + " and a destination of " + destinationKey if not mappings mapItem = (destinationValue, sourceValue) -> for own key of destinationValue if mappings.hasOwnProperty(key) && mappings[key] if typeof mappings[key] is "function" extensions.__key = key extensions.__sourceValue = sourceValue extensions.__destinationValue = destinationValue output = mappings[key].call extensions else # forMember second parameter was not a function output = mappings[key] # object was returned from the 'forMember' call if output value = getValue output if mappings.__forAllMembers mappings.__forAllMembers destinationValue, key, value else destinationValue[key] = value else throw sourceKey + "." + key + " has not been defined." if not sourceValue.hasOwnProperty key value = getValue sourceValue[key] if mappings.__forAllMembers mappings.__forAllMembers destinationValue, key, value else destinationValue[key] = value # actually do the mapping here if sourceValue instanceof Array throw "Cannot map array to object" if not destinationValue instanceof Array # loop for i in [0..sourceValue.length] if not destinationValue[i]? throw "destinationKey of mapping must be a function in order to initialize the array" if typeof destinationKey isnt "function" destinationValue[i] = destinationKey() mapItem destinationValue[i], sourceValue[i] else throw "Cannot map object to array" if destinationValue instanceof Array mapItem destinationValue, sourceValue