Unextendable

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

Introduction

Unextendable originated from the thought of being able to implement the State pattern 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:

MixologyGithub repository

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.×.

Evil-rubyGithub repository

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 is its brother written in C.

StatePatternGithub repository

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 and Facets and wrote this gem.

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

Installation

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

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

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)

Last remarks

Please check out 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

Contact me

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

License

Copyright © 2011 Paul Engel, released under the MIT license

http://holder.nlhttp://codehero.eshttp://gettopup.comhttp://twitter.com/archan937[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.