Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 147 lines (105 sloc) 5.894 kB
d875236 Updated repository structure
Paul Engel authored
1 h1. Unextendable "!https://secure.travis-ci.org/archan937/unextendable.png!":http://travis-ci.org/archan937/unextendable
ed7fb0a Initial commit
Paul Engel authored
2
6f50d3e Updated README.textile
Paul Engel authored
3 A small gem making unextending extended module methods within object instances possible
ed7fb0a Initial commit
Paul Engel authored
4
5 h2. Introduction
6
6f50d3e Updated README.textile
Paul Engel authored
7 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.
8
9 But unfortunately, you cannot just unexclude a module. So after searching the web for solutions, I came across the following:
10
11 *Mixology* - "Github repository":https://github.com/dan-manges/mixology
12
13 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.
14
15 *Evil-ruby* - "Github repository":https://github.com/yugui/evil-ruby
16
a0802ee Released v0.1.0
Paul Engel authored
17 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.
6f50d3e Updated README.textile
Paul Engel authored
18
19 *StatePattern* - "Github repository":https://github.com/dcadenas/state_pattern
20
21 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.
22
8ccdd22 Updated README.textile
Paul Engel authored
23 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.
24
25 h3. Unextendable's advantages
26
27 * It does not require altering your Ruby installation
28 * It should work on every Ruby installation (the tests are running successfully using *Ruby 1.8.7* and *Ruby 1.9.2*)
29 * Implementing Unextendable is straightforward and unobtrusive
ed7fb0a Initial commit
Paul Engel authored
30
31 h2. Installation
32
33 h3. Using Unextendable in Rails 3
34
35 Add Unextendable in @Gemfile@ as a gem dependency:
36
37 <pre>
38 gem "unextendable"
39 </pre>
40
41 Run the following in your console to install with Bundler:
42
43 <pre>
44 bundle install
45 </pre>
46
47 h3. Using Unextendable in Rails 2
48
49 Add Unextendable in @environment.rb@ as a gem dependency:
50
51 <pre>
52 config.gem "unextendable"
53 </pre>
54
55 Run the following in your console:
56
57 <pre>
58 sudo rake gems:install
59 </pre>
60
6f50d3e Updated README.textile
Paul Engel authored
61 h2. Example
62
63 Using Unextendable is pretty straightforward: just extend and unextend modules. A few module and class definitions:
64
65 <pre>
66 require "rubygems"
67 require "unextendable"
68
69 module A
70 def name
71 "A"
72 end
73 end
74 module U
75 unextendable
76 def name
77 "U"
78 end
79 end
80 module X
81 unextendable
82 def name
83 "X"
84 end
85 end
86 class C
87 attr_accessor :title
88 def salutation
89 [title, name].reject{|x| x.nil? || x.empty?}.join " "
90 end
91 def name
92 "C"
93 end
94 end
95 </pre>
96
97 After that, you can do the following:
98
99 <pre>
100 c = C.new
101 c.title = "Mr."
102 c.salutation #=> "Mr. C"
103 c.extend U
104 c.salutation #=> "Mr. U"
105 c.extend X
106 c.salutation #=> "Mr. X"
107 c.extend U
108 c.salutation #=> "Mr. U"
e3b6d17 Released v0.1.1
Paul Engel authored
109 c.unextend U
110 c.salutation #=> "Mr. X"
6f50d3e Updated README.textile
Paul Engel authored
111 c.unextend
112 c.salutation #=> "Mr. C"
113 c.extend A
114 c.salutation #=> "Mr. A"
115 c.extend X
116 c.salutation #=> "Mr. X"
e3b6d17 Released v0.1.1
Paul Engel authored
117 c.unextend do |mod|
118 mod == U
119 end
120 c.salutation #=> "Mr. X"
6f50d3e Updated README.textile
Paul Engel authored
121 c.unextend
122 c.salutation #=> "Mr. A" (because module A is NOT unextendable)
123 </pre>
124
125 h2. Last remarks
126
e3b6d17 Released v0.1.1
Paul Engel authored
127 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.
ed7fb0a Initial commit
Paul Engel authored
128
6f50d3e Updated README.textile
Paul Engel authored
129 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 <code>@c</code> of the class @C@ is also defined.
ed7fb0a Initial commit
Paul Engel authored
130
8ccdd22 Updated README.textile
Paul Engel authored
131 Note: *Unextendable is successfully tested using Ruby 1.8.7 and Ruby 1.9.2*
132
ed7fb0a Initial commit
Paul Engel authored
133 h2. Contact me
134
135 For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl.
136
137 h2. License
138
139 Copyright (c) 2011 Paul Engel, released under the MIT license
140
141 "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
142
143 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:
144
145 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
146
147 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.
Something went wrong with that request. Please try again.