Skip to content

Commit

Permalink
Introduced Class#class_method_proxy in order to make available annota…
Browse files Browse the repository at this point in the history
…ted class methods as private methods
  • Loading branch information
Luca Guidi committed May 22, 2008
1 parent f850c1b commit ae283df
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 40 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG
@@ -1,3 +1,21 @@
* Introduced Class#class_method_proxy in order to make available annotated class methods as private methods

Example:
class Repository
def self.path
@@path
end

class_method_proxy :path
end

Produces:
# Proxy method for <tt>Repository#path</tt>
def path
self.class.path
end
private :path

* Improved tests for repositories system calls


Expand Down
1 change: 1 addition & 0 deletions lib/sashimi.rb
Expand Up @@ -6,6 +6,7 @@
require 'fileutils'
require 'rubygems'
require 'activesupport'
require 'sashimi/core_ext'
require 'sashimi/plugin'
require 'sashimi/repositories'
require 'sashimi/version'
1 change: 1 addition & 0 deletions lib/sashimi/core_ext.rb
@@ -0,0 +1 @@
Dir[File.dirname(__FILE__) + '/core_ext/*.rb'].each{|f| require f}
31 changes: 31 additions & 0 deletions lib/sashimi/core_ext/class.rb
@@ -0,0 +1,31 @@
class Class
# Dinamically creates a proxy for given class methods.
#
# Example:
#
# class Repository
# def self.path
# @@path
# end
#
# class_method_proxy :path
# end
#
# It produces:
# # Proxy method for <tt>Repository#path</tt>
# def path
# self.class.path
# end
# private :path
def class_method_proxy(*method_names)
method_names.each do |m|
self.class_eval %{
# Proxy method for <tt>#{self.class.name}##{m}</tt>
def #{m}(*args)
self.class.#{m}(*args)
end
private :#{m}
}, __FILE__, __LINE__
end
end
end
44 changes: 4 additions & 40 deletions lib/sashimi/repositories/abstract_repository.rb
Expand Up @@ -234,52 +234,16 @@ def remove_temp_folder
FileUtils.rm_rf(plugin.name+'-tmp')
end

class_method_proxy :change_dir, :change_dir_to_local_repository,
:change_dir_to_absolute_plugins_dir, :local_repository_path,
:cache_file, :cache_content, :plugins_dir, :path_to_rails_app

private
# Proxy for <tt>AbstractRepository#change_dir</tt>
def change_dir(dir)
self.class.change_dir(dir)
end

# Proxy for <tt>AbstractRepository#change_dir_to_local_repository</tt>
def change_dir_to_local_repository
self.class.change_dir_to_local_repository
end

# Proxy for <tt>AbstractRepository#change_dir_to_absolute_plugins_dir</tt>
def change_dir_to_absolute_plugins_dir
self.class.change_dir_to_absolute_plugins_dir
end

# Change the current directory with the plugin one
def change_dir_to_plugin_path
change_dir(File.join(local_repository_path, plugin.name || plugin.guess_name))
end

# Proxy for <tt>AbstractRepository#local_repository_path</tt>
def local_repository_path
self.class.local_repository_path
end

# Proxy for <tt>AbstractRepository#cache_file</tt>
def cache_file
self.class.cache_file
end

# Proxy for <tt>AbstractRepository#cache_content</tt>
def cache_content
self.class.cache_content
end

# Proxy for <tt>AbstractRepository#plugins_dir</tt>
def plugins_dir
self.class.plugins_dir
end

# Proxy for <tt>AbstractRepository#path_to_rails_app</tt>
def path_to_rails_app
self.class.path_to_rails_app
end

# Prepare the plugin installation
def prepare_installation
FileUtils.mkdir_p(local_repository_path)
Expand Down
22 changes: 22 additions & 0 deletions test/unit/core_ext/class_test.rb
@@ -0,0 +1,22 @@
require 'test/unit'
require 'test/test_helper'

class Repository
def self.path
@@path ||= 'path'
end

def self.another_path(another_path)
another_path
end

class_method_proxy :path, :another_path
end

class ClassTest < Test::Unit::TestCase
def test_should_respond_to_proxied_methods
repository = Repository.new
assert repository.send(:path)
assert repository.send(:another_path, 'another_path')
end
end

0 comments on commit ae283df

Please sign in to comment.