ri_for 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.0
@@ -1,4 +1,4 @@
1
1
  require 'sane'
2
- for file in Dir[__dir__ + '/ri_for/*'] do
2
+ for file in Dir[__dir__ + '/ri_for/*.rb'] do
3
3
  require file
4
4
  end
@@ -6,6 +6,8 @@ class Class
6
6
  # want_output = false, verbose = false
7
7
  begin
8
8
  puts "begin RI"
9
+ require 'rdoc'
10
+ require 'rdoc/ri/driver'
9
11
  RDoc::RI::Driver.run [to_s, '--no-pager']
10
12
  puts 'end ri'
11
13
  rescue SystemExit
@@ -1,9 +1,7 @@
1
- require 'rubygems'
2
- require 'rdoc'
3
- require 'rdoc/ri/driver'
4
1
  require 'sane'
5
2
 
6
3
  if RUBY_VERSION < '1.9'
4
+ require 'rubygems'
7
5
  require 'ruby2ruby'
8
6
  require 'parse_tree'
9
7
  gem 'rdp-arguments' # TODO why is this necessary?
@@ -55,17 +53,16 @@ module SourceLocationDesc
55
53
  string =~ /Method: .*([#\.])(.*)>/ # include the # or .
56
54
  joiner = $1
57
55
  method_name = $2
58
- full_name = "#{class_name}#{joiner}#{method_name} arity #{arity}"
59
- sig = "sig: #{full_name} arity: #{arity}"
56
+ sig = "sig: #{class_name}#{joiner}#{method_name} arity #{arity}"
60
57
  doc << sig
61
58
  param_string = sig
62
-
59
+
63
60
  # now gather up any other information we now about it, in case there are no rdocs, so we can see it early...
64
61
 
65
62
  if !(respond_to? :source_location)
66
63
  # pull out names for 1.8
67
64
  begin
68
- klass = eval(class_name)
65
+ klass = eval(class_name)
69
66
  # we don't call to_ruby to overcome ruby2ruby bug http://rubyforge.org/tracker/index.php?func=detail&aid=26891&group_id=1513&atid=5921
70
67
  if joiner == '#'
71
68
  raw_code = ParseTree.new.parse_tree_for_method(klass, method_name)
@@ -73,7 +70,7 @@ module SourceLocationDesc
73
70
  raw_code = ParseTree.new.parse_tree_for_method(klass.singleton_class, method_name)
74
71
  end
75
72
  doc << Ruby2Ruby.new.process(ParseTree.new.process(raw_code))
76
-
73
+
77
74
  args = Arguments.names(klass, method_name, false) rescue Arguments.names(klass.singleton_class, method_name, false)
78
75
  out = []
79
76
  args.each{|arg_pair|
@@ -82,11 +79,11 @@ module SourceLocationDesc
82
79
  out = out.join(', ')
83
80
  return out if want_just_summary
84
81
 
85
- param_string = "Parameters: #{method_name}(" + out + ")"
82
+ param_string = "Parameters: #{method_name}(" + out + ")"
86
83
  doc << param_string unless want_the_description_returned
87
84
  rescue Exception => e
88
85
  doc << "appears to be a c method"
89
- puts "fail to parse tree: #{class_name} #{e} #{e.backtrace}" if $VERBOSE
86
+ puts "fail to parse tree: #{class_name} #{e} #{e.backtrace}" if $DEBUG
90
87
  doc << "appears to be a c method"
91
88
  end
92
89
  else
@@ -128,48 +125,52 @@ module SourceLocationDesc
128
125
  end
129
126
  end
130
127
 
131
- puts doc # always output it since RI does currently [todo make optional I suppose, and non out-putty]
128
+ puts doc unless want_the_description_returned
132
129
 
130
+ unless (already_got_ri || want_just_summary)
131
+ require 'rdoc'
132
+ require 'rdoc/ri/driver'
133
133
 
134
- # now run default RI for it
135
- begin
136
- puts 'Searching ri for ' + full_name + "..."
137
- RDoc::RI::Driver.run [full_name, '--no-pager'] unless want_just_summary
138
- rescue *[StandardError, SystemExit]
139
- # not found
140
- ensure
141
- puts '(end ri)'
142
- end unless already_got_ri
134
+ # show default RI for it
135
+ begin
136
+ pps 'Searching ri for', sig, '...'
137
+ RDoc::RI::Driver.run [full_name, '--no-pager']
138
+ rescue *[StandardError, SystemExit]
139
+ # not found
140
+ ensure
141
+ puts '(end ri)'
142
+ end
143
+ end
143
144
 
144
145
  if want_the_description_returned # give them something they can examine
145
146
  doc
146
147
  else
147
- param_string # one liner
148
+ param_string.strip # return one liner
148
149
  end
149
- end
150
150
 
151
+ end
152
+
151
153
  alias :desc :ri
154
+
152
155
  end
153
156
 
154
157
  class Method; include SourceLocationDesc; end
155
158
  class UnboundMethod; include SourceLocationDesc; end
156
159
 
157
160
  # TODO mixin from a separate module
161
+
158
162
  class Object
159
163
  # currently rather verbose, but will attempt to describe all it knows about a method
160
164
  def ri_for name, options = {}
161
165
  if self.is_a?(Class) || self.is_a?(Module)
162
166
  # i.e. String.strip
163
167
  begin
164
- instance_method(name).ri(options)
168
+ instance_method(name).ri(options)
165
169
  rescue NameError => e #allow for Class.instance_method_name, Module.instance_method_name
166
- method(name).ri(options)
170
+ method(name).ri(options)
167
171
  end
168
172
  else
169
173
  method(name).desc(options)
170
174
  end
171
175
  end
172
176
  end
173
-
174
- # attribution
175
- # originally gleaned from http://p.ramaze.net/17901
@@ -1,8 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'ffi'
3
3
 
4
- $VERBOSE = true
5
-
6
4
  class A
7
5
  # a suh-weet rdoc
8
6
  def go(a=5)
@@ -16,7 +14,6 @@ end
16
14
 
17
15
  =begin
18
16
  doctest_require: '../lib/ri_for'
19
- >> $VERBOSE = true
20
17
  >> output = A.ri_for(:go, :want_the_description_returned => true).join(' ')
21
18
  >> output.include? 'a = 33'
22
19
  => true
@@ -41,7 +38,7 @@ it should return you something useful
41
38
  => false
42
39
 
43
40
  it should work with Module
44
- >> FFI::Library.ri_for(:attach_function) == nil
41
+ >> FFI::Library.ri_for(:attach_function, :want_the_description_returned => true).nil?
45
42
  => false
46
43
 
47
44
  it should say c method for c
@@ -96,9 +93,11 @@ it should display the name
96
93
 
97
94
  and arity
98
95
  >> Pathname.instance_method(:children).desc(:want_the_description_returned => true).grep(/arity/)
99
- => ["sig: Pathname#children arity -1 arity: -1"]
96
+ => ["sig: Pathname#children arity -1"]
100
97
 
101
- # todo: one that is guaranteed to exit you early [no docs at all ever]
98
+ it should not duplicate arity
99
+ >> A.ri_for(:go, :want_the_description_returned => true).join(' ').scan(/arity/).length
100
+ => 1
102
101
 
103
102
  wurx with class methods
104
103
  >> class A; def self.go(a = 3); a=5; end; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ri_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Pack