-
Notifications
You must be signed in to change notification settings - Fork 1
/
Template.coffee
137 lines (105 loc) · 3.25 KB
/
Template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
sugar = require 'sugar'
fs = require 'fs'
string = require 'string'
templates = {}
class Template
constructor: (@schema, @section, @obj)->
@attributes = {}
@create()
getSection: (key)->
if @schema.sections[key]
@schema.sections[key]
create: ->
fields = @getSection(@section).fields
for key of fields
if fields[key].required
@attributes[key] = fields[key].default || ''
if @validate()
for key in (Object.keys fields).intersect(Object.keys @obj)
@attributes[key] = @obj[key]
validate: ->
for key of @obj
switch typeof @obj[key]
when 'object'
if Array.isArray @obj[key]
for k in @obj[key]
if typeof @obj[key][k] isnt ('string' or 'number')
throw new Error 'attribute array can\'t contain other objects or arrays'
else
throw new Error 'object must be array'
when 'number', 'string', 'boolean'
break
else
throw new Error 'obj can contain attributes with number, string, boolean or plain array'
true
getAttributeFromSection: (section, key)->
@getSection(section).fields[key]
set: ->
args = Array.prototype.slice.call arguments, 0
if typeof args[0] is 'object'
for key of args[0]
switch typeof args[0][key]
when 'object'
if Array.isArray args[0][key]
for k in args[0][key]
switch typeof k
when 'string', 'number', 'boolean'
@setOne key, k
else
throw new Error 'values of array must be string, number or boolean'
else
throw new Error 'value must be array'
when 'string', 'number', 'boolean'
@setOne key, args[0][key]
else
throw new Error 'not available set object'
else if args[1]
if Array.isArray args[1]
for k in args[1]
switch typeof k
when 'string', 'number', 'boolean'
@setOne args[0], k
else
throw new Error 'values of array must be string, number or boolean'
else
@setOne args[0], args[1]
else
throw new Error 'check set params'
setOne: (key, value)->
attribute = @getAttributeFromSection(@section, key)
if attribute
if attribute.available
if value in attribute.available
@attributes[key] = value
else
throw new Error 'Not available value'
else
if attribute.multiple
current = @attributes[key]
if current
if !Array.isArray current
@attributes[key] = []
@attributes[key].push current
@attributes[key].push value
else
@attributes[key] = value
else
@attributes[key] = value
else
throw new Error 'This key not in schema'
get: (key)->
@attributes[key]
getAttributes: ->
@attributes
getName: ->
@get('name')
fs
.readdirSync "#{__dirname}/templates"
.forEach (file)->
name = string file
.chompRight '.json'
.s
schema = require "./templates/#{file}"
templates[name] = (section, obj)->
new Template schema, section, obj
module.exports = templates