Class: AssertValidContent::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/assert_valid_content/validator.rb

Overview

Base class for all content validators

Direct Known Subclasses

LibXML::HTMLValidator, W3C::CSSValidator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Validator

:nodoc:



11
12
13
# File 'lib/assert_valid_content/validator.rb', line 11

def initialize( opts = {} )  #:nodoc:
  @src, @errors = nil, []
end

Instance Attribute Details

#errorsObject (readonly)

Validation errors



9
10
11
# File 'lib/assert_valid_content/validator.rb', line 9

def errors
  @errors
end

Instance Method Details

#to_sObject

Returns an error message listing validation errors (or nil if there were none).



16
17
18
19
20
# File 'lib/assert_valid_content/validator.rb', line 16

def to_s
  return nil  unless @errors
  "#{self.class.name} validation failed:\n" +
    @errors.map { |e|  "  #{e.to_s}" }.join("\n")
end

#validate(src, &blk) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/assert_valid_content/validator.rb', line 22

def validate( src, &blk )
  @src = src
  content = case src
    when Pathname  then IO.read src
    when IO        then src.read
    else           src
  end
  validate! content, &blk
end