Class: Kwalify::YamlParser

Inherits:
PlainYamlParser show all
Defined in:
lib/kwalify/yaml-parser.rb

Overview

(OBSOLETE) yaml parser

this class has been obsoleted. use Kwalify::Yaml::Parser instead.

ex.

# load document with YamlParser
str = ARGF.read()
parser = Kwalify::YamlParser.new(str)
document = parser.parse()

# validate document
schema = YAML.load(File.read('schema.yaml'))
validator = Kwalify::Validator.new(schema)
errors = validator.validate(document)

# print validation result
if errors && !errors.empty?
  parser.set_errors_linenum(errors)
  errors.sort.each do |error|
    print "line %d: path %s: %s" % [error.linenum, error.path, error.message]
  end
end

Direct Known Subclasses

Parser

Instance Method Summary collapse

Methods inherited from PlainYamlParser

#has_next?, #parse_all

Constructor Details

#initialize(*args) ⇒ YamlParser

Returns a new instance of YamlParser.



754
755
756
757
# File 'lib/kwalify/yaml-parser.rb', line 754

def initialize(*args)
  super
  @linenums_table = {}     # object_id -> hash or array
end

Instance Method Details

#parseObject



759
760
761
762
# File 'lib/kwalify/yaml-parser.rb', line 759

def parse()
  @doc = super()
  return @doc
end

#path_linenum(path) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/kwalify/yaml-parser.rb', line 764

def path_linenum(path)
  return 1 if path.empty? || path == '/'
  elems = path.split('/')
  elems.shift if path[0] == ?/    # delete empty string on head
  last_elem = elems.pop
  c = @doc   # collection
  elems.each do |elem|
    if c.is_a?(Array)
      c = c[elem.to_i]
    elsif c.is_a?(Hash)
      c = c[elem]
    else
      assert false
    end
  end
  linenums = @linenums_table[c.__id__]
  if c.is_a?(Array)
    linenum = linenums[last_elem.to_i]
  elsif c.is_a?(Hash)
    linenum = linenums[last_elem]
  end
  return linenum
end

#set_error_linenums(errors) ⇒ Object



794
795
796
797
# File 'lib/kwalify/yaml-parser.rb', line 794

def set_error_linenums(errors)
  warn "*** Kwalify::YamlParser#set_error_linenums() is obsolete. You should use set_errors_linenum() instead."
  set_errors_linenum(errors)
end

#set_errors_linenum(errors) ⇒ Object



788
789
790
791
792
# File 'lib/kwalify/yaml-parser.rb', line 788

def set_errors_linenum(errors)
  errors.each do |error|
    error.linenum = path_linenum(error.path)
  end
end