Module: AutoTypeDoc
- Defined in:
- lib/auto_type_doc.rb,
lib/auto_type_doc/version.rb,
lib/auto_type_doc/argument.rb,
lib/auto_type_doc/method_info.rb
Defined Under Namespace
Classes: Argument, MethodInfo
Constant Summary
collapse
- METHOD_BLACK_LIST =
%r{
AutoTypeDoc
}x
- LOCATION_BLACK_LIST =
%r{
rubies/
|/gems/
|/spec/
|/test/
}x
- Tracker =
TracePoint.new(:call, :return) do |t|
begin
method = t.self.method(t.method_id)
rescue
next
end
source_location = method.source_location
next if source_location.nil?
source_path = source_location[0]
next if source_path =~ LOCATION_BLACK_LIST
next if method.to_s =~ METHOD_BLACK_LIST
next if method_name_with_owner(method).start_with?("#<")
case t.event
when :call
next if method.parameters.empty?
on_call(t, method: method)
when :return
on_return(t, method: method)
end
end
- VERSION =
"0.1.1"
Class Method Summary
collapse
Class Method Details
.all_method_info ⇒ Object
103
104
105
|
# File 'lib/auto_type_doc.rb', line 103
def all_method_info
@all_method_info ||= {}
end
|
.disable ⇒ Object
111
112
113
|
# File 'lib/auto_type_doc.rb', line 111
def disable
Tracker.disable
end
|
.doc_dir ⇒ Object
115
116
117
|
# File 'lib/auto_type_doc.rb', line 115
def doc_dir
'./type_doc'
end
|
.dump_json ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/auto_type_doc.rb', line 119
def dump_json
FileUtils.mkdir_p(doc_dir)
File.open("#{doc_dir}/types.json", 'w') do |f|
f.write(JSON.pretty_generate(all_method_info))
end
end
|
.enable ⇒ Object
107
108
109
|
# File 'lib/auto_type_doc.rb', line 107
def enable
Tracker.enable
end
|
.method_info(method) ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'lib/auto_type_doc.rb', line 93
def method_info(method)
key = method_name_with_owner(method)
AutoTypeDoc.all_method_info[key] ||= MethodInfo.new(
source_location: method.source_location
)
AutoTypeDoc.all_method_info[key]
end
|
.method_name_with_owner(method) ⇒ String
51
52
53
|
# File 'lib/auto_type_doc.rb', line 51
def method_name_with_owner(method)
method.to_s[/^#<Method:\s(.+)>$/, 1].sub(/^.+\((.+)\)/, '\1')
end
|
.on_call(t, method:) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/auto_type_doc.rb', line 55
def on_call(t, method:)
method_info = method_info(method)
method.parameters.each_with_index do |(kind, name), index|
begin
obj = t.binding.local_variable_get(name)
rescue
next
end
method_info.add_argument(
name: name,
type: type(obj),
kind: kind,
position: index
)
end
end
|
.on_return(t, method:) ⇒ Object
87
88
89
90
91
|
# File 'lib/auto_type_doc.rb', line 87
def on_return(t, method:)
method_info = method_info(method)
obj = t.return_value
method_info.add_return_type(type(obj))
end
|
.type(obj) ⇒ String
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/auto_type_doc.rb', line 75
def type(obj)
if obj.is_a?(Array) && obj.any?
return "#{obj.class}<#{type(obj.first)}>"
end
if obj.equal?(true) || obj.equal?(false)
return "Boolean"
end
obj.class
end
|