Class: Lutaml::Model::Schema::RelaxngSchema

Inherits:
Object
  • Object
show all
Extended by:
SharedMethods
Defined in:
lib/lutaml/model/schema/relaxng_schema.rb

Class Method Summary collapse

Methods included from SharedMethods

extract_register_from

Class Method Details

.generate(klass, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lutaml/model/schema/relaxng_schema.rb', line 9

def self.generate(klass, options = {})
  register = extract_register_from(klass)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.grammar(xmlns: "http://relaxng.org/ns/structure/1.0") do
      generate_start(xml, klass)
      generate_define(xml, klass, register)
    end
  end

  options[:pretty] ? builder.to_xml(indent: 2) : builder.to_xml
end

.generate_attributes(xml, klass, register) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lutaml/model/schema/relaxng_schema.rb', line 27

def self.generate_attributes(xml, klass, register)
  klass.attributes.each do |name, attr|
    attr_type = attr.type(register)
    if attr_type <= Lutaml::Model::Serialize
      xml.ref(name: attr_type.name)
    elsif attr.collection?
      xml.zeroOrMore do
        xml.element(name: name) do
          xml.data(type: get_relaxng_type(attr_type))
        end
      end
    else
      xml.element(name: name) do
        xml.data(type: get_relaxng_type(attr_type))
      end
    end
  end
end

.generate_define(xml, klass, register) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lutaml/model/schema/relaxng_schema.rb', line 46

def self.generate_define(xml, klass, register)
  xml.define(name: klass.name) do
    xml.element(name: klass.name) do
      generate_attributes(xml, klass, register)
    end
  end

  klass.attributes.each_value do |attr|
    if attr.type(register) <= Lutaml::Model::Serialize
      generate_define(xml, attr.type(register), register)
    end
  end
end

.generate_start(xml, klass) ⇒ Object



21
22
23
24
25
# File 'lib/lutaml/model/schema/relaxng_schema.rb', line 21

def self.generate_start(xml, klass)
  xml.start do
    xml.ref(name: klass.name)
  end
end

.get_relaxng_type(type) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/lutaml/model/schema/relaxng_schema.rb', line 60

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