Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Engel committed May 1, 2011
0 parents commit ed7fb0a
Show file tree
Hide file tree
Showing 17 changed files with 512 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
= Unextendable CHANGELOG

== Version 0.1.0 (April 30, 2011)

* Initial release
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -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 changes: 57 additions & 0 deletions README.textile
Original file line number Original file line Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions VERSION
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
3 changes: 3 additions & 0 deletions lib/unextendable.rb
Original file line number Original file line Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions lib/unextendable/module.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,11 @@
class Module

def unextendable
@unextendable = true
end

def unextendable?
!!@unextendable
end

end
79 changes: 79 additions & 0 deletions lib/unextendable/object.rb
Original file line number Original file line Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions lib/unextendable/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
module Unextendable
VERSION = "0.1.0"
end
2 changes: 2 additions & 0 deletions script/console
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env ruby
system "irb -r ./lib/unextendable.rb -r ./script/definitions.rb"
25 changes: 25 additions & 0 deletions script/definitions.rb
Original file line number Original file line Diff line number Diff line change
@@ -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 changes: 41 additions & 0 deletions test/module_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
Loading

0 comments on commit ed7fb0a

Please sign in to comment.