Class: RipperTags::DefaultFormatter
- Inherits:
-
Object
- Object
- RipperTags::DefaultFormatter
show all
- Defined in:
- lib/ripper-tags/default_formatter.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DefaultFormatter.
10
11
12
13
14
|
# File 'lib/ripper-tags/default_formatter.rb', line 10
def initialize(options)
@options = options
check_supported_flags(@options., supported_flags)
check_supported_flags(@options.fields, supported_fields)
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
8
9
10
|
# File 'lib/ripper-tags/default_formatter.rb', line 8
def options
@options
end
|
Instance Method Details
#check_supported_flags(set, supported) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/ripper-tags/default_formatter.rb', line 16
def check_supported_flags(set, supported)
if set
unsupported = set - supported.to_set
if unsupported.any?
raise FatalError, "these fields are not supported in the '%s' format: %s" % [
options.format,
unsupported.to_a.join(", ")
]
end
end
end
|
#constant?(tag) ⇒ Boolean
71
72
73
|
# File 'lib/ripper-tags/default_formatter.rb', line 71
def constant?(tag)
tag[:kind] == 'class' || tag[:kind] == 'module' || tag[:kind] == 'constant'
end
|
#display_inheritance(tag) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/ripper-tags/default_formatter.rb', line 83
def display_inheritance(tag)
if 'class' == tag[:kind] && tag[:inherits]
" < #{tag[:inherits]}"
else
""
end
end
|
#display_kind(tag) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/ripper-tags/default_formatter.rb', line 75
def display_kind(tag)
case tag.fetch(:kind)
when /method$/ then 'def'
when /^const/ then 'const'
else tag[:kind]
end
end
|
30
31
32
|
# File 'lib/ripper-tags/default_formatter.rb', line 30
def (flag)
options. && options..include?(flag)
end
|
#field?(field) ⇒ Boolean
36
37
38
|
# File 'lib/ripper-tags/default_formatter.rb', line 36
def field?(field)
options.fields && options.fields.include?(field)
end
|
91
92
93
94
95
96
97
98
|
# File 'lib/ripper-tags/default_formatter.rb', line 91
def format(tag)
"%5s %8s %s%s" % [
tag.fetch(:line).to_s,
display_kind(tag),
tag.fetch(:full_name),
display_inheritance(tag)
]
end
|
#relative_path(tag) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/ripper-tags/default_formatter.rb', line 62
def relative_path(tag)
path = tag.fetch(:path)
if options.tag_relative && !stdout? && path.index('/') != 0
Pathname.new(path).expand_path.relative_path_from(tag_file_dir).to_s
else
path
end
end
|
#stdout? ⇒ Boolean
40
41
42
|
# File 'lib/ripper-tags/default_formatter.rb', line 40
def stdout?
'-' == options.tag_file_name
end
|
#supported_fields ⇒ Object
34
|
# File 'lib/ripper-tags/default_formatter.rb', line 34
def supported_fields() [] end
|
#supported_flags ⇒ Object
28
|
# File 'lib/ripper-tags/default_formatter.rb', line 28
def supported_flags() [] end
|
#tag_file_dir ⇒ Object
58
59
60
|
# File 'lib/ripper-tags/default_formatter.rb', line 58
def tag_file_dir
@tag_file_dir ||= Pathname.new(options.tag_file_name).dirname.expand_path
end
|
#with_output ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ripper-tags/default_formatter.rb', line 44
def with_output
if stdout?
begin
yield $stdout
rescue Errno::EINVAL
raise BrokenPipe
end
else
File.open(options.tag_file_name, 'w+') do |outfile|
yield outfile
end
end
end
|
#write(tag, io) ⇒ Object
100
101
102
|
# File 'lib/ripper-tags/default_formatter.rb', line 100
def write(tag, io)
io.puts format(tag)
end
|