Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit ed7fb0aa5941a00969d8f63d2b5078ef6ed26ed7 0 parents
Paul Engel authored
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
5 CHANGELOG.rdoc
@@ -0,0 +1,5 @@
+= Unextendable CHANGELOG
+
+== Version 0.1.0 (April 30, 2011)
+
+* Initial release
3  Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Paul Engel
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 README.textile
@@ -0,0 +1,57 @@
+h1. Unextendable
+
+A small gem making unextending extended modules within instances possible
+
+h2. Introduction
+
+A small gem making unextending extended modules within instances possible
+
+h2. Installation
+
+h3. Using Unextendable in Rails 3
+
+Add Unextendable in @Gemfile@ as a gem dependency:
+
+<pre>
+ gem "unextendable"
+</pre>
+
+Run the following in your console to install with Bundler:
+
+<pre>
+ bundle install
+</pre>
+
+h3. Using Unextendable in Rails 2
+
+Add Unextendable in @environment.rb@ as a gem dependency:
+
+<pre>
+ config.gem "unextendable"
+</pre>
+
+Run the following in your console:
+
+<pre>
+ sudo rake gems:install
+</pre>
+
+h2. Getting started
+
+Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+
+h2. Contact me
+
+For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl.
+
+h2. License
+
+Copyright (c) 2011 Paul Engel, released under the MIT license
+
+"http://holder.nl":http://holder.nl – "http://codehero.es":http://codehero.es – "http://gettopup.com":http://gettopup.com – "http://twitter.com/archan937":http://twitter.com/archan937 – "paul.engel@holder.nl":mailto:paul.engel@holder.nl
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 Rakefile
@@ -0,0 +1,10 @@
+require "bundler"
+Bundler::GemHelper.install_tasks
+
+require "rake/testtask"
+Rake::TestTask.new(:test) do |test|
+ test.pattern = "test/**/*_test.rb"
+ test.verbose = true
+end
+
+task :default => :test
1  VERSION
@@ -0,0 +1 @@
+0.1.0
3  lib/unextendable.rb
@@ -0,0 +1,3 @@
+require File.expand_path("../unextendable/module" , __FILE__)
+require File.expand_path("../unextendable/object" , __FILE__)
+require File.expand_path("../unextendable/version", __FILE__)
11 lib/unextendable/module.rb
@@ -0,0 +1,11 @@
+class Module
+
+ def unextendable
+ @unextendable = true
+ end
+
+ def unextendable?
+ !!@unextendable
+ end
+
+end
79 lib/unextendable/object.rb
@@ -0,0 +1,79 @@
+class Object
+
+ def meta_class(&block)
+ class << self
+ yield if block_given?
+ self
+ end
+ end
+ alias :singleton_class :meta_class
+
+ meta_class do
+ def extended_modules
+ @extended_modules ||= []
+ end
+
+ def method_procs
+ @method_procs ||= {}
+ end
+ end
+
+ def extend(*modules)
+ modules.each do |mod|
+ wrap_unextendable_module mod if mod.unextendable?
+ add_extended_module mod
+ super(mod)
+ end
+ end
+
+ def unextend(*modules)
+ if modules.empty?
+ meta_class.extended_modules.delete_if{|mod| mod.unextendable?}
+ else
+ modules.each do |mod|
+ meta_class.extended_modules.delete mod
+ end
+ end
+ end
+
+private
+
+ def wrap_unextendable_module(mod)
+ return unless (mod.class == Module) && mod.unextendable?
+
+ mod.instance_methods.each do |method_name|
+ wrap_unextendable_method method_name
+ end
+ end
+
+ def wrap_unextendable_method(name)
+ return if meta_class.method_procs.key? name
+
+ meta_class.method_procs[name] = method(name).to_proc
+
+ instance_eval <<-CODE
+ def #{name}(*args, &block)
+ call_unextendable_method :#{name}, *args, &block
+ end
+ CODE
+ end
+
+ def add_extended_module(mod)
+ meta_class.extended_modules.delete mod
+ meta_class.extended_modules.unshift mod
+ end
+
+ def call_unextendable_method(method_name, *args, &block)
+ method_for(method_name).call(*args, &block)
+ end
+
+ def method_for(method_name)
+ mod = meta_class.extended_modules.detect{|x| x.instance_methods.include? method_name.to_s}
+ mod ? mod.instance_method(method_name).bind(self) : proc_for(method_name)
+ end
+
+ def proc_for(method_name)
+ meta_class.method_procs[method_name.to_s] || method(method_name.to_s)
+ end
+
+end
3  lib/unextendable/version.rb
@@ -0,0 +1,3 @@
+module Unextendable
+ VERSION = "0.1.0"
+end
2  script/console
@@ -0,0 +1,2 @@
+#!/usr/bin/env ruby
+system "irb -r ./lib/unextendable.rb -r ./script/definitions.rb"
25 script/definitions.rb
@@ -0,0 +1,25 @@
+module A
+ def name
+ "A"
+ end
+end
+
+module U
+ unextendable
+ def name
+ "U"
+ end
+end
+
+class C
+ attr_accessor :title
+ def salutation
+ [title, name].reject{|x| x.nil? || x.empty?}.join " "
+ end
+ def name
+ "C"
+ end
+end
+
+@c = C.new
+@c.title = "Mr."
41 test/module_test.rb
@@ -0,0 +1,41 @@
+require File.expand_path("../test_helper", __FILE__)
+
+class ModuleTest < Test::Unit::TestCase
+
+ context "A module" do
+ setup do
+ module A
+ end
+ end
+
+ should "be able to be marked as unextendable" do
+ assert A.respond_to?(:unextendable)
+ end
+
+ should "respond to :unextendable?" do
+ assert A.respond_to?(:unextendable?)
+ end
+
+ context "which is not unextendable" do
+ should "return false when asked to be unextendable" do
+ assert !A.unextendable?
+ end
+ end
+
+ context "which is unextendable" do
+ setup do
+ module U
+ unextendable
+ def name
+ "U"
+ end
+ end
+ end
+
+ should "return true when asked to be unextendable" do
+ assert U.unextendable?
+ end
+ end
+ end
+
+end
217 test/object_instance_test.rb
@@ -0,0 +1,217 @@
+require File.expand_path("../test_helper", __FILE__)
+
+class ObjectInstanceTest < Test::Unit::TestCase
+
+ context "An object instance" do
+ setup do
+ module A
+ def name
+ "A"
+ end
+ end
+ class C
+ attr_accessor :title
+ def salutation
+ [title, name].reject{|x| x.nil? || x.empty?}.join " "
+ end
+ def name
+ "C"
+ end
+ end
+ @c = C.new
+ @c.title = "Mr."
+ end
+
+ should "respond to meta_class and extended_modules" do
+ assert @c.respond_to?(:meta_class)
+ assert @c.meta_class.respond_to?(:extended_modules)
+ end
+
+ context "when extending modules" do
+ should "check whether a module is unextendable or not" do
+ module B; end
+
+ A.expects(:unextendable?).twice
+ B.expects(:unextendable?)
+
+ @c.extend A
+ @c.extend A, B
+
+ assert @c.meta_class.included_modules.include?(A)
+ assert @c.meta_class.included_modules.include?(B)
+ end
+
+ context "which are not unextendable" do
+ should "behave as normal when the module is not unextendable" do
+ A.expects(:wrap_unextendable_module).never
+ @c.extend A
+ assert @c.meta_class.included_modules.include?(A)
+ assert @c.meta_class.extended_modules.include?(A)
+ end
+
+ should "behave as expected" do
+ assert_equal "Mr. C", @c.salutation
+
+ @c.extend A
+ assert_equal "Mr. A", @c.salutation
+
+ @c.unextend
+ assert_equal "Mr. A", @c.salutation
+
+ @d = C.new
+ @d.title = "Mr."
+ assert_equal "Mr. C", @d.salutation
+
+ @d.extend A
+ assert_equal "Mr. A", @d.salutation
+
+ @d.unextend A
+ assert_equal "Mr. A", @d.salutation
+ end
+ end
+
+ context "which are unextendable" do
+ setup do
+ module U
+ unextendable
+ def name
+ "U"
+ end
+ end
+ end
+
+ should "call wrap_unextendable_module" do
+ @c.expects(:wrap_unextendable_module)
+ @c.extend A, U
+ end
+
+ should "call wrap_unextendable_method" do
+ @c.expects(:wrap_unextendable_method)
+ @c.extend U
+ end
+
+ should "add the module to extended_modules" do
+ assert @c.meta_class.extended_modules.empty?
+ @c.extend U
+ assert @c.meta_class.extended_modules.include?(U)
+ end
+
+ should "add method proc to method_procs" do
+ assert @c.meta_class.send(:method_procs).empty?
+ @c.extend U
+ assert_equal 1, @c.meta_class.send(:method_procs).size
+ end
+
+ context "when calling an unextendable method" do
+ should "call call_unextendable_method" do
+ @c.extend U
+ @c.expects(:call_unextendable_method).with(:name)
+ @c.name
+ end
+
+ should "call method_for" do
+ method = @c.meta_class.instance_method(:name).bind(@c)
+ @c.extend U
+ @c.expects(:method_for).with(:name).returns(method)
+ @c.name
+ end
+
+ should "match the expected method" do
+ assert_equal @c.method(:name), @c.send(:method_for, :name)
+ @c.extend U
+ assert_equal U.instance_method(:name).bind(@c), @c.send(:method_for, :name)
+ end
+
+ should "return the expected value" do
+ assert_equal "Mr. C", @c.salutation
+ @c.extend U
+ assert_equal "Mr. U", @c.salutation
+
+ @d = C.new
+ assert_equal "C", @d.salutation
+ @d.extend U
+ assert_equal "U", @d.salutation
+ end
+ end
+
+ context "when unextending the module afterwards" do
+ should "remove the module from extended_modules" do
+ @c.extend U
+ assert @c.meta_class.extended_modules.include?(U)
+
+ @c.unextend U
+ assert !@c.meta_class.extended_modules.include?(U)
+ end
+
+ context "when calling an unextendable method" do
+ should "match the expected method" do
+ assert_equal @c.method(:name), @c.send(:method_for, :name)
+
+ @c.extend U
+ assert_equal U.instance_method(:name).bind(@c), @c.send(:method_for, :name)
+
+ @c.unextend U
+ assert_equal Proc, @c.send(:method_for, :name).class
+ end
+
+ should "behave as expected" do
+ assert_equal "Mr. C", @c.salutation
+ @c.title = "Dr."
+ assert_equal "Dr. C", @c.salutation
+
+ @c.extend U
+ assert_equal "Dr. U", @c.salutation
+ @c.title = "Sir"
+ assert_equal "Sir U", @c.salutation
+
+ @c.unextend U
+ assert_equal "Sir C", @c.salutation
+ @c.title = ""
+ assert_equal "C", @c.salutation
+
+ @c.extend U
+ assert_equal "U", @c.salutation
+ @c.title = "Ms."
+ assert_equal "Ms. U", @c.salutation
+
+ @c.unextend
+ assert_equal "Ms. C", @c.salutation
+
+ module D
+ unextendable
+ def name
+ "D"
+ end
+ end
+
+ @c.extend D
+ assert_equal "Ms. D", @c.salutation
+
+ @c.extend U
+ assert_equal "Ms. U", @c.salutation
+
+ @c.extend D
+ assert_equal "Ms. D", @c.salutation
+
+ @c.unextend
+ assert_equal "Ms. C", @c.salutation
+
+ @c.extend A
+ assert_equal "Ms. A", @c.salutation
+
+ @c.unextend
+ assert_equal "Ms. A", @c.salutation
+
+ @c.extend D
+ assert_equal "Ms. D", @c.salutation
+
+ @c.unextend
+ assert_equal "Ms. A", @c.salutation
+ end
+ end
+ end
+ end
+ end
+ end
+
+end
7 test/test_helper.rb
@@ -0,0 +1,7 @@
+$:.unshift File.expand_path("../../lib", __FILE__)
+
+require "rubygems"
+require "test/unit"
+require "shoulda"
+require "mocha"
+require "unextendable"
24 unextendable.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "unextendable/version"
+
+Gem::Specification.new do |s|
+ s.name = "unextendable"
+ s.version = Unextendable::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Paul Engel"]
+ s.email = ["paul.engel@holder.nl"]
+ s.homepage = "https://github.com/archan937/unextendable"
+ s.summary = %q{A small gem making unextending extended modules within instances possible}
+ s.description = %q{A small gem making unextending extended modules within instances possible}
+
+ s.rubyforge_project = "unextendable"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "shoulda"
+ s.add_development_dependency "mocha"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.