Module: Banalize

Defined in:
lib/banalize.rb,
lib/banalize/files.rb,
lib/banalize/errors.rb,
lib/banalize/parser.rb,
lib/banalize/policy.rb,
lib/banalize/runner.rb,
lib/helpers/beautify.rb,
lib/banalize/registry.rb,
lib/banalize/exception.rb,
lib/helpers/description.rb,
lib/banalize/parser/numbered.rb,
lib/banalize/policy/severity.rb,
lib/banalize/parser/variables.rb,
lib/banalize/parser/pod_comments.rb

Overview

This is shamelessly stolen form Rspec::DSL. Inject method ‘policy` into the top level namespace, so the we can use in policy definition without need to define class inheritance.

Registerd policies are listed in ‘@@policies` array.

Defined Under Namespace

Modules: DSL Classes: BanalizeError, Describe, Errors, Files, Numbered, Parser, Policy, Registry, Runner

Constant Summary collapse

ROOT =
File.dirname(File.dirname(__FILE__))
VERSION =
File.read(ROOT+'/version.txt').chomp.strip
USER =
{
  dot_banalize: dot_banalize,
  config:       "#{dot_banalize}/config", # Default user config file
  styles:       "#{dot_banalize}/style",  # Default style file
  policies:     "#{dot_banalize}/policies"
}
TRUNCATE =

Truncate long error lines

ENV["BANALIZE_TRUNCATE_LINES"] == 'false' ? nil : 60

Class Method Summary collapse

Class Method Details

.beautify(hash, indent = ' ') ⇒ Object

Make ouput text indented and apply colors to it if this is required

Parameters:

  • hash (Hash)

    check result hash

  • indent (String) (defaults to: ' ')

    Default indent 4 spaces for each level



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/helpers/beautify.rb', line 8

def self.beautify hash, indent='    '
  out = ''
  l1 = "\n"+indent
  l2 = "\n" << indent*2
  l3 = "\n" << indent*2 << "## "

  hash.each do |file,results|
    out << "\n\n#{file.color(:file)}"

    results.each do |f_or_s, checks|

      
      out << l1 << f_or_s << l1 << '-'*10

      checks.each do |policy, string|
        out << l1 << policy.to_s.color(f_or_s == 'Fail' ? :red : :yellow)
        if string
          lines = case string
                  when String
                    string.split("\n")
                  when Array
                    string
                  end
          lines.each do |line| 
            line.gsub!(/(line \d+:)(.*)/, '\1' +"#{l3}    "+ '\2' )
            out << l3 << line
          end

        end
      end
    end
    
  end
  out
end

.run(bash, search) ⇒ Array of Hashes

Run policy check and return result of it.

Parameters:

  • bash (String)

    Bash file for analisys

  • search (String, Hash)

    Name of a policy to check against or hash having :severity and/or :policy keys

Returns:

  • (Array of Hashes)

    each element of array is { :name => result }

See Also:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/banalize/runner.rb', line 15

def self.run bash, search

  $current = bash

  run_list = Policy.search search

  if run_list.empty?
    raise Banalize::Runner::Error, "No policy satisfying criteria: #{search.inspect}"
  end

  res = { }
  run_list.each do |policy|
    res[policy[:policy]] = Banalize::Runner.new(bash, policy).result
  end
  res
end