commit bbe8951879f7c0b7b74df85f94c7de5b712cf78c
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Sun Oct 18 18:16:24 2009 +0200

    [cleanup] internal rework #1

diff --git a/bin/shadowwalker b/bin/shadowwalker
index 8885605..b1a82ad 100755
--- a/bin/shadowwalker
+++ b/bin/shadowwalker
@@ -70,7 +70,7 @@ class Command < CmdParse::Command
     config = LdapShadows::Config.instance
     config.load_global_config([File.join(ENV['HOME'], ".shadowwalker")])
     begin
-      $ldapctl = config.load_shadow($program_options[:shadow_name])
+      $shadow = config.load_shadow($program_options[:shadow_name])
     rescue ActiveLdap::Error => e
       STDERR.puts _("LDAP connection error: %s") % e.to_s
       exit 2
@@ -101,7 +101,7 @@ class Command < CmdParse::Command
     raise SyntaxError, _("no object name given") if args.empty?
 
     obj_hdl = args.shift.downcase.singularize
-    obj_klass = $ldapctl.find_klass(obj_hdl)
+    obj_klass = $shadow.find_klass(obj_hdl)
     raise PreProcessingError, _("No such object '%s'") % obj_hdl if obj_klass.nil?
     obj_klass
   end
@@ -143,7 +143,7 @@ class ListCommand < Command
     obj_hdl = args.shift.downcase.singularize
     case obj_hdl
     when ':object'
-      list = $ldapctl.objects
+      list = $shadow.objects
       puts "=== List of LDAP objects (#{list.size}) ==="
       list.each do |obj_name|
         puts "  - #{obj_name}"
@@ -320,10 +320,10 @@ class TreeCommand < Command
       end
     else
       dn_list = []
-      sconfig = $ldapctl.get_shadow_config()
+      sconfig = $shadow.get_config()
 
-      (sconfig[:tree_objects] || []).each do |obj_hdl|
-        obj_klass = $ldapctl.find_klass(obj_hdl.downcase)
+      (sconfig[:presentation][:tree_objects] || []).each do |obj_hdl|
+        obj_klass = $shadow.find_klass(obj_hdl.downcase)
         unless obj_klass
           raise PreProcessingError, _("Location object '%s' not defined") % obj_hdl
         end
@@ -334,7 +334,7 @@ class TreeCommand < Command
       end
     end
 
-    base_dn = ActiveLdap::DistinguishedName.parse(LdapObject.base)
+    base_dn = ActiveLdap::DistinguishedName.parse(Elements::LdapObject.base)
 
     tree = {}
     dn_list.each do |dn|
@@ -466,11 +466,11 @@ class SearchCommand < Command
 
     raise SyntaxError, _("no search arguments") if args.empty?
 
-    res = LdapObject.items_find_from_strings(args)
+    res = Elements::LdapObject.items_find_from_strings($shadow, args)
     unless res.empty?
       display_lines = []
       res.each do |raw_item|
-        display_lines << LdapObject.raw_item_info(raw_item)[:name]
+        display_lines << Elements::LdapObject.raw_item_info($shadow, raw_item)[:name]
       end
       puts display_lines.join("\n")
     end
diff --git a/lib/ldap_shadows/config.rb b/lib/ldap_shadows/config.rb
index 5f5e5e5..5bae69b 100644
--- a/lib/ldap_shadows/config.rb
+++ b/lib/ldap_shadows/config.rb
@@ -83,7 +83,7 @@ module LdapShadows
         :config_path => shadow_config_path,
         :shadow => shadow
       }
-      shadow.set_shadow_config(config)
+      shadow.set_config(config)
 
       load_config_components(shadow_config_path, 'aspects') do |c_name, c_config|
         shadow.add_aspect(c_name, c_config)
diff --git a/lib/ldap_shadows/elements/object.rb b/lib/ldap_shadows/elements/object.rb
index 368fe17..21ba1ea 100755
--- a/lib/ldap_shadows/elements/object.rb
+++ b/lib/ldap_shadows/elements/object.rb
@@ -136,7 +136,7 @@ module Elements
     end
 
     def organized_data
-      ignored_attrs = self.class.shadow.get_shadow_config[:hidden_attributes] || []
+      ignored_attrs = self.class.shadow.get_config[:presentation][:hidden_attributes] || []
       ignored_attrs += self.class.presentation[:hidden_attributes] || []
       attr_list = self.nonempty_attributes - ignored_attrs
 
@@ -377,7 +377,7 @@ module Elements
       true
     end
 
-    def self.items_find_from_strings(str_list)
+    def self.items_find_from_strings(shadow, str_list)
       ldap_search_objects = "(objectClass=*)"
       ldap_search_aspects = ""
       ldap_search_fields = []
@@ -404,9 +404,9 @@ module Elements
         when ''
           case field
           when 'objects'
-            ldap_search_objects = ldap_search_string_objects(field, op, val)
+            ldap_search_objects = ldap_search_string_objects(shadow, field, op, val)
           when 'aspects'
-            ldap_search_aspects = ldap_search_string_aspects(field, op, val)
+            ldap_search_aspects = ldap_search_string_aspects(shadow, field, op, val)
           else
             raise PreProcessingError, _("Unknown core field '%s'") % field
           end
@@ -433,10 +433,10 @@ module Elements
       end
     end
 
-    def self.ldap_search_string_objects(field, op, val_list)
+    def self.ldap_search_string_objects(shadow, field, op, val_list)
       ldap_search_parts = val_list.split(",").collect do |val|
         obj_hdl = val.downcase.singularize
