[Nitro] Og: Patch: Manage propertywise empty classes

Aleksi Niemela Aleksi.Niemela at cs.helsinki.fi
Sun Jun 19 06:39:06 EDT 2005


Code like

    class Preference
      # This class is just a placeholder, therefore empty.
      # It's managed, however and needs some keyword to be noticed as such.
   
    #  property :name, String  
   
      managed   # without this keyword, there would be no method primary_key
    end
   
    class User
      # This class is not empty in a sense it will have associations to
      # another class, but at this point it doesn't contain any properties.
   
      # Therefore has_many needs to be defined all the time,
      # not only after first property definition.
   
    #  property :name, String
      has_many :preferences, :Preference
    end

doesn't work currently because

1) User doesn't call property and doesn't get enchanted (with 
RelationMixins), therefore call to has_many doesn't succeed.
2) Neither class gets managed as they don't declare @__props, and if 
they did it would be empty.
3) It's not possible to explicitly declare class to be managed even if 
it's empty.

The patch has two parts. First we have to add "stub methods" to 
bootstrap real meta mixins (and relation mixins) and keyword "managed". 
This could be accomplished along the lines of following first patch. 
Please note you have to at least complete the list of methods which will 
bootstrap enchanting.

The second patch completes the functionality by altering finding of 
classes to manage not to require them to be not empty.  I just added 
comment marker, but I believe the rest of the line should be deleted. 
(Also the patch might not work as I've already altered manager.rb for 
other purposes, but I'm sure you got the idea.)

    - Aleksi



$ diff -u ../../../glue-0.18.1/lib/glue/property.rb~ 
../../../glue-0.18.1/lib/glue/property.rb
--- ../../../glue-0.18.1/lib/glue/property.rb~  2005-06-13 
23:53:15.894375000 +0300
+++ ../../../glue-0.18.1/lib/glue/property.rb   2005-06-19 
13:30:01.343750000 +0300
@@ -385,7 +385,24 @@
                end
        end
        alias_method :property, :prop_accessor
+
+  # Bootstrapping meta mixins from their respective keywords
+  # and forwarding call to real method.
+  ['has_many', 'belongs_to' #, ...
+  ].each do |meta_mixin_name|
+    module_eval %{
+      def #{meta_mixin_name}(*params)
+        Glue::PropertyUtils.enchant(self)
+        Glue::PropertyUtils.include_meta_mixins(self)
+        #{meta_mixin_name}(*params)
+      end
+    }
+  end

+  def managed
+    Glue::PropertyUtils.include_meta_mixins(self)
+  end
+
        # Attach metadata.
        # Guard against duplicates, no need to keep order.
        # This method uses closures :)




$ diff -u manager.rb~ manager.rb
--- manager.rb~ 2005-06-13 23:54:27.535000000 +0300
+++ manager.rb  2005-06-19 13:07:12.890625000 +0300
@@ -122,7 +123,7 @@
                classes = []

                ObjectSpace.each_object(Class) do |c|
-                       if c.respond_to?(:__props) and (!c.__props.empty?)
+                       if c.respond_to?(:__props) # and (!c.__props.empty?)
                                classes << c
                        end
                end



More information about the Nitro-general mailing list