summaryrefslogtreecommitdiffhomepage
path: root/src/ffluci/cbi.lua
blob: fc8b1aed3d54393a29fef169d0c0538ec01d34dc (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
--[[
FFLuCI - Configuration Bind Interface

Description:
Offers an interface for binding confiugration values to certain
data types. Supports value and range validation and basic dependencies.

FileId:
$Id$

License:
Copyright 2008 Steven Barth <steven@midlink.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

]]--
module("ffluci.cbi", package.seeall)
require("ffluci.template")
require("ffluci.util")
local class = ffluci.util.class
local instanceof = ffluci.util.instanceof


-- Node pseudo abstract class
Node = class()

function Node.__init__(self, title, description)
	self.children = {}
	self.title = title
	self.description = description
	self.template = "cbi/node"
end

function Node.append(self, obj)
	table.insert(self.children, obj)
end


--[[
Map - A map describing a configuration file 
]]--
Map = class(Node)

function Map.__init__(self, config, ...)
	Node.__init__(self, ...)
	self.config = config
	self.template = "cbi/map"
end

function Map.render(self)
	ffluci.template.render(self.template)
end

function Map.section(self, class, ...)
	if instanceof(class, AbstractClass) then
		local obj = class(...)
		obj.map = self
		table.insert(self.children, obj)
		return obj
	else
		error("class must be a descendent of AbstractSection")
	end
end


--[[
AbstractSection
]]--
AbstractSection = class(Node)

function AbstractSection.__init__(self, ...)
	Node.__init__(self, ...)
end

function AbstractSection.option(self, class, ...)
	if instanceof(class, AbstractValue) then
		local obj = class(...)
		obj.section = self
		obj.map     = self.map
		table.insert(self.children, obj)
		return obj
	else
		error("class must be a descendent of AbstractValue")
	end	
end
	


--[[
NamedSection - A fixed configuration section defined by its name
]]--
NamedSection = class(AbstractSection)

function NamedSection.__init__(self, section, ...)
	AbstractSection.__init__(self, ...)
	self.section = section
	self.template = "cbi/nsection"
end


--[[
TypedSection - A (set of) configuration section(s) defined by the type
	addremove: 	Defines whether the user can add/remove sections of this type
	anonymous:  Allow creating anonymous sections
	valid: 		a table with valid names or a function returning nil if invalid
]]--
TypedSection = class(AbstractSection)

function TypedSection.__init__(self, sectiontype, ...)
	AbstractSection.__init__(self, ...)
	self.sectiontype = sectiontype
	self.template  = "cbi/tsection"
	
	self.addremove = true
	self.anonymous = false
	self.valid     = nil
end


--[[
AbstractValue - An abstract Value Type
	null:		Value can be empty
	valid:		A table with valid names or a function returning nil if invalid
	depends:	A table of option => value pairs of which one must be true
]]--
AbstractValue = class(Node)

function AbstractValue.__init__(self, option, ...)
	Node.__init__(self, ...)
	self.option  = option
	
	self.null    = true
	self.valid   = nil
	self.depends = nil
end
	

--[[
Value - A one-line value 
	maxlength:	The maximum length
	isnumber:	The value must be a valid (floating point) number
	isinteger:  The value must be a valid integer
]]--
Value = class(AbstractValue)

function Value.__init__(self, ...)
	AbstractValue.__init__(self, ...)
	self.template  = "cbi/value"
	
	self.maxlength = nil
	self.isnumber  = false
	self.isinteger = false
end


--[[
Boolean - A simple boolean value 	
]]--
Boolean = class(AbstractValue)

function Boolean.__init__(self, ...)
	AbstractValue.__init__(self, ...)
	self.template = "cbi/boolean"
end