rubywho 0.1.0 → 0.2.0

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.
@@ -0,0 +1,15 @@
1
+ # rubywho 0.2.0 2011/03/09
2
+ * Add commands.
3
+ * who_io?
4
+ * who?
5
+ * who_singleton?
6
+ * who_s?
7
+ * who_instance?
8
+ * who_i?
9
+ * who0?
10
+ * who1?
11
+
12
+ # rubywho 0.1.0 2011/03/09
13
+ * 1st Release.
14
+ * Add command, who?
15
+
@@ -1,6 +1,6 @@
1
1
  = rubywho
2
2
 
3
- Description goes here.
3
+ Ruby running on the object's class hierarchy, methods of each class to display a legible.
4
4
 
5
5
  == Contributing to rubywho
6
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -7,28 +7,73 @@
7
7
 
8
8
  class RubyWho
9
9
  module Adapter
10
- def who?(n = nil, filter = nil)
11
- tap{|x| puts "== who? #{x.inspect} =="; RubyWho.new(x, filter).display(n) }
10
+ def who_io?(io, filter = nil, level = nil, kind = nil)
11
+ tap do |obj|
12
+ if (level.nil? || level > 0)
13
+ io.puts "== #{obj.inspect}.who? =="
14
+ RubyWho.new(obj, io, filter, kind).display(level)
15
+ else
16
+ io.puts "#{obj.inspect} #: #{RubyWho.new(obj, io, filter, kind).obj_str}"
17
+ end
18
+ end
19
+ end
20
+
21
+ def who?(filter = nil, level = nil, kind = nil)
22
+ who_io?($stdout, filter, level, kind)
23
+ end
24
+
25
+ def who_singleton?(filter = nil, level = nil)
26
+ who?(filter, level, :singleton)
27
+ end
28
+
29
+ alias :who_s? :who_singleton?
30
+
31
+ def who_instance?(filter = nil, level = nil)
32
+ who?(filter, level, :instance)
33
+ end
34
+
35
+ alias :who_i? :who_instance?
36
+
37
+ def who0?(filter = nil)
38
+ who?(filter, 0)
39
+ end
40
+
41
+ def who1?(filter = nil)
42
+ who?(filter, 1)
12
43
  end
13
44
  end
14
45
 
15
46
  IGNORE = [RubyWho::Adapter]
16
47
  COLS = 80
17
48
 
18
- def initialize(obj, filter_re = nil)
49
+ def initialize(obj, io, filter_re, kind)
19
50
  @obj = obj
20
- @filter_re = filter_re
51
+ @io = io
52
+ @filter_re = filter_re.is_a?(String) ? Regexp.new(filter_re) : filter_re
53
+ @kind = kind
54
+ end
55
+
56
+ def obj_str
57
+ klass = @obj.is_a?(Module) ? @obj : @obj.class
58
+
59
+ if singleton?
60
+ klass.to_s + '(%s)' % klass.class
61
+ else
62
+ klass.to_s + '#'
63
+ end
21
64
  end
22
65
 
23
66
  def display(n = nil)
24
- if @obj.is_a?(Module)
25
- limit(@obj.ancestors, n).each do |klass|
26
- puts klass.to_s + '(%s)' % klass.class
67
+ obj_klass = @obj.is_a?(Module) ? @obj : @obj.class
68
+
69
+ if singleton?
70
+ limit(obj_klass.ancestors, n).each do |klass|
71
+ @io.puts klass.to_s + '(%s)' % klass.class
27
72
  display_methods(klass.singleton_methods(false))
28
73
  end
29
74
  else
30
- limit(@obj.class.ancestors, n).each do |klass|
31
- puts klass.to_s + '#'
75
+ limit(obj_klass.ancestors, n).each do |klass|
76
+ @io.puts klass.to_s + '#'
32
77
  display_methods(klass.public_instance_methods(false))
33
78
  end
34
79
  end
@@ -36,6 +81,10 @@ class RubyWho
36
81
 
37
82
  private
38
83
 
84
+ def singleton?
85
+ @obj.is_a?(Module) && @kind != :instance || @kind == :singleton
86
+ end
87
+
39
88
  def limit(ancestors, n)
40
89
  as = ancestors.reject{|a| IGNORE.include?(a) }
41
90
  n.nil? ? as : as[0, n]
@@ -45,7 +94,7 @@ class RubyWho
45
94
  alpha, op = methods.sort.partition{|m| m =~ /\A[_A-Za-z]/o }
