Sha256: c7dbb5360263ba207a6a2cc30919c98228707b8bfdf307864ae4e967850c4b17

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require "nokogiri"

module Lutaml
  module Model
    module Schema
      class XsdSchema
        def self.generate(klass, _options = {})
          schema = Nokogiri::XML::Builder.new do |xml|
            xml.schema(xmlns: "http://www.w3.org/2001/XMLSchema") do
              xml.element(name: klass.name) do
                xml.complexType do
                  xml.sequence { generate_elements(klass, xml) }
                end
              end
            end
          end
          schema.to_xml
        end

        def self.generate_elements(klass, xml)
          klass.attributes.each do |name, attr|
            xml.element(name: name, type: get_xsd_type(attr.type))
          end
        end

        def self.get_xsd_type(type)
          {
            Lutaml::Model::Type::String => "xs:string",
            Lutaml::Model::Type::Integer => "xs:integer",
            Lutaml::Model::Type::Boolean => "xs:boolean",
            Lutaml::Model::Type::Float => "xs:float",
            Lutaml::Model::Type::Decimal => "xs:decimal",
            Lutaml::Model::Type::Array => "xs:array",
            Lutaml::Model::Type::Hash => "xs:object",
          }[type] || "xs:string" # Default to string for unknown types
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lutaml-model-0.3.3 lib/lutaml/model/schema/xsd_schema.rb
lutaml-model-0.3.2 lib/lutaml/model/schema/xsd_schema.rb