Commit 14471a2f35678f10e297eb5b009d2ce3bd934532
Committed by
Georg Hopp
1 parent
b0a176cf
another aproach to call a method given by a string....maybe less optimal
Showing
3 changed files
with
24 additions
and
6 deletions
| ... | ... | @@ -58,7 +58,7 @@ |
| 58 | 58 | - 'organizationalRole' |
| 59 | 59 | - 'MailAlias' |
| 60 | 60 | |
| 61 | - :dnPat: "cn=#{:user},o=#{:mail|sub(/.*@/, '')}" | |
| 61 | + :dnPat: "cn=#{:user},o=#{:mail|:sub, /.*@/, ''}" | |
| 62 | 62 | |
| 63 | 63 | :map: |
| 64 | 64 | :cn: :user |
| ... | ... | @@ -72,7 +72,7 @@ |
| 72 | 72 | - 'person' |
| 73 | 73 | - 'MailAlias' |
| 74 | 74 | |
| 75 | - :dnPat: "mail=#{:mail},o=#{:mail|sub(/.*@/, '')}" | |
| 75 | + :dnPat: "mail=#{:mail},o=#{:mail|:sub, /.*@/, ''}" | |
| 76 | 76 | |
| 77 | 77 | :map: |
| 78 | 78 | :sn: :surname |
| ... | ... | @@ -87,7 +87,7 @@ |
| 87 | 87 | - 'person' |
| 88 | 88 | - 'MailAccount' |
| 89 | 89 | |
| 90 | - :dnPat: "mail=#{:mail},o=#{:mail|sub(/.*@/, '')}" | |
| 90 | + :dnPat: "mail=#{:mail},o=#{:mail|:sub, /.*@/, ''}" | |
| 91 | 91 | |
| 92 | 92 | :map: |
| 93 | 93 | :homedirectory: :home | ... | ... |
| ... | ... | @@ -20,16 +20,29 @@ class DsAdmin::Storage::Config |
| 20 | 20 | ## |
| 21 | 21 | # replace special patterns within config strings with data |
| 22 | 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 | 33 | def eval_pattern(pattern, data = {}) |
| 26 | 34 | scan_exp = /([^#]*)(#\{(:[^}|]+)(\|([^}]*))?\})?/ |
| 27 | 35 | |
| 28 | 36 | result = String.new |
| 29 | 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 | 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 | 47 | result += m[0] + (val || "") |
| 35 | 48 | end | ... | ... |
| ... | ... | @@ -149,4 +149,9 @@ puts 'returns: ' + user.save |
| 149 | 149 | puts 'base: ' + user.inspect |
| 150 | 150 | |
| 151 | 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 | 157 | puts 'Memory useage: ' + `ps -o rss= -p #{Process.pid}` | ... | ... |
Please
register
or
login
to post a comment