commit 5b1a634277e5a566517b145c19b8fe3f07ee686d
Author: Marc Dequènes (Duck) <Duck@DuckCorp.org>
Date:   Sun Oct 25 01:36:11 2009 +0200

    [cleanup] improved a bit aspect handling

diff --git a/bin/shadowwalker b/bin/shadowwalker
index 7a5f375..5b73b30 100755
--- a/bin/shadowwalker
+++ b/bin/shadowwalker
@@ -187,10 +187,9 @@ class ListCommand < Command
             puts "  - #{str}"
           end
           obj_klass.possible_aspects.each do |aspect|
-            aspect_human_name = Translator.translate_aspect_name(aspect)
-            list = obj_klass.possible_attributes_for_aspect(aspect)
+            aspect_human_name = Translator.translate_aspect_name(aspect.handle)
             puts "--- Sublist of LDAP Fields for Aspect #{aspect_human_name} (#{list.size}) ---"
-            list.each do |field|
+            aspect.possible_attributes.each do |field|
               str = Translator.translate_field_name(field)
               str += " [#{field}]" if $program_options[:handles]
               puts "  - #{str}"
diff --git a/lib/ldap_shadows/elements/aspect.rb b/lib/ldap_shadows/elements/aspect.rb
index 85bc9a4..c549e9b 100644
--- a/lib/ldap_shadows/elements/aspect.rb
+++ b/lib/ldap_shadows/elements/aspect.rb
@@ -33,6 +33,10 @@ module Elements
         end
       end
     end
+
+    def self.possible_attributes
+      ActiveLdap::Base.objectclasses_attr_list(self.parameters[:mapping][:classes])
+    end
   end
 end
 end
diff --git a/lib/ldap_shadows/elements/object.rb b/lib/ldap_shadows/elements/object.rb
index e3d5c68..f5f431b 100755
--- a/lib/ldap_shadows/elements/object.rb
+++ b/lib/ldap_shadows/elements/object.rb
@@ -57,8 +57,7 @@ module Elements
         if aspect.nil?
           raise PreProcessingError, _("Aspect '%s' is missing for object '%s'") % [aspect_name, self.handle]
         end
-        aspect_data = aspect.parameters
-        object_rel.merge!(aspect_data[:relations])
+        object_rel.merge!(aspect.parameters[:relations])
       end
       return if object_rel.empty?
 
@@ -129,11 +128,8 @@ module Elements
 
       rel_list += self.class.parameters[:presentation][:associated_relations] if self.class.parameters[:presentation][:associated_relations]
 
-      aspects.each do |aspect|
-        aspect_data = self.class.shadow.get_aspect(aspect).parameters
-        if defined?(aspect_data[:presentation][:associated_relations]) and aspect_data[:presentation][:associated_relations]
-          rel_list += aspect_data[:presentation][:associated_relations]
-        end
+      aspects.values.each do |aspect|
+        rel_list += aspect.parameters[:presentation][:associated_relations]
       end
 
       rel_list & possible_relations
@@ -144,20 +140,16 @@ module Elements
     end
 
     def aspects
-      present_aspects = []
-      self.class.parameters[:presentation][:allowed_aspects].each do |aspect|
-        aspect_data = self.class.shadow.get_aspect(aspect).parameters
-        aspect_mapping = aspect_data[:mapping]
-        present_aspects << aspect if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
+      present_aspects = {}
+      self.class.parameters[:presentation][:allowed_aspects].each do |aspect_name|
+        aspect = self.class.shadow.get_aspect(aspect_name)
+        aspect_mapping = aspect.parameters[:mapping]
+        present_aspects[aspect.handle] = aspect if self.classes & aspect_mapping[:classes] == aspect_mapping[:classes]
       end
 
       present_aspects
     end
 
-    def self.possible_attributes_for_aspect(aspect)
-      self.objectclasses_attr_list(self.shadow.get_aspect(aspect).parameters[:mapping][:classes])
-    end
-
     def info_for_relation(rel)
       return nil unless self.relations.include?(rel)
 
@@ -174,28 +166,27 @@ module Elements
         ActiveLdap::Base.schema.attribute(attr).operational?
       end
 
-      aspects = self.aspects
       rel_list = self.possible_relations
 
       # first pass to take aspects forced relations into account
       obj_aspects = {}
-      aspects.each do |aspect|
-        aspect_data = self.class.shadow.get_aspect(aspect).parameters
+      self.aspects.values.each do |aspect|
+        aspect_data = aspect.parameters
 
