       schema.type
 
   wrap: (type, data) ->
-    if wrapper = @representation_ids[type]
+    if wrapper = @representations[type]
       new wrapper(data)
     else
       data





  resourcify: (constructor, resource_type) ->
    client = @
    constructor ||= (@properties) ->
      @url = @properties.url

    @schema_manager = new SchemaManager(options.schemas...)
    @directory = options.directory
    @authorizer = options.authorizer

    @resource_definitions = options.resources

    @representations = @create_representation_constructors(@schema_manager.ids)
    @resource_constructors = @create_resource_constructors(options.resources, @representations)

    @resources = @create_resources(@directory, @resource_constructors)

  create_representation_constructors: (schemas) ->
    constructors = {}
    for id, schema of schemas
      if schema.type == "array"
        constructor = @array_wrapper(schema)
        constructors[schema.id] = constructor
      else if schema.type == "object"
        constructor = @object_wrapper(schema)
        constructors[schema.id] = constructor
      else if !SchemaManager.is_primitive(schema.type)
        constructor = @representation_constructor(schema)
        constructors[schema.id] = constructor
      else
        console.warn "Received unexpected schema type:", schema.type
    return constructors


  create_resource_constructors: (definitions, representations) ->
    constructors = {}
    for resource_type, definition of definitions
      for id, constructor of representations
        # The assumption here is that the frag-ident part of a schema id
        # corresponds to the resource type.
        [base, name] = id.split("#")
        if name == resource_type
          @resourcify(constructor, resource_type, definition)
          constructors[resource_type] = constructor

      # create a resource constructor for types with no directly associated schemas.
      constructors[resource_type] ||= @resourcify(null, resource_type, definition)
    return constructors


  # Create resource instances using the URLs supplied in the service
  # description's directory.
  create_resources: (directory, constructors) ->
    resources = {}
    for key, value of directory
      if constructors[key]
        resources[key] = new constructors[key](url: value)
    return resources

  array_wrapper: (schema) ->
    client = @
