Merge pull request #304 from nmav/ocserv-crypt
[project/luci.git] / libs / luci-lib-rpcc / luasrc / rpcc / ruci.lua
1 -- Copyright 2009 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local util = require "luci.util"
5 local rawget, setmetatable = rawget, setmetatable
6 local ipairs = ipairs
7
8 module "luci.rpcc.ruci"
9
10
11 local Proxy = util.class()
12
13 function factory(rpccl)
14         return {
15                 cursor = function(...)
16                         return Proxy(rpccl, rpccl:request("ruci.cursor", {...}))
17                 end,
18                 cursor_state = function(...)
19                         return Proxy(rpccl, rpccl:request("ruci.cursor_state", {...}))
20                 end
21         }
22 end
23
24 function Proxy.__init__(self, rpccl, objid)
25         self.__rpccl = rpccl
26         self.__objid = objid
27
28         setmetatable(self, {
29                 __index = function(self, key)
30                         return rawget(self, key) or Proxy[key] or function(self, ...)
31                                 local argv = {self.__objid, ...}
32                                 return self.__rpccl:request("ruci."..key, argv)
33                         end
34                 end
35         })
36 end
37
38 function Proxy.foreach(self, config, section, callback)
39         local sections = self.__rpccl:request("ruci.foreach", {self.__objid, config, section})
40         if sections then
41                 for _, s in ipairs(sections) do
42                         callback(s)
43                 end
44                 return true
45         else
46                 return false
47         end
48 end