libs: remove http folder
[project/luci.git] / libs / uci / luasrc / model / uci / bind.lua
index 7e3085e..9472dab 100644 (file)
@@ -45,6 +45,10 @@ function bind.section(self, stype)
                inst.bind  = self
                inst.stype = stype
                inst.sid   = sid
+
+               if inst._init then
+                       inst:_init(sid)
+               end
        end
        return x
 end
@@ -90,49 +94,60 @@ bsection = utl.class()
 
 function bsection.uciop(self, op, ...)
        assert(self.bind and self.bind.uci,
-               "attempt to use unitialized binding: " .. type(self.sid))
+               "attempt to use unitialized binding")
 
-       return op and self.bind.uci[op](self.bind.uci, self.bind.cfg, ...)
-               or self.bind.uci
+       if op then
+               return self.bind.uci[op](self.bind.uci, self.bind.cfg, ...)
+       else
+               return self.bind.uci
+       end
 end
 
 function bsection.get(self, k, c)
        local v
-       self:uciop("foreach", self.stype,
-               function(s)
-                       if type(c) == "table" then
-                               local ck, cv
-                               for ck, cv in pairs(c) do
-                                       if s[ck] ~= cv then return true end
+       if type(c) == "string" then
+               v = self:uciop("get", c, k)
+       else
+               self:uciop("foreach", self.stype,
+                       function(s)
+                               if type(c) == "table" then
+                                       local ck, cv
+                                       for ck, cv in pairs(c) do
+                                               if s[ck] ~= cv then return true end
+                                       end
                                end
-                       elseif type(c) == "string" and s['.name'] ~= c then
-                               return true
-                       end
-                       if k ~= nil then
-                               v = s[k]
-                       else
-                               v = s
-                       end
-                       return false
-               end)
+                               if k ~= nil then
+                                       v = s[k]
+                               else
+                                       v = s
+                               end
+                               return false
+                       end)
+       end
        return v
 end
 
 function bsection.set(self, k, v, c)
        local stat
-       self:uciop("foreach", self.stype,
-               function(s)
-                       if type(c) == "table" then
-                               local ck, cv
-                               for ck, cv in pairs(c) do
-                                       if s[ck] ~= cv then return true end
-                               end
-                       elseif type(c) == "string" and s['.name'] ~= c then
-                               return true
-                       end
+       if type(c) == "string" then
+               if type(v) == "table" and #v == 0 then
+                       stat = self:uciop("delete", c, k)
+               else
                        stat = self:uciop("set", c, k, v)
-                       return false
-               end)
+               end
+       else
+               self:uciop("foreach", self.stype,
+                       function(s)
+                               if type(c) == "table" then
+                                       local ck, cv
+                                       for ck, cv in pairs(c) do
+                                               if s[ck] ~= cv then return true end
+                                       end
+                               end
+                               stat = self:uciop("set", c, k, v)
+                               return false
+                       end)
+       end
        return stat or false
 end
 
@@ -149,9 +164,9 @@ end
 function bsection.property_bool(self, k, n)
        self[n or k] = function(c, val)
                if val == nil then
-                       return self.bind:bool(c:get(k, c.sid))
+                       return bind:bool(c:get(k, c.sid))
                else
-                       return c:set(k, self.bind:bool(val) and "1" or "0", c.sid)
+                       return c:set(k, bind:bool(val) and "1" or "0", c.sid)
                end
        end
 end