Class: Oxidized::SQL
Instance Method Summary
collapse
Methods inherited from Source
#map_model, #node_var_interpolate
Instance Method Details
#load(node_want = nil) ⇒ Object
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
|
# File 'lib/oxidized/source/sql.rb', line 21
def load node_want = nil
nodes = []
db = connect
query = db[@cfg.table.to_sym]
query = query.with_sql(@cfg.query) if @cfg.query?
if node_want
query = query.where(@cfg.map.name.to_sym => node_want)
end
query.each do |node|
keys = {}
@cfg.map.each { |key, sql_column| keys[key.to_sym] = node_var_interpolate node[sql_column.to_sym] }
keys[:model] = map_model keys[:model] if keys.has_key? :model
vars = {}
@cfg.vars_map.each do |key, sql_column|
vars[key.to_sym] = node_var_interpolate node[sql_column.to_sym]
end
keys[:vars] = vars unless vars.empty?
nodes << keys
end
db.disconnect
nodes
end
|
#setup ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/oxidized/source/sql.rb', line 9
def setup
if @cfg.empty?
Oxidized.asetus.user.source.sql.adapter = 'sqlite'
Oxidized.asetus.user.source.sql.database = File.join(Config::Root, 'sqlite.db')
Oxidized.asetus.user.source.sql.table = 'devices'
Oxidized.asetus.user.source.sql.map.name = 'name'
Oxidized.asetus.user.source.sql.map.model = 'rancid'
Oxidized.asetus.save :user
raise NoConfig, 'no source sql config, edit ~/.config/oxidized/config'
end
end
|