Module: AssertValidContent

Defined in:
lib/assert_valid_content/validator.rb,
lib/assert_valid_content.rb,
lib/assert_valid_content/assertions.rb

Overview

:nodoc

Defined Under Namespace

Modules: Assertions, LibXML, W3C Classes: Validator

Constant Summary collapse

TempDir =
if defined? RAILS_ROOT
  File.join RAILS_ROOT, 'tmp/validate'
else
  File.join Dir.tmpdir, 'assert_valid_content'
end
@@validators =
Hash.new { |h, k|  h[k] = Set.new }

Class Method Summary collapse

Class Method Details

.valid_content?(type, src) {|val| ... } ⇒ Boolean

Returns true if the content from src is valid for type type. Yields on failure. src is one of String, Pathname, or IO object

Yields:

  • (val)

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/assert_valid_content/validator.rb', line 12

def self.valid_content?( type, src )  # :yields: validator
  validators = AssertValidContent.validators[type]
  raise "No validators for type: #{type}"  if validators.empty?

  content = case src
    when String    then src
    when Pathname  then IO.read src
    when IO        then src.read
    else           raise "Unknown content src type: #{src.class}"
  end

  val = nil
  valid = validators.all? do |cl|
    val = cl.new
    val.validate content
    val.errors.empty?
  end

  yield(val)  if block_given? and not valid
  valid
end

.validatorsObject

Hash of type to sets of Validator objects for that type.



7
# File 'lib/assert_valid_content/validator.rb', line 7

def self.validators ; @@validators ; end