-        if defined?(aspect_data[:presentation][:associated_attributes]) and aspect_data[:presentation][:associated_attributes]
+        unless aspect_data[:presentation][:associated_attributes].empty?
           taken_attr_list = aspect_data[:presentation][:associated_attributes] & (attr_list + ignored_attrs)
           unless taken_attr_list.empty?
-            obj_aspects[aspect] ||= {}
-            obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
+            obj_aspects[aspect.handle] ||= {}
+            obj_aspects[aspect.handle].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
             attr_list -= taken_attr_list
           end
         end
 
-        if defined?(aspect_data[:presentation][:associated_relations]) and aspect_data[:presentation][:associated_relations]
+        unless aspect_data[:presentation][:associated_relations].empty?
           taken_rel_list = aspect_data[:presentation][:associated_relations] & rel_list
           unless taken_rel_list.empty?
-            obj_aspects[aspect] ||= {}
-            obj_aspects[aspect].merge!(fetch_relations_data(taken_rel_list, expert_attributes))
+            obj_aspects[aspect.handle] ||= {}
+            obj_aspects[aspect.handle].merge!(fetch_relations_data(taken_rel_list, expert_attributes))
             rel_list -= taken_rel_list
           end
         end
@@ -225,10 +216,10 @@ module Elements
 
       # second pass to dispath the remaining attributes
       unless attr_list.empty?
-        aspects.each do |aspect|
-          taken_attr_list = (self.class.possible_attributes_for_aspect(aspect) & attr_list)
-          obj_aspects[aspect] ||= {}
-          obj_aspects[aspect].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
+        self.aspects.values.each do |aspect|
+          taken_attr_list = (aspect.possible_attributes & attr_list)
+          obj_aspects[aspect.handle] ||= {}
+          obj_aspects[aspect.handle].merge!(fetch_attributes_data(taken_attr_list, expert_attributes, admin_attributes))
           attr_list -= taken_attr_list
 
           break if attr_list.empty?
@@ -378,9 +369,9 @@ module Elements
       true
     end
 
-    def item_modify_aspects(op, aspect)
-      unless self.class.possible_aspects.include?(aspect)
-        raise PreProcessingError, _("No such aspect '%s' for object '%s'") % [aspect, self.class.handle]
+    def item_modify_aspects(op, aspect_name)
+      unless self.class.possible_aspects.include?(aspect_name)
+        raise PreProcessingError, _("No such aspect '%s' for object '%s'") % [aspect_name, self.class.handle]
       end
 
       case op
@@ -388,12 +379,12 @@ module Elements
         raise PreProcessingError, _("The equality operator is not possible for aspects")
 
       when '+='
-        return false if self.aspects.include?(aspect)
+        return false if self.aspects.keys.include?(aspect)
 
         self.add_aspect(aspect)
 
       when '-='
-        return false unless self.aspects.include?(aspect)
+        return false unless self.aspects.keys.include?(aspect)
 
         self.remove_aspect(aspect)
 
@@ -486,9 +477,8 @@ module Elements
       ldap_search_parts = val_list.split(",").collect do |val|
         aspect = shadow.get_aspect(val)
         raise PreProcessingError, _("No such aspect '%s'") % val if aspect.nil?
-        aspect_data = aspect.parameters
 
-        ldap_classes = aspect_data[:mapping][:classes]
+        ldap_classes = aspect.parameters[:mapping][:classes]
 
         case op
         when "="
@@ -530,23 +520,23 @@ module Elements
       end
     end
 
-    def add_aspect(aspect)
-      return unless self.class.possible_aspects.include?(aspect)
+    def add_aspect(aspect_name)
+      return unless self.class.possible_aspects.include?(aspect_name)
 
-      aspect_data = self.class.shadow.get_aspect(aspect).parameters
-      add_class(*aspect_data[:mapping][:classes])
+      aspect_mapping = self.class.shadow.get_aspect(aspect_name).parameters[:mapping]
+      add_class(*aspect_mapping[:classes])
 
       # recursive dependency enforcement
-      aspect_data[:mapping][:depend_aspects].each do |dep_aspect|
+      aspect_mapping[:depend_aspects].each do |dep_aspect|
         add_aspect(dep_aspect)
       end
     end
 
-    def remove_aspect(aspect)
-      return unless self.class.possible_aspects.include?(aspect)
+    def remove_aspect(aspect_name)
+      return unless self.class.possible_aspects.include?(aspect_name)
 
-      aspect_data = self.class.shadow.get_aspect(aspect).parameters
-      remove_class(*aspect_data[:mapping][:classes])
+      aspect_mapping = self.class.shadow.get_aspect(aspect_name).parameters[:mapping]
+      remove_class(*aspect_mapping[:classes])
     end
 
     def delete(options = {})
