Commit 14471a2f35678f10e297eb5b009d2ce3bd934532

Authored by Georg GH. Hopp
Committed by Georg Hopp
1 parent b0a176cf

another aproach to call a method given by a string....maybe less optimal

@@ -58,7 +58,7 @@ @@ -58,7 +58,7 @@
58 - 'organizationalRole' 58 - 'organizationalRole'
59 - 'MailAlias' 59 - 'MailAlias'
60 60
61 - :dnPat: "cn=#{:user},o=#{:mail|sub(/.*@/, '')}" 61 + :dnPat: "cn=#{:user},o=#{:mail|:sub, /.*@/, ''}"
62 62
63 :map: 63 :map:
64 :cn: :user 64 :cn: :user
@@ -72,7 +72,7 @@ @@ -72,7 +72,7 @@
72 - 'person' 72 - 'person'
73 - 'MailAlias' 73 - 'MailAlias'
74 74
75 - :dnPat: "mail=#{:mail},o=#{:mail|sub(/.*@/, '')}" 75 + :dnPat: "mail=#{:mail},o=#{:mail|:sub, /.*@/, ''}"
76 76
77 :map: 77 :map:
78 :sn: :surname 78 :sn: :surname
@@ -87,7 +87,7 @@ @@ -87,7 +87,7 @@
87 - 'person' 87 - 'person'
88 - 'MailAccount' 88 - 'MailAccount'
89 89
90 - :dnPat: "mail=#{:mail},o=#{:mail|sub(/.*@/, '')}" 90 + :dnPat: "mail=#{:mail},o=#{:mail|:sub, /.*@/, ''}"
91 91
92 :map: 92 :map:
93 :homedirectory: :home 93 :homedirectory: :home
@@ -20,16 +20,29 @@ class DsAdmin::Storage::Config @@ -20,16 +20,29 @@ class DsAdmin::Storage::Config
20 ## 20 ##
21 # replace special patterns within config strings with data 21 # replace special patterns within config strings with data
22 # given by the data hash param. These patterns also allow to send 22 # given by the data hash param. These patterns also allow to send
23 - # a message to the replaces data e.g. for some kind of subtitution. 23 + # a message to the actual substituted data to do any kind of
  24 + # conversion to it.
  25 + #
  26 + # actually this is used within the handling of the ldap specific
  27 + # :dnPat config parameter to generate the dn entry for a given model
  28 + # Data (create or replace)
  29 + #
  30 + # TODO: I would like to get rid of the eval statements but i don't know
  31 + # a better way to build the correct objects from the config strings
24 # 32 #
25 def eval_pattern(pattern, data = {}) 33 def eval_pattern(pattern, data = {})
26 scan_exp = /([^#]*)(#\{(:[^}|]+)(\|([^}]*))?\})?/ 34 scan_exp = /([^#]*)(#\{(:[^}|]+)(\|([^}]*))?\})?/
27 35
28 result = String.new 36 result = String.new
29 pattern.scan(scan_exp) do |m| 37 pattern.scan(scan_exp) do |m|
30 - key = m[2][1..m[2].length].to_sym if m[2] 38 + key = (eval m[2]) if m[2]
31 val = data[key] if data[key] 39 val = data[key] if data[key]
32 - val = eval('"' + val + '".send ' + m[4]) if val && m[4] 40 +
  41 + args = m[4].split(',').map do |arg|
  42 + eval arg.strip
  43 + end if m[4]
  44 +
  45 + val = val.send *args if args
33 46
34 result += m[0] + (val || "") 47 result += m[0] + (val || "")
35 end 48 end
@@ -149,4 +149,9 @@ puts 'returns: ' + user.save @@ -149,4 +149,9 @@ puts 'returns: ' + user.save
149 puts 'base: ' + user.inspect 149 puts 'base: ' + user.inspect
150 150
151 puts 151 puts
  152 +alias_person.mail = 'noop@steffers.org'
  153 +puts 'returns: ' + alias_person.save
  154 +puts 'base: ' + alias_person.inspect
  155 +
  156 +puts
152 puts 'Memory useage: ' + `ps -o rss= -p #{Process.pid}` 157 puts 'Memory useage: ' + `ps -o rss= -p #{Process.pid}`
Please register or login to post a comment