overider 0.0.1 → 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.mkd CHANGED
@@ -13,15 +13,18 @@ but may become a problem for larger projects.
13
13
  In the [same post](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html),
14
14
  Jay offered an interesting alternative to `alias`,
15
15
  using instead `Module#instance_method` and `Module#define_method`.
16
- This is much better,
17
- but I'm not a Ruby ninja yet,
18
- and I would rather not have to remember the Baroque syntax in `x = self.instance_method(:on!)`
19
- and `x.bind(self).call` every time I want to do this,
20
- let alone what they mean when I look at my code several weeks later.
16
+ This is much better, but I'm not quite satisfied.
17
+ While obvious enough to experienced Ruby-hands,
18
+ people who are newer to Ruby (like me!)
19
+ might have trouble remembering the syntax of `x = self.instance_method(:x)`
20
+ and `x.bind(self).call` every time they need to use this.
21
+ What I'd really like is a way to override defined methods that's a no-brainer.
21
22
 
22
23
  So I thought about how I could take Jay's example one step further
23
24
  and after some trial and lots of error came up with this.
24
25
 
26
+ Thanks and credit go to [Jay Fields](http://blog.jayfields.com/) for his post.
27
+
25
28
  ## Description
26
29
  A mix-in module that allows for super-clean method over-riding without resorting to `alias`
27
30
  or making unbound methods visible.
@@ -48,3 +51,7 @@ or making unbound methods visible.
48
51
  ## See also
49
52
  * [Ruby: Alias method alternative, Jay Fields](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html)
50
53
  * <https://github.com/soveran/override>
54
+
55
+ ## LICENSE
56
+ * [Ruby License](http://www.ruby-lang.org/en/LICENSE.txt)
57
+ * <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">overider.gem</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://westside-consulting.com/" property="cc:attributionName" rel="cc:attributionURL">Lawrence Siden</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="https://github.com/lsiden" rel="dct:source">github.com</a>.
@@ -5,11 +5,9 @@ module Overider
5
5
  orig_unbound_method = self.instance_method(sym)
6
6
 
7
7
  define_method(sym) do |*a, &blk|
8
- orig_self = self
9
- obj = Object.new
10
8
 
11
- obj.define_singleton_method(:overiden) do |*a, &blk|
12
- orig_bound_method = orig_unbound_method.bind(orig_self)
9
+ self.define_singleton_method(:overiden) do |*a, &blk|
10
+ orig_bound_method = orig_unbound_method.bind(self)
13
11
 
14
12
  if !!blk then
15
13
  orig_bound_method.call *a, &blk
@@ -19,9 +17,9 @@ module Overider
19
17
  end
20
18
 
21
19
  if !!blk then
22
- obj.instance_exec(*a, blk, &pr)
20
+ self.instance_exec *a, blk, &pr
23
21
  else
24
- obj.instance_exec(*a, &pr)
22
+ self.instance_exec *a, &pr
25
23
  end
26
24
  end
27
25
  end
@@ -1,3 +1,3 @@
1
1
  module Overider
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
@@ -153,4 +153,53 @@ describe Overider do
153
153
  result = F.new.hello ->{ "block" }
154
154
  result.should == "hello block in F"
155
155
  end
156
+
157
+ it 'can access instance variables' do
158
+ module HelloModule
159
+ def hello
160
+ "hello "
161
+ end
162
+ end
163
+
164
+ class G
165
+ include HelloModule
166
+ extend Overider
167
+
168
+ def initialize
169
+ @one = 'one'
170
+ @two = 'two'
171
+ end
172
+
173
+ # In the overide block, we have to declare blk as a normal block arg, not &blk.
174
+ # It must be a Proc or lambda
175
+ overide :hello do
176
+ overiden + "#{@one} and #{@two}"
177
+ end
178
+ end
179
+
180
+ result = G.new.hello ->{ "block" }
181
+ result.should == "hello one and two"
182
+ end
183
+
184
+ it 'can access instance variables defined in base class' do
185
+ module HelloModule
186
+ def hello
187
+ "hello "
188
+ end
189
+ end
190
+
191
+ class H < G
192
+ include HelloModule
193
+ extend Overider
194
+
195
+ # In the overide block, we have to declare blk as a normal block arg, not &blk.
196
+ # It must be a Proc or lambda
197
+ overide :hello do
198
+ overiden + " and again: #{@one} and #{@two}"
199
+ end
200
+ end
201
+
202
+ result = H.new.hello ->{ "block" }
203
+ result.should == "hello one and two and again: one and two"
204
+ end
156
205
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: overider
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &76593190 !ruby/object:Gem::Requirement
16
+ requirement: &79785640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *76593190
24
+ version_requirements: *79785640
25
25
  description: ! "\n class A\n def hello\n \"hello\"\n end\n end\n\n
26
26
  \ # Later, I want to overide class A methods\n\n class A\n extend Overider\n\n
27
27
  \ overide (:hello) do |*a|\n overiden(*a) + \" overide\"\n end\n