refining 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/refining.rb +42 -8
  2. metadata +27 -18
data/lib/refining.rb CHANGED
@@ -12,9 +12,30 @@
12
12
 
13
13
  class Object
14
14
  def refine_singleton_method (meth, &block)
15
- class << self
16
- self
17
- end.refine_method meth, &block
15
+ return unless block
16
+
17
+ target = self
18
+
19
+ (method(meth) rescue nil).tap {|old|
20
+ old ||= proc {}
21
+
22
+ what = class << target
23
+ self
24
+ end
25
+
26
+ what.send :define_method, meth do |*args, &blk|
27
+ return if @__refining_defined__ && [:method_added, :singleton_method_added].member?(meth)
28
+
29
+ @__refining_defined__ = true
30
+ what.send(:define_method, 'temporary method for refining', &block)
31
+
32
+ target.send('temporary method for refining', old.is_a?(UnboundMethod) ? old.bind(self) : old, *args, &blk).tap {
33
+ what.send(:undef_method, 'temporary method for refining') rescue nil
34
+
35
+ @__refining_defined__ = false
36
+ }
37
+ end
38
+ }
18
39
  end
19
40
  end
20
41
 
@@ -22,13 +43,26 @@ class Module
22
43
  def refine_method (meth, &block)
23
44
  return unless block
24
45
 
25
- old = instance_method(meth) rescue nil
46
+ (instance_method(meth) rescue nil).tap {|old|
47
+ old ||= proc {}
26
48
 
27
- define_method(meth) {|*args|
28
- instance_exec((old.is_a?(UnboundMethod) ? old.bind(self) : old) || proc {}, *args, &block)
29
- }
49
+ define_method(meth) {|*args, &blk|
50
+ return if @__refining_defined__ && [:method_added, :singleton_method_added].member?(meth)
51
+
52
+ what = class << self
53
+ self
54
+ end
30
55
 
31
- old
56
+ @__refining_defined__ = true
57
+ what.send(:define_method, 'temporary method for refining', &block)
58
+
59
+ send('temporary method for refining', old.is_a?(UnboundMethod) ? old.bind(self) : old, *args, &blk).tap {
60
+ what.send(:undef_method, 'temporary method for refining') rescue nil
61
+
62
+ @__refining_defined__ = false
63
+ }
64
+ }
65
+ }
32
66
  end
33
67
 
34
68
  alias refine_module_method refine_singleton_method
metadata CHANGED
@@ -1,45 +1,54 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: refining
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 0.0.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - meh.
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-14 00:00:00.000000000Z
12
+
13
+ date: 2011-08-18 00:00:00 Z
13
14
  dependencies: []
15
+
14
16
  description:
15
17
  email: meh@paranoici.org
16
18
  executables: []
19
+
17
20
  extensions: []
21
+
18
22
  extra_rdoc_files: []
19
- files:
23
+
24
+ files:
20
25
  - lib/refining.rb
21
26
  homepage: http://github.com/meh/refining
22
27
  licenses: []
28
+
23
29
  post_install_message:
24
30
  rdoc_options: []
25
- require_paths:
31
+
32
+ require_paths:
26
33
  - lib
27
- required_ruby_version: !ruby/object:Gem::Requirement
34
+ required_ruby_version: !ruby/object:Gem::Requirement
28
35
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
41
  none: false
35
- requirements:
36
- - - ! '>='
37
- - !ruby/object:Gem::Version
38
- version: '0'
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
39
46
  requirements: []
47
+
40
48
  rubyforge_project:
41
- rubygems_version: 1.8.6
49
+ rubygems_version: 1.8.5
42
50
  signing_key:
43
51
  specification_version: 3
44
52
  summary: Easily refine methods
45
53
  test_files: []
54
+