From 2dd4b16b6f83038e10407f09946641a0e0c680f4 Mon Sep 17 00:00:00 2001
From: Roel van Dijk <roel@finalist.com>
Date: Thu, 20 Sep 2012 15:23:12 +0200
Subject: [PATCH] Add path helpers as class methods.

---
 lib/neography/rest/batch.rb                   |  2 +-
 lib/neography/rest/clean.rb                   |  2 +-
 lib/neography/rest/node_auto_indexes.rb       |  2 +-
 lib/neography/rest/node_indexes.rb            |  2 +-
 lib/neography/rest/node_paths.rb              |  2 +-
 lib/neography/rest/node_properties.rb         |  2 +-
 lib/neography/rest/node_relationships.rb      |  2 +-
 lib/neography/rest/node_traversal.rb          |  2 +-
 lib/neography/rest/nodes.rb                   |  2 +-
 lib/neography/rest/paths.rb                   | 31 +++++-----
 .../rest/relationship_auto_indexes.rb         |  2 +-
 lib/neography/rest/relationship_indexes.rb    |  2 +-
 lib/neography/rest/relationship_properties.rb |  2 +-
 lib/neography/rest/relationships.rb           |  2 +-
 spec/unit/rest/paths_spec.rb                  | 58 ++++++++++++++-----
 15 files changed, 74 insertions(+), 41 deletions(-)

diff --git a/lib/neography/rest/batch.rb b/lib/neography/rest/batch.rb
index 445ad0e..56eb709 100644
--- a/lib/neography/rest/batch.rb
+++ b/lib/neography/rest/batch.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class Batch
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :batch, "/batch"
diff --git a/lib/neography/rest/clean.rb b/lib/neography/rest/clean.rb
index d0ea00c..633094b 100644
--- a/lib/neography/rest/clean.rb
+++ b/lib/neography/rest/clean.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class Clean
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :clean, "/cleandb/secret-key"
diff --git a/lib/neography/rest/node_auto_indexes.rb b/lib/neography/rest/node_auto_indexes.rb
index 0491a68..1bb387a 100644
--- a/lib/neography/rest/node_auto_indexes.rb
+++ b/lib/neography/rest/node_auto_indexes.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodeAutoIndexes < AutoIndexes
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
 
       add_path :key_value,        "/index/auto/node/:key/:value"
       add_path :query_index,      "/index/auto/node/?query=:query"
diff --git a/lib/neography/rest/node_indexes.rb b/lib/neography/rest/node_indexes.rb
index c4d8429..e1bf49f 100644
--- a/lib/neography/rest/node_indexes.rb
+++ b/lib/neography/rest/node_indexes.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodeIndexes < Indexes
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :all,       "/index/node"
diff --git a/lib/neography/rest/node_paths.rb b/lib/neography/rest/node_paths.rb
index 1ba62b0..c18abbc 100644
--- a/lib/neography/rest/node_paths.rb
+++ b/lib/neography/rest/node_paths.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodePaths
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :base, "/node/:id/path"
diff --git a/lib/neography/rest/node_properties.rb b/lib/neography/rest/node_properties.rb
index 8c7806f..e09dcc1 100644
--- a/lib/neography/rest/node_properties.rb
+++ b/lib/neography/rest/node_properties.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodeProperties < Properties
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
 
       add_path :all,    "/node/:id/properties"
       add_path :single, "/node/:id/properties/:property"
diff --git a/lib/neography/rest/node_relationships.rb b/lib/neography/rest/node_relationships.rb
index 2959d11..57240d3 100644
--- a/lib/neography/rest/node_relationships.rb
+++ b/lib/neography/rest/node_relationships.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodeRelationships
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :base,       "/node/:id/relationships"
diff --git a/lib/neography/rest/node_traversal.rb b/lib/neography/rest/node_traversal.rb
index b6d6946..20f5ca4 100644
--- a/lib/neography/rest/node_traversal.rb
+++ b/lib/neography/rest/node_traversal.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class NodeTraversal
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :traversal, "/node/:id/traverse/:type"
diff --git a/lib/neography/rest/nodes.rb b/lib/neography/rest/nodes.rb
index 9f2cacb..7dc1e9f 100644
--- a/lib/neography/rest/nodes.rb
+++ b/lib/neography/rest/nodes.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class Nodes
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :index,  "/node"
diff --git a/lib/neography/rest/paths.rb b/lib/neography/rest/paths.rb
index bd25b59..32a0e76 100644
--- a/lib/neography/rest/paths.rb
+++ b/lib/neography/rest/paths.rb
@@ -2,8 +2,23 @@ module Neography
   class Rest
     module Paths
 
