marshally / refactoring_rails

0 stars 0 forks source link

Dynamic method definition pattern #4

Open jpowell opened 9 years ago

jpowell commented 9 years ago

Let's help Marshall prep for India!

jpowell commented 9 years ago
diff --git app/models/git.rb app/models/git.rb
index f47486a..d90bc1f 100644
--- app/models/git.rb
+++ app/models/git.rb
@@ -1,34 +1,20 @@
-class Git
-  def initialize(path)
-    @path = path
-  end
+class Git < Struct.new(:path)
+  extend Forwardable

-  def head
-    repo.head
-  end
+  GIT_OPS = %i[head tags log blame]

-  def tags
-    repo.tags
-  end
+  def_delegators :repo, *GIT_OPS

   def remote_fetch(name)
     repo.remote_fetch(name) unless fetch_disabled?
   end

-  def log(commit = 'master', path = nil, options = {})
-    repo.log(commit, path, options)
-  end
-
   def commit(id)
     Rails.cache.fetch("git.commit(#{id})") do
       repo.commit(id)
     end
   end

-  def blame(file, commit = nil)
-    repo.blame(file, commit)
-  end
-
   def repo
     @repo ||= Grit::Repo.new(@path)
   end
@@ -36,5 +22,4 @@ class Git
   def fetch_disabled?
     false
   end
-
 end