 
     optimizer = Optimizer.create language
 
+    #console.log 'rules', typeof rules, rules
+
     @_recursiveNormalize rules, optimizer, rulesObj
 
     resourceObj.rules[name] = rulesObj
     @parsed[resource] = resourceObj
 
   _recursiveNormalize: (rules, optimizer, rulesObj, level=1)->
+    #padding = ''
     if _.isArray rules.either
       #console.log(padding + 'either', arguments[0], arguments[2])
       $or = []

      #console.log(padding + 'string', arguments[0], arguments[2])
      rulesObj.push optimizer.optimize rules

  applyContext: (resource, action, context)->
    root = _.cloneDeep @parsed[resource]?.rules
    cloned = root?[action]

    return unless cloned

    root = {$and: cloned}

    #for rule, index in cloned
    @_recursiveApplyContext root, '$and', cloned, context, '$and'

    if root.$and?.length == 1
      return root.$and[0]

    return root


      for key, subValue of value
        @_recursiveApplyContext value, key, subValue, context, key

  isAllowed: (resource, ruleName, model, context)->
    logger = log4js.getLogger 'isAllowed'
    logger.setLevel 'ERROR'

    logger.trace 'resource', resource
    logger.trace 'ruleName', ruleName
    logger.trace 'context', context

    rules = @applyContext resource, ruleName, context
    logger.debug 'rules', rules
    @validateModel model, rules

  createAllowed: (resource, model, context)->
    @isAllowed resource, 'create', model, context

  validateModel: (model, rules)->
    logger = log4js.getLogger 'validateModel'
