Sha256: 770108dc2f156181bc9c1c91f7138685335d82d5b4e982579923f0de04eb2bf7

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'nokogiri'
require 'active_support/inflector'
require 'xsd_model/document_attributes'
require 'xsd_model/elements/requires'

module XsdModel
  class Parser
    using DocumentAttributes

    def self.call(xml_string, options = {})
      new(xml_string, options).call
    end

    def initialize(xml_string, options)
      @xml = Nokogiri(xml_string)
      @options = options
    end

    def call
      parse_node(@xml)
    end

    private

    def parse_node(node)
      klass = XsdModel::Elements.const_get node.name.camelize

      children = collect_children(node).map { |child| parse_node(child) }
      attributes = node.attributes.transform_values(&:value)
      namespaces = node.namespaces

      klass.new(children, attributes: attributes, namespaces: namespaces, css_path: node.css_path)
    end

    def collect_children(node)
      skippable, collectible = node.children.partition do |child|
        (@options[:collect_only] && !@options[:collect_only].call(child)) ||
          (@options[:ignore] && @options[:ignore].call(child))
      end

      collectible + skippable.flat_map { |child| collect_children(child) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xsd_model-0.7.0 lib/xsd_model/parser.rb