Commit a4818bf1d60b74ae18105669239743e61bf3ae09

Authored by Georg GH. Hopp
Committed by Georg Hopp
1 parent 5da7e735

fix for Model#_load to correctly initialize the current model in storage config …

…object and some cleanups
... ... @@ -42,21 +42,25 @@ module DsAdmin::Model
42 42 initialize(_load(id))
43 43 end
44 44
  45 + ##
  46 + # used to save either new or changed models
  47 + #
  48 + # @storage is free to change an already existent storage id
  49 + # for organizational, indexing or other reasons thus it
  50 + # always has to be stored back into @id
  51 + #
45 52 def save
46   - @id = @storage.create_id(self) unless @id
47   - @storage.write(self)
  53 + @id = @storage.write(self)
48 54 end
49 55
50   - def update
51   - @storage.write(self)
52   - end
53   -
54   - def to_sym
  56 + def config_key
55 57 self.class.to_s.to_sym
56 58 end
57 59
58 60 protected
59 61 def _load(id)
  62 + @@storage.config.model = self
  63 +
60 64 data = @@storage.find {|data| data[:id] == id}
61 65 throw "unknown id (#{id})" unless data
62 66 data
... ...
1 1 module DsAdmin::Storage
2 2 include Enumerable
3 3
  4 + DsAdmin::Storage.autoload(:Config, 'storage/config')
4 5 DsAdmin::Storage.autoload(:Ldap, 'storage/ldap')
5 6 DsAdmin::Storage.autoload(:Mysql, 'storage/mysql')
6   - DsAdmin::Storage.autoload(:Config, 'storage/config')
7 7
8 8 attr_accessor :config
9 9
... ... @@ -15,23 +15,19 @@ module DsAdmin::Storage
15 15 read.each(&block)
16 16 end
17 17
18   - ##
19   - # We don't need this....the 'id' is a storage id and as
20   - # thus returned after successfully writing a new entry.
21   - #
22   - def create_id(model)
23   - return "dummy id for #{model.inspect}"
24   - end
25   -
26 18 def read
27 19 throw "#{self.class}: read not implemented"
28 20 end
29 21
  22 + ##
  23 + # write has to return the storage id of the written
  24 + # entry.
  25 + #
30 26 def write(model)
31 27 throw "#{self.class}: write not implemented"
32 28 end
33 29
34   - def to_sym
  30 + def config_key
35 31 self.class.to_s.to_sym
36 32 end
37 33 end
... ...
1   -require 'yaml'
2   -
3 1 class DsAdmin::Storage::Config
4 2 attr_accessor :model
5 3
6   - def initialize(yml_file)
7   - @config = YAML.load_file(yml_file)
  4 + def initialize(conf_data)
  5 + @config = conf_data
8 6 end
9 7
10 8 def con(storage)
11   - @config[storage.to_sym][:con]
  9 + @config[storage.config_key][:con]
12 10 end
13 11
14 12 def query(storage)
15   - @config[storage.to_sym][@model.to_sym][:query]
  13 + @config[storage.config_key][@model.config_key][:query]
16 14 end
17 15
18 16 def map(storage)
19   - @config[storage.to_sym][@model.to_sym][:map]
  17 + @config[storage.config_key][@model.config_key][:map]
20 18 end
21 19 end
... ...
1 1 require 'ds_admin'
  2 +require 'yaml'
2 3
3   -config = DsAdmin::Storage::Config.new(File.dirname(__FILE__) + '/config/storage.yml')
4   -DsAdmin::Model.storage = DsAdmin::Storage::Ldap.new(config)
  4 +DsAdmin::Model.storage = DsAdmin::Storage::Ldap.new(
  5 + DsAdmin::Storage::Config.new(
  6 + YAML.load_file(File.dirname(__FILE__) + '/config/storage.yml')
  7 +))
5 8
6 9 user = DsAdmin::Model::User.new
7 10 group = DsAdmin::Model::Group.new
... ... @@ -10,31 +13,108 @@ alias_role = DsAdmin::Model::MailAliasRole.new
10 13 alias_person = DsAdmin::Model::MailAliasPerson.new
11 14 account = DsAdmin::Model::MailAccount.new
12 15
  16 +puts '=== base objects ==='
  17 +puts 'base: ' + user.inspect
  18 +puts 'base: ' + group.inspect
  19 +puts 'base: ' + site.inspect
  20 +puts 'base: ' + alias_role.inspect
  21 +puts 'base: ' + alias_person.inspect
  22 +puts 'base: ' + account.inspect
  23 +
  24 +puts
  25 +puts '=== Model#all ==='
  26 +puts user.all.inspect
  27 +puts 'base: ' + user.inspect
  28 +puts group.all.inspect
  29 +puts 'base: ' + group.inspect
  30 +puts site.all.inspect
  31 +puts 'base: ' + site.inspect
  32 +puts alias_role.all.inspect
  33 +puts 'base: ' + alias_role.inspect
  34 +puts alias_person.all.inspect
  35 +puts 'base: ' + alias_person.inspect
  36 +puts account.all.inspect
  37 +puts 'base: ' + account.inspect
  38 +
  39 +puts
