Class | Kwalify::YamlParser
In: | kwalify/yaml-parser.rb
Parent: | PlainYamlParser

(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

### Methods

add_to_map add_to_seq create_mapping create_scalar create_sequence merge_map new parse path_linenum set_default set_error_linenums set_errors_linenum set_map_with set_seq_at

### Public Class methods

new(*args)

[Source]

    # File kwalify/yaml-parser.rb, line 754
        def initialize(*args)
          super
          @linenums_table = {}     # object_id -> hash or array
        end

### Public Instance methods

parse()

[Source]

    # File kwalify/yaml-parser.rb, line 759
        def parse()
          @doc = super()
          return @doc
        end

path_linenum(path)

[Source]

    # File 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)

[Source]

    # File 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)

[Source]

    # File kwalify/yaml-parser.rb, line 788
        def set_errors_linenum(errors)
          errors.each do |error|
            error.linenum = path_linenum(error.path)
          end
        end

### Protected Instance methods

add_to_map(map, key, value, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 823
        def add_to_map(map, key, value, linenum)
          map[key] = value
          @linenums_table[map.__id__][key] = linenum
        end

add_to_seq(seq, value, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 807
        def add_to_seq(seq, value, linenum)
          seq << value
          @linenums_table[seq.__id__] << linenum
        end

create_mapping(linenum=current_linenum())

[Source]

    # File kwalify/yaml-parser.rb, line 817
        def create_mapping(linenum=current_linenum())
          map = {}
          @linenums_table[map.__id__] = {}
          return map
        end

create_scalar(value, linenum=current_linenum())

[Source]

    # File kwalify/yaml-parser.rb, line 852
        def create_scalar(value, linenum=current_linenum())
          data = super(value)
          #return Scalar.new(data, linenum)
          return data
        end

create_sequence(linenum=current_linenum())

[Source]

    # File kwalify/yaml-parser.rb, line 801
        def create_sequence(linenum=current_linenum())
          seq = []
          @linenums_table[seq.__id__] = []
          return seq
        end

merge_map(map, collection, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 838
        def merge_map(map, collection, linenum)
          t = @linenums_table[map.__id__]
          list = collection.is_a?(Array) ? collection : [ collection ]
          list.each do |m|
            t2 = @linenums_table[m.__id__]
            m.each do |key, val|
              unless map.key?(key)
                map[key] = val
                t[key] = t2[key]
              end
            end
          end
        end

set_default(map, value, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 833
        def set_default(map, value, linenum)
          map.default = value
          @linenums_table[map.__id__]['=''='] = linenum
        end

set_map_with(map, key, value, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 828
        def set_map_with(map, key, value, linenum)
          map[key] = value
          @linenums_table[map.__id__][key] = linenum
        end

set_seq_at(seq, i, value, linenum)

[Source]

    # File kwalify/yaml-parser.rb, line 812
        def set_seq_at(seq, i, value, linenum)
          seq[i] = value
          @linenums_table[seq.__id__][i] = linenum
        end

[Validate]