Sha256: 28cc7038d0d127a405c6e0ed47222b601f0a3d69576e4439e4e0830575b411ac

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

# lib/lutaml/model/schema/relaxng_schema.rb
require "nokogiri"

module Lutaml
  module Model
    module Schema
      class RelaxngSchema
        def self.generate(klass, options = {})
          schema = Nokogiri::XML::Builder.new do |xml|
            xml.element(name: klass.name) do
              xml.complexType do
                xml.sequence do
                  generate_elements(klass, xml)
                end
              end
            end
          end
          schema.to_xml
        end

        private

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

        def self.get_relaxng_type(type)
          case type
          when Lutaml::Model::Type::String
            "string"
          when Lutaml::Model::Type::Integer
            "integer"
          when Lutaml::Model::Type::Boolean
            "boolean"
          when Lutaml::Model::Type::Float
            "float"
          when Lutaml::Model::Type::Array
            "array"
          when Lutaml::Model::Type::Hash
            "object"
          else
            "string" # Default to string for unknown types
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lutaml-model-0.1.0 lib/lutaml/model/schema/relaxng_schema.rb