-      def self.included(mod)
-        mod.send :extend, ClassMethods
+      def add_path(key, path)
+        method_name = :"#{key}_path"
+
+        metaclass = (class << self; self; end)
+        metaclass.instance_eval do
+          define_method method_name do |*attributes|
+            if attributes.any?
+              build_path(path, *attributes)
+            else
+              path
+            end
+          end
+        end
+
+        define_method method_name do |*attributes|
+          self.class.send(method_name, *attributes)
+        end
       end
 
       def build_path(path, attributes)
@@ -16,18 +31,6 @@ def encode(value)
         URI.encode(value).gsub("/","%2F")
       end
 
-      module ClassMethods
-        def add_path(key, path)
-          define_method :"#{key}_path" do |*attributes|
-            if attributes.any?
-              build_path(path, *attributes)
-            else
-              path
-            end
-          end
-        end
-      end
-
     end
   end
 end
diff --git a/lib/neography/rest/relationship_auto_indexes.rb b/lib/neography/rest/relationship_auto_indexes.rb
index 0594a15..69745df 100644
--- a/lib/neography/rest/relationship_auto_indexes.rb
+++ b/lib/neography/rest/relationship_auto_indexes.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class RelationshipAutoIndexes < AutoIndexes
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
 
       add_path :key_value,        "/index/auto/relationship/:key/:value"
       add_path :query_index,      "/index/auto/relationship/?query=:query"
diff --git a/lib/neography/rest/relationship_indexes.rb b/lib/neography/rest/relationship_indexes.rb
index 872b37a..b0a2aa6 100644
--- a/lib/neography/rest/relationship_indexes.rb
+++ b/lib/neography/rest/relationship_indexes.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class RelationshipIndexes < Indexes
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :all,       "/index/relationship"
diff --git a/lib/neography/rest/relationship_properties.rb b/lib/neography/rest/relationship_properties.rb
index 324d250..c45b979 100644
--- a/lib/neography/rest/relationship_properties.rb
+++ b/lib/neography/rest/relationship_properties.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class RelationshipProperties < Properties
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
 
       add_path :all,    "/relationship/:id/properties"
       add_path :single, "/relationship/:id/properties/:property"
diff --git a/lib/neography/rest/relationships.rb b/lib/neography/rest/relationships.rb
index 43cfa17..265ed95 100644
--- a/lib/neography/rest/relationships.rb
+++ b/lib/neography/rest/relationships.rb
@@ -1,7 +1,7 @@
 module Neography
   class Rest
     class Relationships
-      include Neography::Rest::Paths
+      extend Neography::Rest::Paths
       include Neography::Rest::Helpers
 
       add_path :base,   "/relationship/:id"
diff --git a/spec/unit/rest/paths_spec.rb b/spec/unit/rest/paths_spec.rb
index a30bfae..a5aa79f 100644
--- a/spec/unit/rest/paths_spec.rb
+++ b/spec/unit/rest/paths_spec.rb
@@ -4,7 +4,7 @@ module Neography
   class Rest
 
     class Dummy
-      include Paths
+      extend Paths
 
       add_path :one, "/node/:id"
       add_path :two, "/node/:id/properties/:property"
@@ -12,24 +12,54 @@ class Dummy
 
     describe Dummy do
 
-      it { should respond_to(:one_path) }
-      it { should respond_to(:two_path) }
+      context "instance methods" do
 
-      it "replaces a key" do
-        subject.one_path(:id => 42).should == "/node/42"
-      end
+        it { should respond_to(:one_path) }
+        it { should respond_to(:two_path) }
 
-      it "replaces multiple keys" do
-        subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
-      end
+        it "replaces a key" do
+          subject.one_path(:id => 42).should == "/node/42"
+        end
+
+        it "replaces multiple keys" do
+          subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
+        end
+
+        it "url encodes spaces" do
+          subject.one_path(:id => "with space").should == "/node/with%20space"
+        end
+
+        # URI.encode does not escape slashes (and rightly so), but should escape these keys
+        it "url encodes slashes" do
+          subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
+        end
 
-      it "url encodes spaces" do
-        subject.one_path(:id => "with space").should == "/node/with%20space"
       end
 
-      # URI.encode does not escape slashes (and rightly so), but should escape these keys
-      it "url encodes slashes" do
-        subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
+      context "class methods" do
+
+        subject { Dummy }
+
+        it { should respond_to(:one_path) }
+        it { should respond_to(:two_path) }
+
+        it "replaces a key" do
+          subject.one_path(:id => 42).should == "/node/42"
+        end
+
+        it "replaces multiple keys" do
+          subject.two_path(:id => 42, :property => "foo").should == "/node/42/properties/foo"
+        end
+
+        it "url encodes spaces" do
+          subject.one_path(:id => "with space").should == "/node/with%20space"
+        end
+
+        # URI.encode does not escape slashes (and rightly so), but should escape these keys
+        it "url encodes slashes" do
+          subject.one_path(:id => "with/slash").should == "/node/with%2Fslash"
+        end
+
       end
 
     end