13 40 puts '=== DsAdmin each ==='
14   -user.each {|user| puts user.inspect}
  41 +user.each {|u| puts u.inspect}
  42 +puts 'base: ' + user.inspect
15 43 puts '---'
16   -group.each {|group| puts group.inspect}
  44 +group.each {|g| puts g.inspect}
  45 +puts 'base: ' + group.inspect
17 46 puts '---'
18   -site.each {|site| puts site.inspect}
  47 +site.each {|s| puts s.inspect}
  48 +puts 'base: ' + site.inspect
19 49 puts '---'
20   -alias_role.each {|ma_role| puts ma_role.inspect}
  50 +alias_role.each {|mar| puts mar.inspect}
  51 +puts 'base: ' + alias_role.inspect
21 52 puts '---'
22   -alias_person.each {|ma_person| puts ma_person.inspect}
  53 +alias_person.each {|map| puts map.inspect}
  54 +puts 'base: ' + alias_person.inspect
23 55 puts '---'
24 56 account.each {|macc| puts macc.inspect}
  57 +puts 'base: ' + account.inspect
25 58
26 59 puts
27 60 puts '=== DsAdmin Enumerable ==='
28   -puts user.find {|user| user.uid == '1000'}.inspect
  61 +puts user.find {|u| u.uid == '1000'}.inspect
  62 +puts 'base: ' + user.inspect
29 63 puts '---'
30   -puts group.find {|group| group.gid == '10'}.inspect
  64 +puts group.find {|g| g.gid == '10'}.inspect
  65 +puts 'base: ' + group.inspect
31 66 puts '---'
32   -puts site.find {|site| site.name == 'steffers.org'}.inspect
  67 +puts site.find {|s| s.name == 'steffers.org'}.inspect
  68 +puts 'base: ' + site.inspect
33 69 puts '---'
34 70 puts alias_role.find_all {|mar| mar.maildrop == 'abuse'}.inspect
  71 +puts 'base: ' + alias_role.inspect
35 72 puts '---'
36 73 puts alias_person.find {|map| map.mail == 'georg@steffers.org'}.inspect
  74 +puts 'base: ' + alias_person.inspect
37 75 puts '---'
38   -puts account.find {|acc| acc.mail == 'drachenfrau@steffers.org'}.inspect
  76 +puts account.find {|macc| macc.mail == 'drachenfrau@steffers.org'}.inspect
  77 +puts 'base: ' + account.inspect
39 78
  79 +puts
  80 +puts '=== Model#load ==='
  81 +puts user.load('uid=georg,ou=user,o=system,dc=weird-web-workers,dc=org').inspect
  82 +puts 'base: ' + user.inspect
  83 +puts '---'
  84 +puts group.load('cn=wheel,ou=group,o=system,dc=weird-web-workers,dc=org').inspect
  85 +puts 'base: ' + group.inspect
  86 +puts '---'
  87 +puts site.load('o=steffers.org,o=hosting,dc=weird-web-workers,dc=org').inspect
  88 +puts 'base: ' + site.inspect
  89 +puts '---'
  90 +puts alias_role.load('cn=abuse,o=weird-web-workers.org,o=hosting,dc=weird-web-workers,dc=org').inspect
  91 +puts 'base: ' + alias_role.inspect
  92 +puts '---'
  93 +puts alias_person.load('mail=georg@steffers.org,o=steffers.org,o=hosting,dc=weird-web-workers,dc=org').inspect
  94 +puts 'base: ' + alias_person.inspect
  95 +puts '---'
  96 +puts account.load('mail=drachenfrau@steffers.org,o=steffers.org,o=hosting,dc=weird-web-workers,dc=org').inspect
  97 +puts 'base: ' + account.inspect
  98 +
  99 +puts
  100 +puts '=== Model#load! ==='
  101 +user.load!('uid=georg,ou=user,o=system,dc=weird-web-workers,dc=org')
  102 +puts 'base: ' + user.inspect
  103 +puts '---'
  104 +group.load!('cn=wheel,ou=group,o=system,dc=weird-web-workers,dc=org')
  105 +puts 'base: ' + group.inspect
  106 +puts '---'
  107 +site.load!('o=steffers.org,o=hosting,dc=weird-web-workers,dc=org')
  108 +puts 'base: ' + site.inspect
  109 +puts '---'
  110 +alias_role.load!('cn=abuse,o=weird-web-workers.org,o=hosting,dc=weird-web-workers,dc=org')
  111 +puts 'base: ' + alias_role.inspect
  112 +puts '---'
  113 +alias_person.load!('mail=georg@steffers.org,o=steffers.org,o=hosting,dc=weird-web-workers,dc=org')
  114 +puts 'base: ' + alias_person.inspect
  115 +puts '---'
  116 +account.load!('mail=drachenfrau@steffers.org,o=steffers.org,o=hosting,dc=weird-web-workers,dc=org')
  117 +puts 'base: ' + account.inspect
  118 +
  119 +puts
40 120 puts 'Memory useage: ' + `ps -o rss= -p #{Process.pid}`
... ...
Please register or login to post a comment