config.rb
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
class DsAdmin::Storage::Config
attr_accessor :model
def initialize(conf_data)
@config = conf_data
end
def con(storage)
@config[storage.config_key][:con]
end
def query(storage)
@config[storage.config_key][@model.config_key][:query]
end
def map(storage)
@config[storage.config_key][@model.config_key][:map]
end
##
# replace special patterns within config strings with data
# given by the data hash param. These patterns also allow to send
# a message to the actual substituted data to do any kind of
# conversion to it.
#
# actually this is used within the handling of the ldap specific
# :dnPat config parameter to generate the dn entry for a given model
# Data (create or replace)
#
# TODO: I would like to get rid of the eval statements but i don't know
# a better way to build the correct objects from the config strings
#
def eval_pattern(pattern, data = {})
scan_exp = /([^#]*)(#\{(:[^}|]+)(\|([^}]*))?\})?/
result = String.new
pattern.scan(scan_exp) do |m|
key = (eval m[2]) if m[2]
val = data[key] if data[key]
args = m[4].split(',').map do |arg|
eval arg.strip
end if m[4]
val = val.send *args if args
result += m[0] + (val || "")
end
result
end
end