Class: RipperTags::DefaultFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ripper-tags/default_formatter.rb

Direct Known Subclasses

EmacsFormatter, JSONFormatter, VimFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DefaultFormatter

Returns a new instance of DefaultFormatter.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ripper-tags/default_formatter.rb', line 10

def initialize(options)
  @options = options

  if @options.extra_flags
    unsupported = @options.extra_flags - supported_flags.to_set
    if unsupported.any?
      raise FatalError, "these flags are not supported in the '%s' format: %s" % [
        options.format,
        unsupported.to_a.join(", ")
      ]
    end
  end
end

Instance Attribute Details

#optionsObject (readonly)

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

#constant?(tag) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ripper-tags/default_formatter.rb', line 61

def constant?(tag)
  tag[:kind] == 'class' || tag[:kind] == 'module' || tag[:kind] == 'constant'
end

#display_inheritance(tag) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/ripper-tags/default_formatter.rb', line 73

def display_inheritance(tag)
  if 'class' == tag[:kind] && tag[:inherits]
    " < #{tag[:inherits]}"
  else
    ""
  end
end

#display_kind(tag) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ripper-tags/default_formatter.rb', line 65

def display_kind(tag)
  case tag.fetch(:kind)
  when /method$/ then 'def'
  when /^const/  then 'const'
  else tag[:kind]
  end
end

#extra_flag?(flag) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ripper-tags/default_formatter.rb', line 26

def extra_flag?(flag)
  options.extra_flags && options.extra_flags.include?(flag)
end

#format(tag) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/ripper-tags/default_formatter.rb', line 81

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



52
53
54
55
56
57
58
59
# File 'lib/ripper-tags/default_formatter.rb', line 52

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

Returns:

  • (Boolean)


30
31
32
# File 'lib/ripper-tags/default_formatter.rb', line 30

def stdout?
  '-' == options.tag_file_name
end

#supported_flagsObject



24
# File 'lib/ripper-tags/default_formatter.rb', line 24

def supported_flags() [] end

#tag_file_dirObject



48
49
50
# File 'lib/ripper-tags/default_formatter.rb', line 48

def tag_file_dir
  @tag_file_dir ||= Pathname.new(options.tag_file_name).dirname.expand_path
end

#with_outputObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ripper-tags/default_formatter.rb', line 34

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



90
91
92
# File 'lib/ripper-tags/default_formatter.rb', line 90

def write(tag, io)
  io.puts format(tag)
end