-        obj_klass = $ldapctl.find_klass(obj_hdl)
+        obj_klass = shadow.find_klass(obj_hdl)
         raise PreProcessingError, _("No such object '%s'") % val if obj_klass.nil?
 
         ldap_classes = obj_klass.required_classes
@@ -455,9 +455,9 @@ module Elements
       "(|" + ldap_search_parts.join + ")"
     end
 
-    def self.ldap_search_string_aspects(field, op, val_list)
+    def self.ldap_search_string_aspects(shadow, field, op, val_list)
       ldap_search_parts = val_list.split(",").collect do |val|
-        aspect_data = self.class.shadow.get_aspect(val)
+        aspect_data = shadow.get_aspect(val)
         raise PreProcessingError, _("No such aspect '%s'") % val if aspect_data.nil?
 
         ldap_classes = aspect_data[:mapping][:classes]
@@ -476,19 +476,19 @@ module Elements
       "(&" + ldap_search_parts.join + ")"
     end
 
-    def self.find_raw_item_object(raw_item)
-      self.class.shadow.objects.each do |obj_hdl|
-        obj_klass = $ldapctl.find_klass(obj_hdl)
+    def self.find_raw_item_object(shadow, raw_item)
+      shadow.objects.each do |obj_hdl|
+        obj_klass = shadow.find_klass(obj_hdl)
         ldap_classes = obj_klass.required_classes
         return obj_hdl if raw_item.classes & ldap_classes == ldap_classes
       end
       nil
     end
 
-    def self.raw_item_info(raw_item)
-      obj_hdl = self.find_raw_item_object(raw_item)
+    def self.raw_item_info(shadow, raw_item)
+      obj_hdl = self.find_raw_item_object(shadow, raw_item)
       if obj_hdl
-        obj_klass = $ldapctl.find_klass(obj_hdl)
+        obj_klass = shadow.find_klass(obj_hdl)
         item = obj_klass.new(raw_item.dn)
         {:name => "#{obj_hdl}/#{item.name}", :item => item, :object => obj_klass}
       else
diff --git a/lib/ldap_shadows/shadow.rb b/lib/ldap_shadows/shadow.rb
index 531ea97..fed42b1 100755
--- a/lib/ldap_shadows/shadow.rb
+++ b/lib/ldap_shadows/shadow.rb
@@ -49,22 +49,21 @@ module LdapShadows
       @aspects = {}
     end
 
-    def set_shadow_config(shadow_def_raw)
+    def set_config(shadow_def_raw)
       @shadow_config = @config.parse_and_validate(@name, 'shadow', shadow_def_raw)
     end
 
-    def get_shadow_config
-      # TODO: remove this workaround
-      @shadow_config[:presentation]
+    def get_config
+      @shadow_config
     end
 
     def add_aspect(aspect_name, aspect_def_raw)
       e_type = 'aspect'
 
-      klass = add_container_element(e_type, aspect_name)
-      aspect_def = @config.parse_and_validate(aspect_name, e_type, aspect_def_raw)
+      klass = add_container_element(e_type, aspect_name, aspect_def_raw)
 
       # TODO: remove this crap
+      aspect_def = @config.parse_and_validate(aspect_name, e_type, aspect_def_raw)
       @aspects[aspect_name.to_sym] = aspect_def
     end
 
@@ -79,12 +78,12 @@ module LdapShadows
     def add_object(object_name, object_def_raw)
       e_type = 'object'
 
-      klass = add_container_element(e_type, object_name)
-      object_def = @config.parse_and_validate(object_name, 'object', object_def_raw)
-      klass.presentation = object_def[:presentation]
-      klass.ldap_mapping object_def[:mapping]
+      klass = add_container_element(e_type, object_name, object_def_raw)
 
       # TODO: remove this crap
+      object_def = @config.parse_and_validate(object_name, e_type, object_def_raw)
+      klass.presentation = object_def[:presentation]
+      klass.ldap_mapping object_def[:mapping]
       @object_definitions[object_name.to_sym] = object_def
     end
 
@@ -147,7 +146,7 @@ module LdapShadows
 
     protected
 
-    def add_container_element(e_type, e_name)
+    def add_container_element(e_type, e_name, e_def_raw)
       superklass_location = LdapShadows::Elements
       superklass_name = 'Ldap' + e_type.classify
       unless superklass_location.const_defined?(superklass_name)
@@ -159,16 +158,19 @@ module LdapShadows
         raise PreProcessingError, _("Element '%s' for model '%s' as already been defined") % [e_name, e_type]
       end
 
-      begin
-        klass = @container.module_eval(<<-EOS)
-          class #{klass_name} < #{superklass_location}::#{superklass_name}; self; end
-        EOS
-        klass.instance_variable_set(:@handle, e_name)
-        klass.instance_variable_set(:@shadow, self)
-        klass.meta_eval do
-          attr_reader :handle, :shadow
-        end
+      klass = @container.module_eval(<<-EOS)
+        class #{klass_name} < #{superklass_location}::#{superklass_name}; self; end
+      EOS
+      klass.instance_variable_set(:@handle, e_name)
+      klass.instance_variable_set(:@shadow, self)
+      klass.meta_eval do
+        attr_reader :handle, :shadow
+      end
+
+      e_def = @config.parse_and_validate(e_name, e_type, e_def_raw)
+      klass.instance_variable_set(:@parameters, e_def)
 
+      begin
         klass_content = @config.load_hook_content(@name, e_type, e_name)
         klass.class_eval(klass_content) unless klass_content.nil?
       rescue
@@ -179,6 +181,8 @@ module LdapShadows
       @container_elements[e_type][e_name] = klass
 
       klass
+    rescue
+      raise PreProcessingError, _("Could not create element '%s' for model '%s': %s") % [e_type, e_name, $!]
     end
   end
 end
