Globally reduce copyright headers
[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 --- Transparent UCI over RPC client.
9 -- @cstyle instance
10 module "luci.rpcc.ruci"
11
12
13 local Proxy = util.class()
14
15 --- Create a new UCI over RPC proxy.
16 -- @param rpccl RPC client
17 -- @return Network transparent UCI module 
18 function factory(rpccl)
19         return {
20                 cursor = function(...)
21                         return Proxy(rpccl, rpccl:request("ruci.cursor", {...}))
22                 end,
23                 cursor_state = function(...)
24                         return Proxy(rpccl, rpccl:request("ruci.cursor_state", {...}))
25                 end
26         }
27 end
28
29 function Proxy.__init__(self, rpccl, objid)
30         self.__rpccl = rpccl
31         self.__objid = objid
32
33         setmetatable(self, {
34                 __index = function(self, key)
35                         return rawget(self, key) or Proxy[key] or function(self, ...)
36                                 local argv = {self.__objid, ...}
37                                 return self.__rpccl:request("ruci."..key, argv)
38                         end
39                 end
40         })
41 end
42
43 function Proxy.foreach(self, config, section, callback)
44         local sections = self.__rpccl:request("ruci.foreach", {self.__objid, config, section})
45         if sections then
46                 for _, s in ipairs(sections) do
47                         callback(s)
48                 end
49                 return true
50         else
51                 return false
52         end
53 end