-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.coffee
81 lines (80 loc) · 2.52 KB
/
Resource.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
_ = require 'lodash'
q = require 'q'
# abstract
class Resource
account: null
resourceFactory: null
constructor: ->
getPropertySet: ->
return _.keys @propertyMap
setAccessContext: (@account) ->
return @
setResourceFactory: (@resourceFactory) ->
return @
getResourceFactory: ->
if @resourceFactory == null
@resourceFactory = new (require './ResourceFactory')
@resourceFactory.addResource @
return @resourceFactory
can: (verb, id = null) ->
# no real clue yet on what I'm doing with this
#
# navigation =
# path: ''
# idList: []
#
# for rule in @access[verb]
# partList = rule.path.split '/'
# for part, i in partList
# if part == '{A}'
# navigation.idList.push @account
# else if path == '{true}'
# navigation.idList.push true
# else
# if i == 0
# navigation.path = part
# else
# navigation.path = navigation.path + '/' + part
getPropertyDefinition: (property) ->
if typeof @propertyMap[property] == 'undefined'
return null
definition = @propertyMap[property]
if typeof definition == 'string'
definition = type: definition
return definition
navigate: (path, resourceObj) ->
if _.isString path
path = path.split '/'
for x, i in path
path[i] = path[i].trim()
path = _.without path, ''
if path.length == 0
return @get resourceObj
property = path[0]
definition = @getPropertyDefinition property
if definition == null
throw "INVALID PATH - UNKNOWN : #{ property } is an unknown property for #{ @name }"
if definition.type == 'reference'
resource = @
return @get(resourceObj, property).then (reference) ->
referencedResource = resource.getResourceFactory().getResource definition.resource
if _.isArray reference
reference = _.unique reference
reference = _.without reference, null
return referencedResource.navigate path[1..], reference
if path.length > 1
throw "INVALID PATH - UNREACHABLE : #{ path[1] } is unreachable because it is specified after a value property"
if definition.type == 'valueList' && _.isArray resourceObj
return @get(resourceObj, property).then (list) ->
return _.uniq _.flatten list, true
# 'value'
return @get resourceObj, property
get: (id, propertyList = null) ->
# should be implemented by extending class
post: (data = null, propertyList = null) ->
# should be implemented by extending class
put: (id, data = null, propertyList = null) ->
# should be implemented by extending class
delete: (id) ->
# should be implemented by extending class
module.exports = Resource