Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/archan937/unextendable

A small gem making unextending extended module methods within object instances possible
https://github.com/archan937/unextendable

Last synced: about 2 months ago
JSON representation

A small gem making unextending extended module methods within object instances possible

Awesome Lists containing this project

README

        

h1. Unextendable "!https://secure.travis-ci.org/archan937/unextendable.png!":http://travis-ci.org/archan937/unextendable

A small gem making unextending extended module methods within object instances possible

h2. Introduction

Unextendable originated from the thought of being able to implement the "State pattern":http://tobyho.com/Design_Patterns_Revisited%3A_State within object instances using modules. In other words: I wanted object instances to behave dependent on their state using modules. I really want to use modules because they are commonly used to define a set of methods which you can extend within an object instance.

But unfortunately, you cannot just unexclude a module. So after searching the web for solutions, I came across the following:

*Mixology* - "Github repository":https://github.com/dan-manges/mixology

A gem that allows objects to effectively mixin and unmix modules. The downside is that it's only implemented for MRI 1.8.x, 1.9.x and JRuby 1.1.x.

*Evil-ruby* - "Github repository":https://github.com/yugui/evil-ruby

A gem that extends Ruby's semantics by accessing its internals from pure Ruby code. I do not want to mess with Ruby internals too much just for what I want to implement. As the name already says, is it ethical or just evil? I will let you judge that. By the way, "evilr":https://github.com/jeremyevans/evilr is its brother written in C.

*StatePattern* - "Github repository":https://github.com/dcadenas/state_pattern

A gem that implements the Ruby state pattern. Ouch! I know what you are thinking: "Dude! Didn't you want to implement the state pattern?". Yes, but as I already mentioned I want to use modules and also, I want to implement it as unobtrusive as possible.

So the gems can do the trick, but slightly did not fit the picture. After some further research on Ruby core classes, I got inspired by "Jay Fields' blog article":http://blog.jayfields.com/2007/08/ruby-calling-methods-of-specific.html and "Facets":https://github.com/rubyworks/facets and wrote this gem.

h3. Unextendable's advantages

* It does not require altering your Ruby installation
* It should work on every Ruby installation (the tests are running successfully using *Ruby 1.8.7* and *Ruby 1.9.2*)
* Implementing Unextendable is straightforward and unobtrusive

h2. Installation

h3. Using Unextendable in Rails 3

Add Unextendable in @Gemfile@ as a gem dependency:


gem "unextendable"

Run the following in your console to install with Bundler:


bundle install

h3. Using Unextendable in Rails 2

Add Unextendable in @environment.rb@ as a gem dependency:


config.gem "unextendable"

Run the following in your console:


sudo rake gems:install

h2. Example

Using Unextendable is pretty straightforward: just extend and unextend modules. A few module and class definitions:


require "rubygems"
require "unextendable"

module A
def name
"A"
end
end
module U
unextendable
def name
"U"
end
end
module X
unextendable
def name
"X"
end
end
class C
attr_accessor :title
def salutation
[title, name].reject{|x| x.nil? || x.empty?}.join " "
end
def name
"C"
end
end

After that, you can do the following:


c = C.new
c.title = "Mr."
c.salutation #=> "Mr. C"
c.extend U
c.salutation #=> "Mr. U"
c.extend X
c.salutation #=> "Mr. X"
c.extend U
c.salutation #=> "Mr. U"
c.unextend U
c.salutation #=> "Mr. X"
c.unextend
c.salutation #=> "Mr. C"
c.extend A
c.salutation #=> "Mr. A"
c.extend X
c.salutation #=> "Mr. X"
c.unextend do |mod|
mod == U
end
c.salutation #=> "Mr. X"
c.unextend
c.salutation #=> "Mr. A" (because module A is NOT unextendable)

h2. Last remarks

Please check out "https://github.com/archan937/unextendable/blob/master/test/object_instance_test.rb":https://github.com/archan937/unextendable/blob/master/test/object_instance_test.rb for most of the tests available. You can run the unit tests with @rake@ within the terminal.

Also, the Unextendable repo is provided with @script/console@ which you can run for testing purposes. The module and class definitions are already defined when starting up and the object instance @c of the class @C@ is also defined.

Note: *Unextendable is successfully tested using Ruby 1.8.7 and Ruby 1.9.2*

h2. Contact me

For support, remarks and requests please mail me at "[email protected]":mailto:[email protected].

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 – "[email protected]":mailto:[email protected]

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.