object_tree 0.0.5

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGM0NTg2ZTdmMTRlYzBjNjdkYjc1ZDcxOGM0MDUwM2JhOGZiZTYwNg==
5
+ data.tar.gz: !binary |-
6
+ YjQwYWEyYWI0MzFkODFmMjE3OGY0Mzk3NDRmZjZiOGFmOWY0MDczMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OGFhNjI4YWI2Mjk0MDdlNWMwNmQwYWE1NjYxNzg0ZjMxMTgwMjJlMGMxYWM3
10
+ YjE3YWNlZmYzYzU5MjQwOGNhZmU3YzAzOTI1MzgxYmFmZDUzY2ZmOTMzMWU1
11
+ MDUzNDA3MmNiZDRjZWI3MDQzMDZlYWQxMzZjMGI4NzA2ODg1NjE=
12
+ data.tar.gz: !binary |-
13
+ M2RlNTEzZjY3NDhkZGU5MTFlNGQxNjlmOGUyNWM3MWY1ZTY5YWE2MjhjZjlj
14
+ MWRiODRmYTI0MjRhM2ZkNTcyOGM5NDNhZDA5MzE1OGY3ZDg5N2U2MjlhOWEy
15
+ Mjg1MDk4ODRmMGRkOTZlMTVhZGMxZDlmYTliYTQ2N2VjNDk4NDk=
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ **/*.swp
20
+ *.swp
21
+ **/*~
22
+ *.*~
23
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in object_tree.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 siman-man
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # ObjectTree
2
+
3
+ Rubyのオブジェクト関係をtreeっぽく表示
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'object_tree'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install object_tree
18
+
19
+ ## Usage
20
+
21
+ Object以下のクラス関係を表示
22
+
23
+ tree = ObjectTree::Tree.create(Object)
24
+ tree.draw
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/example/sample.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'object_tree'
2
+ require 'active_record'
3
+
4
+ module D
5
+ end
6
+
7
+ module E
8
+ end
9
+
10
+ module F
11
+ end
12
+
13
+ module G
14
+ end
15
+
16
+ class A
17
+ prepend F
18
+ end
19
+
20
+ class H
21
+ end
22
+
23
+ class B < A
24
+ include D
25
+ include E
26
+ prepend G
27
+ end
28
+
29
+ class C < A
30
+ include E
31
+ include D
32
+ end
33
+
34
+
35
+ tree = ObjectTree::Tree.create(A)
36
+ tree.draw
37
+
38
+ tree = ObjectTree::Tree.create(A, true)
39
+ tree.draw
@@ -0,0 +1,39 @@
1
+ require 'object_tree'
2
+ require 'active_record'
3
+
4
+ module D
5
+ end
6
+
7
+ module E
8
+ end
9
+
10
+ module F
11
+ end
12
+
13
+ module G
14
+ end
15
+
16
+ class A
17
+ prepend F
18
+ end
19
+
20
+ class H
21
+ end
22
+
23
+ class B < A
24
+ include D
25
+ include E
26
+ prepend G
27
+ end
28
+
29
+ class C < A
30
+ include E
31
+ include D
32
+ end
33
+
34
+
35
+ tree = ObjectTree::Tree.create(BasicObject)
36
+ tree.draw
37
+
38
+ tree = ObjectTree::Tree.create(BasicObject, true)
39
+ tree.draw
@@ -0,0 +1,3 @@
1
+ module ObjectTree
2
+ VERSION = "0.0.5"
3
+ end
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ require "object_tree/version"
3
+
4
+ class Module
5
+ def prepended_modules
6
+ ancestors = self.ancestors
7
+ ancestors[0..ancestors.find_index(self)] - [self]
8
+ end
9
+ end
10
+
11
+ module ObjectTree
12
+ class Tree
13
+ attr_reader :tree
14
+ SPACE_SIZE = 6
15
+
16
+ # 31: red, 32: green, 33: yellow, 34: blue, 35: pink, 36: aqua, 37: gray
17
+ MCOLOR = 31
18
+ CCOLOR = 32
19
+
20
+ def initialize(klass, mflag = false)
21
+ @tree = Hash.new{|h, k| h[k] = [] }
22
+ @mflag = mflag
23
+ @root_class = klass
24
+
25
+ if klass == BasicObject
26
+ klass = Object
27
+ @tree[BasicObject] << Object
28
+ end
29
+
30
+ create_object_tree(object_list(klass))
31
+ end
32
+
33
+ class << self
34
+ alias :create :new
35
+ end
36
+
37
+ def singleton(klass)
38
+ class << klass; self end
39
+ end
40
+
41
+ def object_list(klass)
42
+ ObjectSpace.each_object(singleton(klass)) do |subclass|
43
+ if klass != subclass
44
+ @tree[klass] << subclass
45
+ object_list(subclass) unless @tree.has_key?(subclass)
46
+ end
47
+ end
48
+
49
+ @tree
50
+ end
51
+
52
+ def create_object_tree(tree)
53
+ tree.each do |klass1, subclasses1|
54
+ tree.each do |klass2, subclasses2|
55
+ if klass1 > klass2
56
+ tree[klass1] -= (subclasses2 - [klass2])
57
+ end
58
+ end
59
+ end
60
+
61
+ tree
62
+ end
63
+
64
+ def draw
65
+ if @tree.size.zero?
66
+ puts "\e[#{CCOLOR}m<C>\e[m#{@root_class}"
67
+ else
68
+ @last_check = []
69
+ draw_tree({ @root_class => @tree[@root_class] }, 0)
70
+ end
71
+ end
72
+
73
+ def draw_tree(tree, nest, root = true, last = false)
74
+
75
+ @last_check[nest-1] = last if nest > 0
76
+
77
+ tree.each do |klass, subclasses|
78
+ if ( @mflag && klass.kind_of?(Class) && !klass.superclass.nil? )
79
+ module_list = klass.ancestors[0..klass.ancestors.find_index(klass.superclass)-1].reverse - [klass, klass.superclass] - klass.prepended_modules
80
+
81
+ module_list.each_with_index do |m, add|
82
+ puts "\e[#{MCOLOR}m<M>\e[m#{m.inspect}"
83
+ @last_check[nest] = true
84
+
85
+ nest.times do |column_number|
86
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
87
+ end
88
+ print "└─ ─ ── "
89
+ nest += 1
90
+ end
91
+ end
92
+
93
+ puts ( klass.kind_of?(Class) )? "\e[#{CCOLOR}m<C>\e[m#{klass.inspect}" : "\e[#{MCOLOR}m<M>\e[m#{klass.inspect}"
94
+
95
+ subclasses.each do |sub|
96
+ @last_check[nest] = true if sub == subclasses.last
97
+
98
+ nest.times do |column_number|
99
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
100
+ end
101
+
102
+ if @tree.has_key?(sub)
103
+ print ( sub == subclasses.last )? "└─ ─ ── " : "├─ ─ ── "
104
+ if sub == subclasses.last
105
+ draw_tree({ sub => @tree[sub]}, nest + 1, false, true)
106
+ else
107
+ draw_tree({ sub => @tree[sub]}, nest + 1, false, false)
108
+ end
109
+ else
110
+ print ( sub == subclasses.last )? "└─ ─ ── " : "├─ ─ ── "
111
+ nest_count = 0
112
+ if( @mflag && sub.kind_of?(Class) )
113
+ module_list = sub.ancestors[0..sub.ancestors.find_index(klass)-1].reverse - ([sub, klass] + sub.prepended_modules)
114
+ @last_check[nest] = ( sub == subclasses.last )? true : false
115
+
116
+ module_list.each_with_index do |m, add|
117
+ puts "\e[#{MCOLOR}m<M>\e[m#{m.inspect}"
118
+ @last_check[nest+add+1] = true
119
+
120
+ (nest+add+1).times do |column_number|
121
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
122
+ end
123
+ print "└─ ─ ── "
124
+ nest_count += 1
125
+ end
126
+ end
127
+ puts ( sub.kind_of?(Class) )? "\e[#{CCOLOR}m<C>\e[m#{sub.inspect}" : "\e[#{MCOLOR}m<M>\e[m#{sub.inspect}"
128
+
129
+ if ( @mflag && sub.kind_of?(Class) )
130
+ prepended_modules = sub.prepended_modules
131
+
132
+ prepended_modules.each_with_index do |m, add|
133
+ (nest+nest_count+add+1).times do |column_number|
134
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
135
+ end
136
+ puts "└─ ─ ── \e[#{MCOLOR}m<M>\e[m#{m.inspect}"
137
+ @last_check[nest+add+1] = true
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ require "object_tree/version"
3
+
4
+ class Module
5
+ def prepended_modules
6
+ ancestors = self.ancestors
7
+ ancestors[0..ancestors.find_index(self)] - [self]
8
+ end
9
+ end
10
+
11
+ module ObjectTree
12
+ class Tree
13
+ attr_reader :tree
14
+ SPACE_SIZE = 6
15
+
16
+ # 31: red, 32: green, 33: yellow, 34: blue, 35: pink, 36: aqua, 37: gray
17
+ MCOLOR = 31
18
+ CCOLOR = 32
19
+
20
+ def initialize(klass, mflag = false)
21
+ @tree = Hash.new{|h, k| h[k] = [] }
22
+ @mflag = mflag
23
+ @root_class = klass
24
+
25
+ if klass == BasicObject
26
+ klass = Object
27
+ @tree[BasicObject] << Object
28
+ end
29
+
30
+ create_object_tree(object_list(klass))
31
+ end
32
+
33
+ class << self
34
+ alias :create :new
35
+ end
36
+
37
+ def singleton(klass)
38
+ class << klass; self end
39
+ end
40
+
41
+ def object_list(klass)
42
+ ObjectSpace.each_object(singleton(klass)) do |subclass|
43
+ if klass != subclass
44
+ @tree[klass] << subclass
45
+ object_list(subclass) unless @tree.has_key?(subclass)
46
+ end
47
+ end
48
+
49
+ @tree
50
+ end
51
+
52
+ def create_object_tree(tree)
53
+ tree.each do |klass1, subclasses1|
54
+ tree.each do |klass2, subclasses2|
55
+ if klass1 > klass2
56
+ tree[klass1] -= (subclasses2 - [klass2])
57
+ end
58
+ end
59
+ end
60
+
61
+ tree
62
+ end
63
+
64
+ def draw
65
+ if @tree.size.zero?
66
+ puts "\e[#{CCOLOR}m<C>\e[m#{@root_class}"
67
+ else
68
+ @last_check = []
69
+ draw_tree({ @root_class => @tree.first.last }, 0)
70
+ end
71
+ end
72
+
73
+ def draw_tree(tree, nest, root = true, last = false)
74
+
75
+ @last_check[nest-1] = last if nest > 0
76
+
77
+ tree.each do |klass, subclasses|
78
+ if ( @mflag && klass.kind_of?(Class) && !klass.superclass.nil? )
79
+ module_list = klass.ancestors[0..klass.ancestors.find_index(klass.superclass)-1].reverse - [klass, klass.superclass] - klass.prepended_modules
80
+
81
+ module_list.each_with_index do |m, add|
82
+ puts "\e[#{MCOLOR}m<M>\e[m#{m.inspect}"
83
+ @last_check[nest] = true
84
+
85
+ nest.times do |column_number|
86
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
87
+ end
88
+ print "└─ ─ ── "
89
+ nest += 1
90
+ end
91
+ end
92
+
93
+ puts ( klass.kind_of?(Class) )? "\e[#{CCOLOR}m<C>\e[m#{klass.inspect}" : "\e[#{MCOLOR}m<M>\e[m#{klass.inspect}"
94
+
95
+ subclasses.each do |sub|
96
+ @last_check[nest] = true if sub == subclasses.last
97
+
98
+ nest.times do |column_number|
99
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
100
+ end
101
+
102
+ if @tree.has_key?(sub)
103
+ print ( sub == subclasses.last )? "└─ ─ ── " : "├─ ─ ── "
104
+ if sub == subclasses.last
105
+ draw_tree({ sub => @tree[sub]}, nest + 1, false, true)
106
+ else
107
+ draw_tree({ sub => @tree[sub]}, nest + 1, false, false)
108
+ end
109
+ else
110
+ print ( sub == subclasses.last )? "└─ ─ ── " : "├─ ─ ── "
111
+ nest_count = 0
112
+ if( @mflag && sub.kind_of?(Class) )
113
+ module_list = sub.ancestors[0..sub.ancestors.find_index(klass)-1].reverse - ([sub, klass] + sub.prepended_modules)
114
+ @last_check[nest] = ( sub == subclasses.last )? true : false
115
+
116
+ module_list.each_with_index do |m, add|
117
+ puts "\e[#{MCOLOR}m<M>\e[m#{m.inspect}"
118
+ @last_check[nest+add+1] = true
119
+
120
+ (nest+add+1).times do |column_number|
121
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
122
+ end
123
+ print "└─ ─ ── "
124
+ nest_count += 1
125
+ end
126
+ end
127
+ puts ( sub.kind_of?(Class) )? "\e[#{CCOLOR}m<C>\e[m#{sub.inspect}" : "\e[#{MCOLOR}m<M>\e[m#{sub.inspect}"
128
+
129
+ if ( @mflag && sub.kind_of?(Class) )
130
+ prepended_modules = sub.prepended_modules
131
+
132
+ prepended_modules.each_with_index do |m, add|
133
+ (nest+nest_count+add+1).times do |column_number|
134
+ print ((@last_check[column_number])? " " : "│ ") + " " * SPACE_SIZE
135
+ end
136
+ puts "└─ ─ ── \e[#{MCOLOR}m<M>\e[m#{m.inspect}"
137
+ @last_check[nest+add+1] = true
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'object_tree/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "object_tree"
8
+ spec.version = ObjectTree::VERSION
9
+ spec.authors = ["siman-man"]
10
+ spec.email = ["k128585@ie.u-ryukyu.ac.jp"]
11
+ spec.description = "this is object tree"
12
+ spec.summary = "hoge piyo"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - siman-man
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: this is object tree
42
+ email:
43
+ - k128585@ie.u-ryukyu.ac.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - example/sample.rb
54
+ - example/sample.rb~
55
+ - lib/object_tree.rb
56
+ - lib/object_tree.rb~
57
+ - lib/object_tree/version.rb
58
+ - object_tree.gemspec
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: hoge piyo
83
+ test_files: []