Class: Pedant::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/pedant/check.rb

Constant Summary collapse

@@statuses =
{
  :died => Rainbow('DIED').color(:red),
  :fail => Rainbow('FAIL').color(:red),
  :pass => Rainbow('PASS').color(:green),
  :skip => Rainbow('SKIP').color(:green),
  :warn => Rainbow('WARN').color(:yellow),
  :void => Rainbow('VOID').color(:magenta)
}
@@levels =
[:error, :warn, :info]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kb) ⇒ Check

Returns a new instance of Check.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pedant/check.rb', line 46

def initialize(kb)
  @report = []
  @result = :void

  @kb = kb

  # Run all the dependencies for this check if we're in test mode.
  return unless @kb[:test_mode]
  self.class.depends.each do |cls|
    chk = cls.new(@kb)
    chk.run
  end
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



29
30
31
# File 'lib/pedant/check.rb', line 29

def result
  @result
end

Class Method Details

.allObject



64
65
66
# File 'lib/pedant/check.rb', line 64

def self.all
  (@_all ||= [])
end

.dependsObject



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

def self.depends
  keys = self.requires

  Check.all.reject do |cls|
    (cls.provides & keys).empty?
  end
end

.friendly_nameObject



117
118
119
120
# File 'lib/pedant/check.rb', line 117

def self.friendly_name
  # Mangle the class name to be more user-friendly.
  self.name.gsub(/.*::/, '').gsub(/^Check/, '').gsub(/[A-Z][^A-Z]*/, ' \&').strip
end

.inherited(cls) ⇒ Object



68
69
70
# File 'lib/pedant/check.rb', line 68

def self.inherited(cls)
  all << cls
end

.initialize!Object



42
43
44
# File 'lib/pedant/check.rb', line 42

def self.initialize!
  Dir.glob(Pedant.lib + 'pedant/checks/*.rb').each { |f| load(f) }
end

.listObject



60
61
62
# File 'lib/pedant/check.rb', line 60

def self.list
  all.map{ |cls| cls.friendly_name }.sort
end

.providesObject



72
73
74
# File 'lib/pedant/check.rb', line 72

def self.provides
  return []
end

.ready?(kb) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/pedant/check.rb', line 80

def self.ready?(kb)
  self.requires.reduce(true) do |stat, req|
    stat && kb.has_key?(req)
  end
end

.requiresObject



76
77
78
# File 'lib/pedant/check.rb', line 76

def self.requires
  return []
end

Instance Method Details

#failObject



122
123
124
# File 'lib/pedant/check.rb', line 122

def fail
  @result = :fail
end

#fatalObject



126
127
128
129
# File 'lib/pedant/check.rb', line 126

def fatal
  report(:error, "This is a fatal error.")
  @result = :died
end

#passObject



131
132
133
# File 'lib/pedant/check.rb', line 131

def pass
  @result = :pass
end

#report(level, text = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pedant/check.rb', line 94

def report(level, text=nil)
  unless text.nil?
    if @@levels.index(level).nil?
      raise "Reporting level #{level} is not known."
    end

    @report << [level, text]
    return
  end

  # Convert level from symbol to an array index.
  level = @@levels.index(level) if level.is_a?(Symbol)

  # Format all components of a report at or below the specified level.
  msg = @report.select { |l, t| @@levels.index(l) <= level }.map { |l, t| t }.join("\n")
  msg << "\n" unless msg.empty?

  # Format the check's result.
  msg = "[#{@@statuses[@result]}] #{self.class.friendly_name}\n#{msg}"

  return msg
end

#skipObject



135
136
137
# File 'lib/pedant/check.rb', line 135

def skip
  @result = :skip
end

#warnObject



139
140
141
# File 'lib/pedant/check.rb', line 139

def warn
  @result = :warn
end