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
# File 'lib/ripper-tags/default_formatter.rb', line 10

def initialize(options)
  @options = options
  check_supported_flags(@options.extra_flags, supported_flags)
  check_supported_flags(@options.fields, supported_fields)
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

#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

Returns:

  • (Boolean)


78
79
80
# File 'lib/ripper-tags/default_formatter.rb', line 78

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

#display_inheritance(tag) ⇒ Object



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

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

#display_kind(tag) ⇒ Object



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

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)


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

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

#field?(field) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ripper-tags/default_formatter.rb', line 36

def field?(field)
  options.fields && options.fields.include?(field)
end

#format(tag) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/ripper-tags/default_formatter.rb', line 98

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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ripper-tags/default_formatter.rb', line 64

def relative_path(tag)
  path = tag.fetch(:path)
  case options.tag_relative
  when true, 'yes', 'always'
    filepath = Pathname.new(path)
    if options.tag_relative == 'always' || filepath.relative?
      path = filepath.expand_path.relative_path_from(tag_file_dir).to_s
    end
  when 'never'
    path = File.expand_path(path)
  end
  path
end

#stdout?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ripper-tags/default_formatter.rb', line 40

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

#supported_fieldsObject



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

def supported_fields() [] end

#supported_flagsObject



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

def supported_flags() [] end

#tag_file_dirObject



58
59
60
61
62
# File 'lib/ripper-tags/default_formatter.rb', line 58

def tag_file_dir
  @tag_file_dir ||= stdout? ?
    Pathname.pwd :
    Pathname.new(options.tag_file_name).dirname.expand_path
end

#with_outputObject



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



107
108
109
# File 'lib/ripper-tags/default_formatter.rb', line 107

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