46
95
  display_each_methods(alpha, COLS)
47
96
  display_each_methods(op, COLS)
48
- puts 'v' + '-' * (COLS - 1)
97
+ @io.puts 'v' + '-' * (COLS - 1)
49
98
  end
50
99
 
51
100
  def display_each_methods(methods, cols)
@@ -53,13 +102,13 @@ class RubyWho
53
102
  methods.each do |m|
54
103
  next if @filter_re && @filter_re !~ m
55
104
  if (buf + m).length > cols
56
- puts buf.sub(/\,\s*\Z/, '')
105
+ @io.puts buf.sub(/\,\s*\Z/, '')
57
106
  buf = '| ' + m + ', '
58
107
  else
59
108
  buf << m << ', '
60
109
  end
61
110
  end
62
- puts buf.sub(/\,\s*\Z/, '') unless buf == '| '
111
+ @io.puts buf.sub(/\,\s*\Z/, '') unless buf == '| '
63
112
  end
64
113
  end
65
114
  Object.send(:include, RubyWho::Adapter)
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubywho}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ongaeshi"]
@@ -13,11 +13,13 @@ Gem::Specification.new do |s|
13
13
  s.description = %q{Ruby running on the object's class hierarchy, methods of each class to display a legible.}
14
14
  s.email = %q{ongaeshi0621@gmail.com}
15
15
  s.extra_rdoc_files = [
16
+ "ChangeLog.markdown",
16
17
  "LICENSE.txt",
17
18
  "README.rdoc"
18
19
  ]
19
20
  s.files = [
20
21
  ".document",
22
+ "ChangeLog.markdown",
21
23
  "Gemfile",
22
24
  "Gemfile.lock",
23
25
  "LICENSE.txt",
@@ -1,11 +1,166 @@
1
- require 'helper'
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), "../lib/rubywho")
3
+ require 'stringio'
4
+
5
+ class TestRubyWho < Test::Unit::TestCase
6
+ def setup
7
+ @io = StringIO.new
8
+ @old_stdout = $stdout
9
+ $stdout = @io
10
+ end
11
+
12
+ def test_who_io?
13
+ io = StringIO.new
14
+
15
+ io.string = ""
16
+ 1.who_io?(io)
17
+ assert_match(/== 1\.who\? ==/, io.string)
18
+ assert_match(/Fixnum/, io.string)
19
+ assert_match(/Integer/, io.string)
20
+
21
+ io.string = ""
22
+ "string".who_io?(io)
23
+ assert_match(/== "string"\.who\? ==/, io.string)
24
+ assert_match(/String/, io.string)
25
+ assert_match(/lines, ljust, lstrip/, io.string)
26
+ assert_match(/Enumerable/, io.string)
27
+
28
+ io.string = ""
29
+ String.who_io?(io)
30
+ assert_match(/String\(Class\)/, io.string)
31
+
32
+ io.string = ""
33
+ [1, "cat", "cat", 2, "cat", 3].who_io?(io).select{|i| i == 'cat'}.who_io?(io).count.who_io?(io)
34
+ assert_match(/== \[1, "cat", "cat", 2, "cat", 3\]\.who\? ==\nArray/, io.string)
35
+ assert_match(/== \["cat", "cat", "cat"\]\.who\? ==\nArray/, io.string)
36
+ assert_match(/== 3\.who\? ==\nFixnum/, io.string)
37
+ end
2
38
 
3
- class TestRubywho < Test::Unit::TestCase
4
39
  def test_who?
40
+ @io.string = ""
41
+ who?
42
+ assert_match /TestRubyWho/, @io.string
43
+
44
+ @io.string = ""
5
45
  %w[a b c].who?
46
+ assert_match /Array/, @io.string
47
+
48
+ @io.string = ""
6
49
  1.who?
50
+ assert_match(/== 1\.who\? ==/, @io.string)
51
+ assert_match(/Fixnum/, @io.string)
52
+ assert_match(/Integer/, @io.string)
53
+
54
+ @io.string = ""
7
55
  "string".who?
56
+ assert_match(/== "string"\.who\? ==/, @io.string)
57
+ assert_match(/String/, @io.string)
58
+ assert_match(/lines, ljust, lstrip/, @io.string)
59
+ assert_match(/Enumerable/, @io.string)
60
+
61
+ @io.string = ""
8
62
  String.who?
