2 FFLuCI - Configuration Bind Interface
5 Offers an interface for binding confiugration values to certain
6 data types. Supports value and range validation and basic dependencies.
12 Copyright 2008 Steven Barth <steven@midlink.org>
14 Licensed under the Apache License, Version 2.0 (the "License");
15 you may not use this file except in compliance with the License.
16 You may obtain a copy of the License at
18 http://www.apache.org/licenses/LICENSE-2.0
20 Unless required by applicable law or agreed to in writing, software
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
27 module("ffluci.cbi", package.seeall)
28 require("ffluci.template")
29 require("ffluci.util")
30 local Template = ffluci.template.Template
31 local class = ffluci.util.class
32 local instanceof = ffluci.util.instanceof
35 -- Node pseudo abstract class
38 function Node.__init__(self, title, description)
41 self.description = description
42 self.template = "cbi/node"
45 function Node.append(self, obj)
46 table.insert(self.children, obj)
49 function Node.render(self)
50 ffluci.template.render(self.template, self)
55 Map - A map describing a configuration file
59 function Map.__init__(self, config, ...)
60 Node.__init__(self, ...)
62 self.template = "cbi/map"
65 function Map.section(self, class, ...)
66 if instanceof(class, AbstractSection) then
67 local obj = class(...)
69 table.insert(self.children, obj)
72 error("class must be a descendent of AbstractSection")
80 AbstractSection = class(Node)
82 function AbstractSection.__init__(self, sectiontype, ...)
83 Node.__init__(self, ...)
84 self.sectiontype = sectiontype
87 function AbstractSection.option(self, class, ...)
88 if instanceof(class, AbstractValue) then
89 local obj = class(...)
91 table.insert(self.children, obj)
94 error("class must be a descendent of AbstractValue")
101 NamedSection - A fixed configuration section defined by its name
103 NamedSection = class(AbstractSection)
105 function NamedSection.__init__(self, section, ...)
106 AbstractSection.__init__(self, ...)
107 self.template = "cbi/nsection"
109 self.section = section
112 function NamedSection.option(self, ...)
113 local obj = AbstractSection.option(self, ...)
114 obj.section = self.section
120 TypedSection - A (set of) configuration section(s) defined by the type
121 addremove: Defines whether the user can add/remove sections of this type
122 anonymous: Allow creating anonymous sections
123 valid: a table with valid names or a function returning nil if invalid
125 TypedSection = class(AbstractSection)
127 function TypedSection.__init__(self, ...)
128 AbstractSection.__init__(self, ...)
129 self.template = "cbi/tsection"
131 self.addremove = true
132 self.anonymous = false
138 AbstractValue - An abstract Value Type
139 null: Value can be empty
140 valid: A function returning nil if invalid
141 depends: A table of option => value pairs of which one must be true
142 default: The default value
144 AbstractValue = class(Node)
146 function AbstractValue.__init__(self, option, ...)
147 Node.__init__(self, ...)
157 Value - A one-line value
158 maxlength: The maximum length
159 isnumber: The value must be a valid (floating point) number
160 isinteger: The value must be a valid integer
162 Value = class(AbstractValue)
164 function Value.__init__(self, ...)
165 AbstractValue.__init__(self, ...)
166 self.template = "cbi/value"
169 self.isnumber = false
170 self.isinteger = false
175 ListValue - A one-line value predefined in a list
177 ListValue = class(AbstractValue)
179 function ListValue.__init__(self, ...)
180 AbstractValue.__init__(self, ...)
181 self.template = "cbi/value"
186 function ListValue.addValue(self, key, val)