Class: Fdlint::Validator

Inherits:
Object show all
Includes:
Helper::Logger
Defined in:
lib/fdlint/validator.rb

Constant Summary

Constants included from Helper::Logger

Helper::Logger::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Logger

#log, #logger

Constructor Details

#initialize(path = nil, options = {}) ⇒ Validator

Returns a new instance of Validator.



17
18
19
20
21
22
# File 'lib/fdlint/validator.rb', line 17

def initialize( path = nil, options = {} )
  @file      = path
  @source    = options[:text]
  @syntax    = options[:syntax] || Helper::CodeType.guess( source, file )
  @log_level = options[:log_level]
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/fdlint/validator.rb', line 13

def file
  @file
end

#log_levelObject (readonly)

Returns the value of attribute log_level.



13
14
15
# File 'lib/fdlint/validator.rb', line 13

def log_level
  @log_level
end

#resultsObject (readonly)

Returns the value of attribute results.



13
14
15
# File 'lib/fdlint/validator.rb', line 13

def results
  @results
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/fdlint/validator.rb', line 13

def source
  @source
end

#syntaxObject (readonly)

Returns the value of attribute syntax.



13
14
15
# File 'lib/fdlint/validator.rb', line 13

def syntax
  @syntax
end

Instance Method Details

#validateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fdlint/validator.rb', line 24

def validate
  @results = []

  begin
    @source ||= read_file

    validate_file if file
    validate_content
  rescue EncodingError
    results << InvalidFileEncoding.new
  end

  filter_results_by_log_level if log_level

  if block_given?
    yield file, source, results
  else
    results
  end
end

#validate_contentObject



54
55
56
57
58
59
60
# File 'lib/fdlint/validator.rb', line 54

def validate_content
  if source.valid_encoding?
    self << parse.flatten
  else
    self << [InvalidFileEncoding.new]
  end
end

#validate_fileObject



45
46
47
48
49
50
51
52
# File 'lib/fdlint/validator.rb', line 45

def validate_file
  debug { "validating file: " << file }
  file = File.new( self.file )
  entries = file_level_rules.map do |validation|
    validation.exec( file, source, file )
  end
  self << entries.flatten.compact
end