63
+ assert_match(/String\(Class\)/, @io.string)
64
+
65
+ @io.string = ""
9
66
  [1, "cat", "cat", 2, "cat", 3].who?.select{|i| i == 'cat'}.who?.count.who?
67
+ String.who_io?(@io)
68
+ assert_match(/== \[1, "cat", "cat", 2, "cat", 3\]\.who\? ==\nArray/, @io.string)
69
+ assert_match(/== \["cat", "cat", "cat"\]\.who\? ==\nArray/, @io.string)
70
+ assert_match(/== 3\.who\? ==\nFixnum/, @io.string)
71
+
72
+ @io.string = ""
73
+ self.who?(/assert/)
74
+ assert_match(/assert_no_match/, @io.string)
75
+ assert_no_match(/clone/, @io.string)
76
+
77
+ @io.string = ""
78
+ self.who?("assert")
79
+ assert_match(/assert_no_match/, @io.string)
80
+ assert_no_match(/clone/, @io.string)
81
+ end
82
+
83
+ def test_who_singleton?
84
+ @io.string = ""
85
+ 1.who_singleton?
86
+ v1 = @io.string
87
+
88
+ @io.string = ""
89
+ Fixnum.who_singleton?
90
+ v2 = @io.string
91
+
92
+ assert_equal v1.to_a[1..-1], v2.to_a[1..-1]
93
+ assert_match /Fixnum\(Class\)/, v1
94
+ assert_no_match /Fixnum#/, v1
95
+
96
+ @io.string = ""
97
+ 1.who_s?
98
+ v1 = @io.string
99
+
100
+ @io.string = ""
101
+ Fixnum.who_s?
102
+ v2 = @io.string
103
+
104
+ assert_equal v1.to_a[1..-1], v2.to_a[1..-1]
105
+ assert_match /Fixnum\(Class\)/, v1
106
+ assert_no_match /Fixnum#/, v1
107
+ end
108
+
109
+ def test_who_instance?
110
+ @io.string = ""
111
+ 1.who_instance?
112
+ v1 = @io.string
113
+
114
+ @io.string = ""
115
+ Fixnum.who_instance?
116
+ v2 = @io.string
117
+
118
+ assert_equal v1.to_a[1..-1], v2.to_a[1..-1]
119
+ assert_no_match /Fixnum\(Class\)/, v1
120
+ assert_match /Fixnum#/, v1
121
+
122
+ @io.string = ""
123
+ 1.who_i?
124
+ v1 = @io.string
125
+
126
+ @io.string = ""
127
+ Fixnum.who_i?
128
+ v2 = @io.string
129
+
130
+ assert_equal v1.to_a[1..-1], v2.to_a[1..-1]
131
+ assert_no_match /Fixnum\(Class\)/, v1
132
+ assert_match /Fixnum#/, v1
133
+ end
134
+
135
+ def test_who0?
136
+ @io.string = ""
137
+ 1.who0?
138
+ assert_match(/1 #: Fixnum#/, @io.string)
139
+
140
+ @io.string = ""
141
+ [1, 2, 3].who0?
142
+ assert_match(/\[1, 2, 3\] #: Array#/, @io.string)
143
+
144
+ @io.string = ""
145
+ Array.who0?
146
+ assert_match(/Array #: Array\(Class\)/, @io.string)
147
+
148
+ @io.string = ""
149
+ [1, "cat", "cat", 2, "cat", 3].who0?.select{|i| i == 'cat'}.who0?.count.who0?
150
+ assert_match /\[1, "cat", "cat", 2, "cat", 3\] #: Array#/, @io.string
151
+ assert_match /\["cat", "cat", "cat"\] #: Array#/, @io.string
152
+ assert_match /3 #: Fixnum#/, @io.string
153
+ end
154
+
155
+ def test_who1?
156
+ @io.string = ""
157
+ 1.who1?
158
+ assert_match(/== 1\.who\? ==/, @io.string)
159
+ assert_no_match(/Integer/, @io.string)
160
+ end
161
+
162
+ def teardown
163
+ $stdout = @old_stdout
10
164
  end
11
165
  end
166
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - ongaeshi
@@ -64,10 +64,12 @@ executables: []
64
64
  extensions: []
65
65
 
66
66
  extra_rdoc_files:
67
+ - ChangeLog.markdown
67
68
  - LICENSE.txt
68
69
  - README.rdoc
69
70
  files:
70
71
  - .document
72
+ - ChangeLog.markdown
71
73
  - Gemfile
72
74
  - Gemfile.lock
73
75
  - LICENSE.txt