Class: Pedant::Check

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

Constant Summary collapse

@@statuses =
{
  :fatal => 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
# 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]
  Check.run_checks_in_dependency_order(kb, self.class.depends)
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



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

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

.dependsObject



83
84
85
86
87
88
89
# File 'lib/pedant/check.rb', line 83

def self.depends
  keys = self.requires

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

.friendly_nameObject



145
146
147
148
# File 'lib/pedant/check.rb', line 145

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



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

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



57
58
59
# File 'lib/pedant/check.rb', line 57

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

.providesObject



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

def self.provides
  return []
end

.ready?(kb) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/pedant/check.rb', line 77

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

.requiresObject



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

def self.requires
  return []
end

.run_checks_in_dependency_order(kb, checks) ⇒ Object



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

def self.run_checks_in_dependency_order(kb, checks)
  # Try to run each pending check, until we've run all our checks or
  # deadlocked.
  fatal = false
  run_checks = []
  until checks.empty? || fatal
    # Find all of the checks that can run right now.
    ready = checks.select { |cls| cls.ready?(kb) }
    break if ready.empty?

    # Run all of the checks that are ready.
    ready.each do |cls|
      # Create a new check instance.
      chk = cls.new(kb)
      checks.delete(cls)

      chk.run

      # Yield the results of the finished check
      run_checks << chk

      # Fatal errors mean that no further checks should be processed.
      if chk.result == :fatal
        fatal = true
        break
      end
    end
  end
  run_checks
end

Instance Method Details

#failObject



150
151
152
# File 'lib/pedant/check.rb', line 150

def fail
  @result = :fail
end

#fatalObject



154
155
156
157
# File 'lib/pedant/check.rb', line 154

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

#passObject



159
160
161
# File 'lib/pedant/check.rb', line 159

def pass
  @result = :pass
end

#report(level, text = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pedant/check.rb', line 122

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



163
164
165
# File 'lib/pedant/check.rb', line 163

def skip
  @result = :skip
end

#warnObject



167
168
169
# File 'lib/pedant/check.rb', line 167

def warn
  @result = :warn
end