introspection 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/introspection.gemspec +2 -1
- data/lib/introspection.rb +1 -0
- data/lib/introspection/change_detector.rb +54 -0
- data/lib/introspection/core_ext.rb +1 -0
- data/lib/introspection/core_ext/symbol.rb +6 -0
- data/lib/introspection/method.rb +1 -1
- data/lib/introspection/receivers.rb +3 -3
- data/lib/introspection/snapshot.rb +2 -2
- data/lib/introspection/version.rb +1 -1
- data/test/class_snapshot_test.rb +9 -9
- data/test/instance_snapshot_test.rb +3 -3
- data/test/module_snapshot_test.rb +3 -3
- data/test/snapshot_test.rb +13 -8
- metadata +31 -10
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/introspection.gemspec
CHANGED
@@ -22,7 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
|
-
s.add_dependency "
|
25
|
+
s.add_dependency "metaclass", "~> 0.0.1"
|
26
|
+
s.add_dependency "instantiator", "~> 0.0.3"
|
26
27
|
|
27
28
|
s.add_development_dependency "rake"
|
28
29
|
end
|
data/lib/introspection.rb
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "introspection"
|
2
|
+
require "instantiator"
|
3
|
+
|
4
|
+
before_class_snapshots = {}
|
5
|
+
before_instance_snapshots = {}
|
6
|
+
ObjectSpace.each_object do |object|
|
7
|
+
if Class === object
|
8
|
+
next if Instantiator.unsupported_class?(object)
|
9
|
+
object.instantiate
|
10
|
+
end
|
11
|
+
end
|
12
|
+
ObjectSpace.each_object do |object|
|
13
|
+
if Class === object
|
14
|
+
next if Instantiator.unsupported_class?(object)
|
15
|
+
before_class_snapshots[object] = Introspection::Snapshot.new(object)
|
16
|
+
before_instance_snapshots[object] = Introspection::Snapshot.new(object.instantiate)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class String
|
21
|
+
def foo
|
22
|
+
end
|
23
|
+
def self.bar
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
after_class_snapshots = {}
|
28
|
+
after_instance_snapshots = {}
|
29
|
+
ObjectSpace.each_object do |object|
|
30
|
+
if Class === object
|
31
|
+
next if Instantiator.unsupported_class?(object)
|
32
|
+
after_class_snapshots[object] = Introspection::Snapshot.new(object)
|
33
|
+
after_instance_snapshots[object] = Introspection::Snapshot.new(object.instantiate)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
before_instance_snapshots.each_key do |key|
|
38
|
+
if after_instance_snapshots[key]
|
39
|
+
if before_instance_snapshots[key].changed?(after_instance_snapshots[key])
|
40
|
+
p [key, :instance_method, before_instance_snapshots[key].diff(after_instance_snapshots[key])]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
before_class_snapshots.each_key do |key|
|
46
|
+
if after_class_snapshots[key]
|
47
|
+
if before_class_snapshots[key].changed?(after_class_snapshots[key])
|
48
|
+
p [key, :class_method, before_class_snapshots[key].diff(after_class_snapshots[key])]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# => [String, :instance_method, {:removed=>[], :added=>[String#foo (public)]}]
|
54
|
+
# => [String, :class_method, {:removed=>[], :added=>[#<Class:String>#bar (public)]}]
|
@@ -0,0 +1 @@
|
|
1
|
+
require "introspection/core_ext/symbol"
|
data/lib/introspection/method.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "
|
1
|
+
require "metaclass"
|
2
2
|
|
3
3
|
module Introspection
|
4
4
|
|
@@ -11,7 +11,7 @@ module Introspection
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class NullReceiver
|
14
|
-
def
|
14
|
+
def __metaclass__
|
15
15
|
NullMetaclass.new
|
16
16
|
end
|
17
17
|
|
@@ -25,7 +25,7 @@ module Introspection
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def local_receivers
|
28
|
-
[
|
28
|
+
[__metaclass__] + __metaclass__.ancestors - superklass.__metaclass__.ancestors
|
29
29
|
end
|
30
30
|
|
31
31
|
def receivers
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require "introspection/receivers"
|
2
|
-
require "
|
2
|
+
require "metaclass"
|
3
3
|
|
4
4
|
module Introspection
|
5
5
|
|
@@ -7,7 +7,7 @@ module Introspection
|
|
7
7
|
attr_reader :methods
|
8
8
|
|
9
9
|
def initialize(object)
|
10
|
-
@methods = object.receivers.map do |receiver|
|
10
|
+
@methods = (object.receivers rescue []).map do |receiver|
|
11
11
|
[:public, :protected, :private].map do |visibility|
|
12
12
|
query_method = "#{visibility}_instance_methods"
|
13
13
|
receiver.send(query_method, false).map do |method|
|
data/test/class_snapshot_test.rb
CHANGED
@@ -5,29 +5,29 @@ class ClassSnapshotTest < Test::Unit::TestCase
|
|
5
5
|
def test_detect_class_method_on_class
|
6
6
|
for_all_method_visibilities do |visibility|
|
7
7
|
klass = Class.new
|
8
|
-
klass.
|
9
|
-
klass.
|
10
|
-
assert_method_exists(klass, klass.
|
8
|
+
klass.__metaclass__.send(:define_method, :foo) {}
|
9
|
+
klass.__metaclass__.send(visibility, :foo)
|
10
|
+
assert_method_exists(klass, klass.__metaclass__, :foo, visibility)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_detect_class_method_on_superclass
|
15
15
|
for_all_method_visibilities do |visibility|
|
16
16
|
superklass = Class.new
|
17
|
-
superklass.
|
18
|
-
superklass.
|
17
|
+
superklass.__metaclass__.send(:define_method, :foo) {}
|
18
|
+
superklass.__metaclass__.send(visibility, :foo)
|
19
19
|
klass = Class.new(superklass)
|
20
|
-
assert_method_exists(klass, superklass.
|
20
|
+
assert_method_exists(klass, superklass.__metaclass__, :foo, visibility)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_detect_class_method_on_superclass_of_superclass
|
25
25
|
for_all_method_visibilities do |visibility|
|
26
26
|
superduperklass = Class.new
|
27
|
-
superduperklass.
|
28
|
-
superduperklass.
|
27
|
+
superduperklass.__metaclass__.send(:define_method, :foo) {}
|
28
|
+
superduperklass.__metaclass__.send(visibility, :foo)
|
29
29
|
klass = Class.new(Class.new(superduperklass))
|
30
|
-
assert_method_exists(klass, superduperklass.
|
30
|
+
assert_method_exists(klass, superduperklass.__metaclass__, :foo, visibility)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -5,9 +5,9 @@ class InstanceSnapshotTest < Test::Unit::TestCase
|
|
5
5
|
def test_detect_instance_method_on_singleton_class
|
6
6
|
for_all_method_visibilities do |visibility|
|
7
7
|
instance = Class.new.new
|
8
|
-
instance.
|
9
|
-
instance.
|
10
|
-
assert_method_exists(instance, instance.
|
8
|
+
instance.__metaclass__.send(:define_method, :foo) {}
|
9
|
+
instance.__metaclass__.send(visibility, :foo)
|
10
|
+
assert_method_exists(instance, instance.__metaclass__, :foo, visibility)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -5,9 +5,9 @@ class ModuleSnapshotTest < Test::Unit::TestCase
|
|
5
5
|
def test_detect_module_method_on_module
|
6
6
|
for_all_method_visibilities do |visibility|
|
7
7
|
mod = Module.new
|
8
|
-
mod.
|
9
|
-
mod.
|
10
|
-
assert_method_exists(mod, mod.
|
8
|
+
mod.__metaclass__.send(:define_method, :foo) {}
|
9
|
+
mod.__metaclass__.send(visibility, :foo)
|
10
|
+
assert_method_exists(mod, mod.__metaclass__, :foo, visibility)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
data/test/snapshot_test.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "test_helper"
|
2
|
+
require "blankslate"
|
2
3
|
|
3
4
|
class SnapshotTest < Test::Unit::TestCase
|
4
5
|
|
@@ -7,38 +8,38 @@ class SnapshotTest < Test::Unit::TestCase
|
|
7
8
|
def test_should_report_methods_added
|
8
9
|
instance = Class.new.new
|
9
10
|
before = Snapshot.new(instance)
|
10
|
-
instance.
|
11
|
+
instance.__metaclass__.send(:define_method, :foo) {}
|
11
12
|
after = Snapshot.new(instance)
|
12
13
|
diff = before.diff(after)
|
13
14
|
assert_equal 1, diff[:added].length
|
14
|
-
assert_equal instance.
|
15
|
+
assert_equal instance.__metaclass__, diff[:added][0].owner
|
15
16
|
assert_equal :foo, diff[:added][0].name
|
16
17
|
end
|
17
18
|
|
18
19
|
def test_should_report_methods_removed
|
19
20
|
instance = Class.new.new
|
20
|
-
instance.
|
21
|
+
instance.__metaclass__.send(:define_method, :foo) {}
|
21
22
|
before = Snapshot.new(instance)
|
22
|
-
instance.
|
23
|
+
instance.__metaclass__.send(:remove_method, :foo)
|
23
24
|
after = Snapshot.new(instance)
|
24
25
|
diff = before.diff(after)
|
25
26
|
assert_equal 1, diff[:removed].length
|
26
|
-
assert_equal instance.
|
27
|
+
assert_equal instance.__metaclass__, diff[:removed][0].owner
|
27
28
|
assert_equal :foo, diff[:removed][0].name
|
28
29
|
end
|
29
30
|
|
30
31
|
def test_should_indicate_snapshot_has_changed_when_method_is_added
|
31
32
|
instance = Class.new.new
|
32
33
|
assert_snapshot_changed(instance) do
|
33
|
-
instance.
|
34
|
+
instance.__metaclass__.send(:define_method, :foo) {}
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
38
|
def test_should_indicate_snapshot_has_changed_when_method_is_removed
|
38
39
|
instance = Class.new.new
|
39
|
-
instance.
|
40
|
+
instance.__metaclass__.send(:define_method, :foo) {}
|
40
41
|
assert_snapshot_changed(instance) do
|
41
|
-
instance.
|
42
|
+
instance.__metaclass__.send(:remove_method, :foo)
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
@@ -47,4 +48,8 @@ class SnapshotTest < Test::Unit::TestCase
|
|
47
48
|
assert_snapshot_unchanged(instance) {}
|
48
49
|
end
|
49
50
|
|
51
|
+
def test_should_cope_with_blankslate_object
|
52
|
+
assert_nothing_raised { Snapshot.new(BlankSlate.new) }
|
53
|
+
end
|
54
|
+
|
50
55
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: introspection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Mead
|
@@ -15,28 +15,45 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-12 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: metaclass
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 29
|
30
30
|
segments:
|
31
|
-
- 1
|
32
31
|
- 0
|
33
|
-
|
32
|
+
- 0
|
33
|
+
- 1
|
34
|
+
version: 0.0.1
|
34
35
|
type: :runtime
|
35
36
|
version_requirements: *id001
|
36
37
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
+
name: instantiator
|
38
39
|
prerelease: false
|
39
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 25
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
- 3
|
50
|
+
version: 0.0.3
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rake
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
57
|
none: false
|
41
58
|
requirements:
|
42
59
|
- - ">="
|
@@ -46,7 +63,7 @@ dependencies:
|
|
46
63
|
- 0
|
47
64
|
version: "0"
|
48
65
|
type: :development
|
49
|
-
version_requirements: *
|
66
|
+
version_requirements: *id003
|
50
67
|
description: ""
|
51
68
|
email:
|
52
69
|
- james@floehopper.org
|
@@ -58,12 +75,16 @@ extra_rdoc_files: []
|
|
58
75
|
|
59
76
|
files:
|
60
77
|
- .gitignore
|
78
|
+
- .travis.yml
|
61
79
|
- Gemfile
|
62
80
|
- README.md
|
63
81
|
- Rakefile
|
64
82
|
- introspection.gemspec
|
65
83
|
- lib/introspection.rb
|
66
84
|
- lib/introspection/assertions.rb
|
85
|
+
- lib/introspection/change_detector.rb
|
86
|
+
- lib/introspection/core_ext.rb
|
87
|
+
- lib/introspection/core_ext/symbol.rb
|
67
88
|
- lib/introspection/method.rb
|
68
89
|
- lib/introspection/receivers.rb
|
69
90
|
- lib/introspection/snapshot.rb
|