From b3a6a77e67167f0c3328d47de77d0f5c170a89cf Mon Sep 17 00:00:00 2001
From: Cyril Rohr <cyril.rohr@irisa.fr>
Date: Mon, 16 Mar 2009 18:28:20 +0100
Subject: [PATCH] refactored so that it works with ruby < 1.8.7

---
 generators/lib/core_extensions.rb | 20 +++++++-
 generators/lib/g5k_generator.rb   | 76 +++++++++++++++++++++++--------
 2 files changed, 76 insertions(+), 20 deletions(-)

diff --git a/generators/lib/core_extensions.rb b/generators/lib/core_extensions.rb
index 4b3dceb51a7..62711dc1e84 100755
--- a/generators/lib/core_extensions.rb
+++ b/generators/lib/core_extensions.rb
@@ -91,4 +91,22 @@ class Hash
     result
   end
   
-end
\ No newline at end of file
+end
+
+
+# for versions of ruby < 1.8.7
+module Enumerable
+  def index_by
+    inject({}) do |accum, elem|
+      accum[yield(elem)] = elem
+      accum
+    end
+  end
+
+  def group_by #:yield:
+    #h = k = e = nil
+    r = Hash.new
+    each{ |e| (r[yield(e)] ||= []) << e }
+    r
+  end  
+end
diff --git a/generators/lib/g5k_generator.rb b/generators/lib/g5k_generator.rb
index dceaff19a8e..eac651126c4 100755
--- a/generators/lib/g5k_generator.rb
+++ b/generators/lib/g5k_generator.rb
@@ -63,27 +63,65 @@ class ReferenceGenerator
     Resolv.getaddress(network_address)
   end
   
-  %w{site cluster environment node service}.each do |method|
-    define_method(method) do |uid, *options, &block|
-      key = method.pluralize.to_sym
-      uid = uid.to_s
-      options = options.first || Hash.new
-      old_context = @context
-      @context[key] ||= G5K::Folder.new
-      if options.has_key? :refer_to
-        @context[key] << G5K::Link.new(uid, options[:refer_to])
-      else    
-        # if the same object already exists, we return it for completion/modification
-        if (same_trees = @context[key].select{|tree| tree[:uid] == uid}).size > 0
-          @context = same_trees.first
-        else
-          @context[key] << G5K::Tree.new.replace({:uid => uid, :type => method})
-          @context = @context[key].last
-        end
-        block.call(uid) if block
+  # This doesn't work with Ruby < 1.8.7. Replaced by a call to build_context (see below).
+  #
+  # %w{site cluster environment node service}.each do |method|
+  #   define_method(method) do |uid, *options, &block|
+  #     key = method.pluralize.to_sym
+  #     uid = uid.to_s
+  #     options = options.first || Hash.new
+  #     old_context = @context
+  #     @context[key] ||= G5K::Folder.new
+  #     if options.has_key? :refer_to
+  #       @context[key] << G5K::Link.new(uid, options[:refer_to])
+  #     else    
+  #       # if the same object already exists, we return it for completion/modification
+  #       if (same_trees = @context[key].select{|tree| tree[:uid] == uid}).size > 0
+  #         @context = same_trees.first
+  #       else
+  #         @context[key] << G5K::Tree.new.replace({:uid => uid, :type => method})
+  #         @context = @context[key].last
+  #       end
+  #       block.call(uid) if block
+  #     end
+  #     @context = old_context
+  #   end
+  # end
+  
+  def site(uid, *options, &block)
+    build_context(:sites, uid, *options, &block)
+  end
+  def cluster(uid, *options, &block)
+    build_context(:clusters, uid, *options, &block)
+  end
+  def environment(uid, *options, &block)
+    build_context(:environments, uid, *options, &block)
+  end
+  def node(uid, *options, &block)
+    build_context(:nodes, uid, *options, &block)
+  end
+  def service(uid, *options, &block)
+    build_context(:services, uid, *options, &block)
+  end
+  def build_context(key, uid, *options, &block)
+    type = key.to_s.chop
+    uid = uid.to_s
+    options = options.first || Hash.new
+    old_context = @context
+    @context[key] ||= G5K::Folder.new
+    if options.has_key? :refer_to
+      @context[key] << G5K::Link.new(uid, options[:refer_to])
+    else    
+      # if the same object already exists, we return it for completion/modification
+      if (same_trees = @context[key].select{|tree| tree[:uid] == uid}).size > 0
+        @context = same_trees.first
+      else
+        @context[key] << G5K::Tree.new.replace({:uid => uid, :type => type})
+        @context = @context[key].last
       end
-      @context = old_context
+      block.call(uid) if block
     end
+    @context = old_context
   end
   
   # Initializes a new generator that will generates data files in a hierachical way. 
-- 
GitLab