Class: Wool::Warning

Inherits:
Struct
  • Object
show all
Extended by:
Advice, ModuleExtensions
Includes:
LexicalAnalysis, SexpAnalysis
Defined in:
lib/wool/warning.rb

Direct Known Subclasses

FileWarning, LineWarning

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Advice

advice_counter, after_advice, argument_advice, before_advice, bump_advice_counter!, with_advice

Methods included from ModuleExtensions

attr_accessor_with_default, cattr_accessor, cattr_accessor_with_default, cattr_get_and_setter, cattr_reader, cattr_writer

Methods included from SexpAnalysis

#find_sexps, #parse

Methods included from LexicalAnalysis

#find_keyword, #find_token, #lex, #select_token, #split_on_keyword, #split_on_token, #text_between_token_positions

Constructor Details

#initialize(file, body, settings = {}) ⇒ Warning

Default initializer.



77
78
79
80
# File 'lib/wool/warning.rb', line 77

def initialize(file, body, settings={})
  super(self.class.short_desc, file, body, 0, self.class.severity)
  @settings = settings
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



2
3
4
# File 'lib/wool/warning.rb', line 2

def body
  @body
end

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



2
3
4
# File 'lib/wool/warning.rb', line 2

def file
  @file
end

#line_numberObject

Returns the value of attribute line_number

Returns:

  • (Object)

    the current value of line_number



2
3
4
# File 'lib/wool/warning.rb', line 2

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/wool/warning.rb', line 2

def name
  @name
end

#settingsObject

Returns the value of attribute settings.



11
12
13
# File 'lib/wool/warning.rb', line 11

def settings
  @settings
end

#severityObject

Returns the value of attribute severity

Returns:

  • (Object)

    the current value of severity



2
3
4
# File 'lib/wool/warning.rb', line 2

def severity
  @severity
end

Class Method Details

.all_typesObject

All types should be shared and modified by all subclasses. This makes Wool::Warning.all_types a global registry.



32
33
34
# File 'lib/wool/warning.rb', line 32

def self.all_types
  @@all_types ||= Hash.new {|h,k| h[k] = []}
end

.all_warningsObject

This tracks all subclasses (and subclasses of subclasses, etc). Plus, this method is inherited, so Wool::LineWarning.all_subclasses will have all subclasses of Wool::LineWarning!



18
19
20
# File 'lib/wool/warning.rb', line 18

def self.all_warnings
  @all_warnings ||= [self]
end

.concrete_warningsArray<Class>

Returns all “concrete” warnings, that is, those that have an actual implementation. No meta-warnings like FileWarning/LineWarning.

Returns:

  • (Array<Class>)

    the concrete warnings you might want to use



26
27
28
# File 'lib/wool/warning.rb', line 26

def self.concrete_warnings
  all_warnings - [self, FileWarning, LineWarning]
end

.inherited(klass) ⇒ Object

When a Warning subclass is subclassed, store the subclass and inform the next superclass up the inheritance hierarchy.



38
39
40
41
42
43
44
45
# File 'lib/wool/warning.rb', line 38

def self.inherited(klass)
  self.all_warnings << klass
  next_klass = self.superclass
  while next_klass != Wool::Warning.superclass
    next_klass.send(:inherited, klass)
    next_klass = next_klass.superclass
  end
end

.opt(*args) ⇒ Object

Adds an option in Trollop format.



53
54
55
# File 'lib/wool/warning.rb', line 53

def self.opt(*args)
  self.options << args
end

.optionsObject

Override in subclasses to provide a list of options to send to Trollop



48
49
50
# File 'lib/wool/warning.rb', line 48

def self.options
  @options ||= [:debug, "Shows debug output from wool's scanner", {:short => '-d'}]
end

.setting_accessor(*syms) ⇒ Object

Adds an instance method that extracts a key from the settings of the warning. This is a simple way of storing metadata about the discovered error/issue for presentational purposes.



72
73
74
# File 'lib/wool/warning.rb', line 72

def self.setting_accessor(*syms)
  syms.each { |sym| class_eval("def #{sym}\n  @settings[#{sym.inspect}]\nend") }
end

.type(*args) ⇒ Object

Modified cattr_get_and_setter that updates the class’s short_name and registers the class as a member of the given type.



59
60
61
62
63
64
65
66
67
# File 'lib/wool/warning.rb', line 59

def self.type(*args)
  if args.any?
    @type = args.first.to_s
    all_types[@type] << self
    self.short_name = @type[0,2].upcase + all_types[@type].size.to_s
  else
    @type
  end
end

Instance Method Details

#count_occurrences(string, substring) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/wool/warning.rb', line 114

def count_occurrences(string, substring)
  count = 0
  0.upto(string.size - substring.size) do |start|
    if string[start,substring.size] == substring
      count += 1
    end
  end
  count
end

#descObject



102
103
104
105
106
107
# File 'lib/wool/warning.rb', line 102

def desc
  case desc = self.class.desc
  when String then desc
  when Proc then instance_eval(&self.class.desc)
  end
end

#fixObject



94
95
96
# File 'lib/wool/warning.rb', line 94

def fix
  self.body
end

#fixable?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/wool/warning.rb', line 98

def fixable?
  self.fix != self.body rescue false
end

#generated_warnings(*args) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/wool/warning.rb', line 86

def generated_warnings(*args)
  case match_result = match?(*args)
  when Array then match_result
  when false, nil then []
  else [self]
  end
end

#get_indent(line = self.body) ⇒ Object



124
125
126
# File 'lib/wool/warning.rb', line 124

def get_indent(line = self.body)
  line =~ /^(\s*).*$/ ? $1 : ''
end

#indent(string, amt = nil) ⇒ Object



109
110
111
112
# File 'lib/wool/warning.rb', line 109

def indent(string, amt=nil)
  amt ||= self.body.match(/^(\s*)/)[1].size
  ' ' * amt + string.lstrip
end

#match?(body = self.body) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/wool/warning.rb', line 82

def match?(body = self.body)
  false
end