ruby_patch 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_patch/auto_load.rb +3 -1
- data/lib/ruby_patch/core_ext/class.rb +18 -0
- data/lib/ruby_patch/{enumerable.rb → core_ext/enumerable.rb} +2 -0
- data/lib/ruby_patch/core_ext/module.rb +23 -0
- data/lib/ruby_patch/core_ext/object.rb +52 -0
- data/lib/ruby_patch/{string.rb → core_ext/string.rb} +2 -0
- data/lib/ruby_patch/{time.rb → core_ext/time.rb} +2 -0
- data/lib/ruby_patch/core_ext.rb +12 -0
- data/lib/ruby_patch/version.rb +1 -1
- data/lib/ruby_patch.rb +5 -7
- data/spec/ruby_patch/auto_load_spec.rb +0 -2
- data/spec/ruby_patch/{class_spec.rb → core_ext/class_spec.rb} +0 -2
- data/spec/ruby_patch/{enumerable_spec.rb → core_ext/enumerable_spec.rb} +0 -1
- data/spec/ruby_patch/core_ext/module_spec.rb +122 -0
- data/spec/ruby_patch/{object_spec.rb → core_ext/object_spec.rb} +0 -3
- data/spec/ruby_patch/{string_spec.rb → core_ext/string_spec.rb} +0 -2
- data/spec/ruby_patch/{time_spec.rb → core_ext/time_spec.rb} +0 -2
- data/spec/spec_helper.rb +2 -0
- metadata +25 -23
- data/lib/ruby_patch/class.rb +0 -18
- data/lib/ruby_patch/object.rb +0 -49
- data/spec/ruby_patch_spec.rb +0 -3
data/lib/ruby_patch/auto_load.rb
CHANGED
@@ -11,7 +11,9 @@ module RubyPatch
|
|
11
11
|
|
12
12
|
# You should use +module Http; end+ rather than +module HTTP; end+
|
13
13
|
def _file_name(name)
|
14
|
-
"#{self}::#{name}".split('::').map{|t|
|
14
|
+
"#{self}::#{name}".split('::').map{|t|
|
15
|
+
t.sub(/\A([A-Z])/){$1.downcase}.gsub(/([A-Z])/){"_#{$1.downcase}"}
|
16
|
+
}.join('/') + '.rb'
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Class
|
2
|
+
new_methods :posterity_of?, :children, :posterities
|
3
|
+
|
4
|
+
def posterity_of?(klass)
|
5
|
+
self.ancestors.include?(klass)
|
6
|
+
end
|
7
|
+
|
8
|
+
def children()
|
9
|
+
posterities.select{|c|
|
10
|
+
c.superclass == self
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def posterities()
|
15
|
+
classes = ObjectSpace.each_object(Class)\
|
16
|
+
.select{|c| c.ancestors.include?(self)} - [self]
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Module
|
2
|
+
UnintendedOverrideError = Class.new(StandardError)
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def new_methods(*names)
|
7
|
+
unless (buf = names.select{|m| instance_methods.include?(m)}).empty?
|
8
|
+
raise UnintendedOverrideError, "Unintended public/protected method(s) override: #{buf.join(' ')}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def new_private_methods(*names)
|
13
|
+
unless (buf = names.select{|m| private_instance_methods.include?(m)}).empty?
|
14
|
+
raise UnintendedOverrideError, "Unintended private method(s) override: #{buf.join(' ')}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_singleton_methods(*names)
|
19
|
+
unless (buf = names.select{|m| singleton_methods.include?(m)}).empty?
|
20
|
+
raise UnintendedOverrideError, "Unintended singleton method(s) override: #{buf.join(' ')}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Object
|
2
|
+
new_methods :exception?, :deep_clone
|
3
|
+
new_private_methods :parse_caller, :__DIR__, :__METHOD__, :check_type
|
4
|
+
|
5
|
+
def exception?()
|
6
|
+
self.class.posterity_of?(Exception)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Create a deep clone by using Marshal.
|
10
|
+
def deep_clone()
|
11
|
+
Marshal.load(Marshal.dump(self))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# @return [Hash]
|
17
|
+
def parse_caller(backtrace)
|
18
|
+
if /\A(.+?):(\d+)(?::in `(.*)')?/ =~ backtrace
|
19
|
+
file = $1
|
20
|
+
line = $2.to_i
|
21
|
+
method = $3
|
22
|
+
{
|
23
|
+
file: file,
|
24
|
+
line: line,
|
25
|
+
method: method,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def __DIR__()
|
31
|
+
called_from = parse_caller(caller(1).first)[:file]
|
32
|
+
dir = File.dirname(called_from)
|
33
|
+
File.expand_path(dir)
|
34
|
+
end
|
35
|
+
|
36
|
+
def __METHOD__()
|
37
|
+
parse_caller(caller(1).first)[:method]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Check type of var.
|
41
|
+
# @param [Symbol, String, Class, Array<Symbol, String, Class, Array<...>>]
|
42
|
+
# @return [var]
|
43
|
+
def check_type(var, *allow)
|
44
|
+
if allow.flatten\
|
45
|
+
.map{|klass| klass.to_s.to_sym}\
|
46
|
+
.include?(var.class.to_s.to_sym)
|
47
|
+
var
|
48
|
+
else
|
49
|
+
raise TypeError, "#{var}:#{var.class} is not one of #{allow.flatten}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/ruby_patch/version.rb
CHANGED
data/lib/ruby_patch.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
module RubyPatch
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'ruby_patch
|
7
|
-
require 'ruby_patch/time'
|
8
|
-
require 'ruby_patch/enumerable'
|
2
|
+
%w(
|
3
|
+
version
|
4
|
+
auto_load
|
5
|
+
core_ext
|
6
|
+
).each{|lib| require File.join('ruby_patch', lib)}
|
9
7
|
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Module do
|
4
|
+
before :all do
|
5
|
+
class DummyClass1
|
6
|
+
def self.single_c1
|
7
|
+
:single_c1
|
8
|
+
end
|
9
|
+
|
10
|
+
def public_c1
|
11
|
+
:public_c1
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def private_c1
|
17
|
+
:private_c1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module DummyModule1
|
22
|
+
|
23
|
+
module_function
|
24
|
+
|
25
|
+
def function_m1
|
26
|
+
:function_m1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'new_methods' do
|
32
|
+
it do
|
33
|
+
lambda{
|
34
|
+
class DummyClass1
|
35
|
+
new_methods :public_c1
|
36
|
+
end
|
37
|
+
}.should raise_error ::Module::UnintendedOverrideError
|
38
|
+
end
|
39
|
+
|
40
|
+
it do
|
41
|
+
lambda{
|
42
|
+
class DummyClass1
|
43
|
+
new_methods :dummy
|
44
|
+
end
|
45
|
+
}.should_not raise_error
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'new_private_methods' do
|
50
|
+
it do
|
51
|
+
lambda{
|
52
|
+
class DummyClass1
|
53
|
+
new_private_methods :private_c1
|
54
|
+
end
|
55
|
+
}.should raise_error ::Module::UnintendedOverrideError
|
56
|
+
end
|
57
|
+
|
58
|
+
it do
|
59
|
+
lambda{
|
60
|
+
class DummyClass1
|
61
|
+
new_private_methods :dummy
|
62
|
+
end
|
63
|
+
}.should_not raise_error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'new_singleton_methods' do
|
68
|
+
it do
|
69
|
+
lambda{
|
70
|
+
class DummyClass1
|
71
|
+
new_singleton_methods :single_c1
|
72
|
+
end
|
73
|
+
}.should raise_error ::Module::UnintendedOverrideError
|
74
|
+
end
|
75
|
+
|
76
|
+
it do
|
77
|
+
lambda{
|
78
|
+
class DummyClass1
|
79
|
+
new_singleton_methods :dummy
|
80
|
+
end
|
81
|
+
}.should_not raise_error
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'module_function' do
|
86
|
+
describe 'new_private_methods' do
|
87
|
+
it do
|
88
|
+
lambda{
|
89
|
+
module DummyModule1
|
90
|
+
new_private_methods :function_m1
|
91
|
+
end
|
92
|
+
}.should raise_error ::Module::UnintendedOverrideError
|
93
|
+
end
|
94
|
+
|
95
|
+
it do
|
96
|
+
lambda{
|
97
|
+
module DummyModule1
|
98
|
+
new_private_methods :dummy
|
99
|
+
end
|
100
|
+
}.should_not raise_error
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'new_singleton_methods' do
|
105
|
+
it do
|
106
|
+
lambda{
|
107
|
+
module DummyModule1
|
108
|
+
new_singleton_methods :function_m1
|
109
|
+
end
|
110
|
+
}.should raise_error ::Module::UnintendedOverrideError
|
111
|
+
end
|
112
|
+
|
113
|
+
it do
|
114
|
+
lambda{
|
115
|
+
module DummyModule1
|
116
|
+
new_private_methods :dummy
|
117
|
+
end
|
118
|
+
}.should_not raise_error
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_patch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153918720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.10'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153918720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: simplecov
|
27
|
-
requirement: &
|
27
|
+
requirement: &2153918220 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0.6'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2153918220
|
36
36
|
description: Monkey patches used by kshramt's libraries.
|
37
37
|
email:
|
38
38
|
executables: []
|
@@ -41,22 +41,24 @@ extra_rdoc_files: []
|
|
41
41
|
files:
|
42
42
|
- lib/ruby_patch.rb
|
43
43
|
- lib/ruby_patch/auto_load.rb
|
44
|
-
- lib/ruby_patch/
|
45
|
-
- lib/ruby_patch/
|
46
|
-
- lib/ruby_patch/
|
47
|
-
- lib/ruby_patch/
|
48
|
-
- lib/ruby_patch/
|
44
|
+
- lib/ruby_patch/core_ext.rb
|
45
|
+
- lib/ruby_patch/core_ext/class.rb
|
46
|
+
- lib/ruby_patch/core_ext/enumerable.rb
|
47
|
+
- lib/ruby_patch/core_ext/module.rb
|
48
|
+
- lib/ruby_patch/core_ext/object.rb
|
49
|
+
- lib/ruby_patch/core_ext/string.rb
|
50
|
+
- lib/ruby_patch/core_ext/time.rb
|
49
51
|
- lib/ruby_patch/version.rb
|
50
52
|
- rakefile
|
51
53
|
- ruby_patch.gemspec
|
52
54
|
- spec/ruby_patch/auto_load_spec.rb
|
53
|
-
- spec/ruby_patch/class_spec.rb
|
55
|
+
- spec/ruby_patch/core_ext/class_spec.rb
|
56
|
+
- spec/ruby_patch/core_ext/enumerable_spec.rb
|
57
|
+
- spec/ruby_patch/core_ext/module_spec.rb
|
58
|
+
- spec/ruby_patch/core_ext/object_spec.rb
|
59
|
+
- spec/ruby_patch/core_ext/string_spec.rb
|
60
|
+
- spec/ruby_patch/core_ext/time_spec.rb
|
54
61
|
- spec/ruby_patch/dummy/a.rb
|
55
|
-
- spec/ruby_patch/enumerable_spec.rb
|
56
|
-
- spec/ruby_patch/object_spec.rb
|
57
|
-
- spec/ruby_patch/string_spec.rb
|
58
|
-
- spec/ruby_patch/time_spec.rb
|
59
|
-
- spec/ruby_patch_spec.rb
|
60
62
|
- spec/spec_helper.rb
|
61
63
|
homepage:
|
62
64
|
licenses: []
|
@@ -84,10 +86,10 @@ specification_version: 3
|
|
84
86
|
summary: Monky patches for ruby.
|
85
87
|
test_files:
|
86
88
|
- spec/ruby_patch/auto_load_spec.rb
|
87
|
-
- spec/ruby_patch/class_spec.rb
|
88
|
-
- spec/ruby_patch/enumerable_spec.rb
|
89
|
-
- spec/ruby_patch/
|
90
|
-
- spec/ruby_patch/
|
91
|
-
- spec/ruby_patch/
|
92
|
-
- spec/
|
89
|
+
- spec/ruby_patch/core_ext/class_spec.rb
|
90
|
+
- spec/ruby_patch/core_ext/enumerable_spec.rb
|
91
|
+
- spec/ruby_patch/core_ext/module_spec.rb
|
92
|
+
- spec/ruby_patch/core_ext/object_spec.rb
|
93
|
+
- spec/ruby_patch/core_ext/string_spec.rb
|
94
|
+
- spec/ruby_patch/core_ext/time_spec.rb
|
93
95
|
has_rdoc:
|
data/lib/ruby_patch/class.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module RubyPatch
|
2
|
-
class ::Class
|
3
|
-
def posterity_of?(klass)
|
4
|
-
self.ancestors.include?(klass)
|
5
|
-
end
|
6
|
-
|
7
|
-
def children()
|
8
|
-
posterities.select{|c|
|
9
|
-
c.superclass == self
|
10
|
-
}
|
11
|
-
end
|
12
|
-
|
13
|
-
def posterities()
|
14
|
-
classes = ObjectSpace.each_object(Class)\
|
15
|
-
.select{|c| c.ancestors.include?(self)} - [self]
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/lib/ruby_patch/object.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
module RubyPatch
|
2
|
-
class ::Object
|
3
|
-
def exception?()
|
4
|
-
self.class.posterity_of?(Exception)
|
5
|
-
end
|
6
|
-
|
7
|
-
# @return [Hash]
|
8
|
-
def parse_caller(backtrace)
|
9
|
-
if /\A(.+?):(\d+)(?::in `(.*)')?/ =~ backtrace
|
10
|
-
file = $1
|
11
|
-
line = $2.to_i
|
12
|
-
method = $3
|
13
|
-
{
|
14
|
-
file: file,
|
15
|
-
line: line,
|
16
|
-
method: method,
|
17
|
-
}
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def __DIR__()
|
22
|
-
called_from = parse_caller(caller(1).first)[:file]
|
23
|
-
dir = File.dirname(called_from)
|
24
|
-
File.expand_path(dir)
|
25
|
-
end
|
26
|
-
|
27
|
-
def __METHOD__()
|
28
|
-
parse_caller(caller(1).first)[:method]
|
29
|
-
end
|
30
|
-
|
31
|
-
# Create a deep clone by using Marshal.
|
32
|
-
def deep_clone()
|
33
|
-
Marshal.load(Marshal.dump(self))
|
34
|
-
end
|
35
|
-
|
36
|
-
# Check type of var.
|
37
|
-
# +allow+ is an array which contains Symbol, String or Class object which represents allowable classes.
|
38
|
-
# +allow+ can be nested array because they are flattened.
|
39
|
-
def check_type(var, *allow)
|
40
|
-
if allow.flatten\
|
41
|
-
.map{|klass| klass.to_s.to_sym}\
|
42
|
-
.include?(var.class.to_s.to_sym)
|
43
|
-
var
|
44
|
-
else
|
45
|
-
raise TypeError, "#{var}:#{var.class} is not one of #{allow.flatten}"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/spec/ruby_patch_spec.rb
DELETED