Module: Lutaml::Model::Schema::XmlCompiler
- Extended by:
- XmlCompiler
- Included in:
- XmlCompiler
- Defined in:
- lib/lutaml/model/schema/xml_compiler.rb,
lib/lutaml/model/schema/xml_compiler/group.rb,
lib/lutaml/model/schema/xml_compiler/choice.rb,
lib/lutaml/model/schema/xml_compiler/element.rb,
lib/lutaml/model/schema/xml_compiler/sequence.rb,
lib/lutaml/model/schema/xml_compiler/attribute.rb,
lib/lutaml/model/schema/xml_compiler/restriction.rb,
lib/lutaml/model/schema/xml_compiler/simple_type.rb,
lib/lutaml/model/schema/xml_compiler/complex_type.rb,
lib/lutaml/model/schema/xml_compiler/simple_content.rb,
lib/lutaml/model/schema/xml_compiler/attribute_group.rb,
lib/lutaml/model/schema/xml_compiler/complex_content.rb,
lib/lutaml/model/schema/xml_compiler/registry_generator.rb,
lib/lutaml/model/schema/xml_compiler/xml_namespace_class.rb,
lib/lutaml/model/schema/xml_compiler/complex_content_restriction.rb
Defined Under Namespace
Classes: Attribute, AttributeGroup, Choice, ComplexContent, ComplexContentRestriction, ComplexType, Element, Group, RegistryGenerator, Restriction, Sequence, SimpleContent, SimpleType, XmlNamespaceClass
Constant Summary collapse
- ELEMENT_ORDER_IGNORABLE =
%w[import include].freeze
- XML_ADAPTER_NOT_SET_MESSAGE =
<<~MSG Nokogiri is not set as XML Adapter. Make sure Nokogiri is installed and set as XML Adapter eg. execute: gem install nokogiri require 'lutaml/xml' Lutaml::Model::Config.xml_adapter = :nokogiri MSG
- XML_DEFINED_ATTRIBUTES =
NOTE: These must be full class names (strings), not symbols like :xml_id. The type resolver looks up these strings directly in the type registry, and symbols would be interpreted as literal type names rather than W3C types.
{ "id" => "Lutaml::Xml::W3c::XmlIdType", "lang" => "Lutaml::Xml::W3c::XmlLangType", "space" => "Lutaml::Xml::W3c::XmlSpaceType", "base" => "Lutaml::Xml::W3c::XmlBaseType", }.freeze
- XML_NAMESPACE_URI =
"http://www.w3.org/XML/1998/namespace"
Instance Attribute Summary collapse
-
#attribute_groups ⇒ Object
readonly
Returns the value of attribute attribute_groups.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#complex_types ⇒ Object
readonly
Returns the value of attribute complex_types.
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#group_types ⇒ Object
readonly
Returns the value of attribute group_types.
-
#namespace_classes ⇒ Object
readonly
Returns the value of attribute namespace_classes.
-
#simple_types ⇒ Object
readonly
Returns the value of attribute simple_types.
Instance Method Summary collapse
- #as_models(schema, options: {}) ⇒ Object
- #collect_namespaces(schemas, options) ⇒ Object
- #create_file(name, content, dir) ⇒ Object
- #populate_default_values ⇒ Object
- #require_classes(classes_hash) ⇒ Object
- #resolved_element_order(object) ⇒ Object
- #restriction_content(instance, restriction) ⇒ Object
- #restriction_length(lengths) ⇒ Object
-
#restriction_min_max(restriction, instance, field:, value_method: :min) ⇒ Object
Use min/max to get the value from the field_value array.
- #restriction_patterns(patterns, instance) ⇒ Object
- #schema_to_models(schemas) ⇒ Object
- #setup_attribute(attribute) ⇒ Object
- #setup_attribute_groups(attribute_group) ⇒ Object
- #setup_attribute_type(attribute) ⇒ Object
- #setup_choice(choice) ⇒ Object
- #setup_complex_content(complex_content, name, compiler_complex_type) ⇒ Object
- #setup_complex_content_restriction(restriction, compiler_complex_type) ⇒ Object
- #setup_complex_type(complex_type) ⇒ Object
- #setup_element(element) ⇒ Object
-
#setup_element_type(element, _instance) ⇒ Object
Populates @simple_types or @complex_types based on elements available value.
- #setup_extension(extension, instance) ⇒ Object
- #setup_group_type(group, root_call: false) ⇒ Object
- #setup_group_type_instance(group) ⇒ Object
- #setup_restriction(restriction) ⇒ Object
- #setup_sequence(sequence) ⇒ Object
- #setup_simple_content(simple_content) ⇒ Object
- #setup_simple_type(simple_type) ⇒ Object
- #to_models(schema, options = {}) ⇒ Object
- #xml_defined_attribute?(schema, item_name) ⇒ Boolean
Instance Attribute Details
#attribute_groups ⇒ Object (readonly)
Returns the value of attribute attribute_groups.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def attribute_groups @attribute_groups end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def attributes @attributes end |
#complex_types ⇒ Object (readonly)
Returns the value of attribute complex_types.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def complex_types @complex_types end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def elements @elements end |
#group_types ⇒ Object (readonly)
Returns the value of attribute group_types.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def group_types @group_types end |
#namespace_classes ⇒ Object (readonly)
Returns the value of attribute namespace_classes.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def namespace_classes @namespace_classes end |
#simple_types ⇒ Object (readonly)
Returns the value of attribute simple_types.
33 34 35 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 33 def simple_types @simple_types end |
Instance Method Details
#as_models(schema, options: {}) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 173 def as_models(schema, options: {}) unless Config.xml_adapter.name.end_with?("NokogiriAdapter") raise Error, XML_ADAPTER_NOT_SET_MESSAGE end parsed_schema = Lutaml::Xml::Schema::Xsd.parse(schema, location: [:location]) @elements = MappingHash.new @attributes = MappingHash.new @group_types = MappingHash.new @simple_types = MappingHash.new @complex_types = MappingHash.new @attribute_groups = MappingHash.new @namespace_classes = MappingHash.new populate_default_values collect_namespaces(Array(parsed_schema), ) schema_to_models(Array(parsed_schema)) end |
#collect_namespaces(schemas, options) ⇒ Object
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 549 def collect_namespaces(schemas, ) # Collect unique namespace URIs from the schemas namespace_uris = Set.new # Add the main namespace from options if provided if [:namespace] namespace_uris.add([:namespace]) end # Extract namespaces from schema elements schemas.each do |schema| namespace_uris.add(schema.target_namespace) if schema.target_namespace end # Create XmlNamespaceClass for each unique namespace namespace_uris.each do |uri| next if uri.nil? || uri.empty? # Use provided prefix if available prefix = [:prefix] if [:namespace] == uri ns_class = XmlNamespaceClass.new(uri: uri, prefix: prefix) @namespace_classes[ns_class.class_name] = ns_class end end |
#create_file(name, content, dir) ⇒ Object
139 140 141 142 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 139 def create_file(name, content, dir) name = name.split(":").last File.write("#{dir}/#{Utils.snake_case(name)}.rb", content) end |
#populate_default_values ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 195 def populate_default_values XML_DEFINED_ATTRIBUTES.each do |name, value| @attributes[name] = Attribute.new(name: name) @attributes[name].type = value end # W3C XmlNamespace is now auto-loaded via lutaml/model.rb # Reference: Lutaml::Xml::W3c::XmlNamespace end |
#require_classes(classes_hash) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 144 def require_classes(classes_hash) Dir.mktmpdir do |dir| # Create subdirectory for class files (matches autoload path in registry) module_subdir = "generatedmodels" full_dir = File.join(dir, module_subdir) FileUtils.mkdir_p(full_dir) # Generate registry file first with autoload registry_content = RegistryGenerator.generate(classes_hash, module_namespace: "GeneratedModels", register_id: :default) if registry_content registry_file = File.join(dir, "registry.rb") File.write(registry_file, registry_content) end # Write class files to subdirectory classes_hash.each do |name, klass| create_file(name, klass, full_dir) end # Require the registry first to set up autoloads require "#{dir}/registry" # Call register_all to register all classes GeneratedModels.register_all if defined?(GeneratedModels) end end |
#resolved_element_order(object) ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 524 def resolved_element_order(object) return [] if object.element_order.nil? # If the object is an XSD type (which has its own resolved_element_order), # use it instead of processing element_order which returns generic XML elements if object.is_a?(Lutaml::Xml::Schema::Xsd::Base) return object.resolved_element_order end object.element_order.each_with_object(object.element_order.dup) do |builder_instance, array| next array.delete(builder_instance) if builder_instance.text? || ELEMENT_ORDER_IGNORABLE.include?(builder_instance.name) index = 0 array.each_with_index do |element, i| next unless element == builder_instance array[i] = Array(object.public_send(Utils.snake_case(builder_instance.name)))[index] index += 1 end end object.element_order end |
#restriction_content(instance, restriction) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 253 def restriction_content(instance, restriction) restriction_min_max(restriction, instance, field: :max_length, value_method: :min) restriction_min_max(restriction, instance, field: :min_length, value_method: :max) restriction_min_max(restriction, instance, field: :min_inclusive, value_method: :max) restriction_min_max(restriction, instance, field: :max_inclusive, value_method: :min) restriction_min_max(restriction, instance, field: :max_exclusive, value_method: :max) restriction_min_max(restriction, instance, field: :min_exclusive, value_method: :min) instance.length = restriction_length(restriction.length) if restriction.length&.any? end |
#restriction_length(lengths) ⇒ Object
281 282 283 284 285 286 287 288 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 281 def restriction_length(lengths) lengths.map do |length| MappingHash.new.tap do |hash| hash[:value] = length.value hash[:fixed] = length.fixed if length.fixed end end end |
#restriction_min_max(restriction, instance, field:, value_method: :min) ⇒ Object
Use min/max to get the value from the field_value array.
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 270 def restriction_min_max(restriction, instance, field:, value_method: :min) field_value = restriction.public_send(field) return unless field_value&.any? instance.public_send( :"#{field}=", field_value.map(&:value).public_send(value_method).to_s, ) end |
#restriction_patterns(patterns, instance) ⇒ Object
488 489 490 491 492 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 488 def restriction_patterns(patterns, instance) return if Utils.blank?(patterns) instance.pattern = patterns.map { |p| "(#{p.value})" }.join("|") end |
#schema_to_models(schemas) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 205 def schema_to_models(schemas) return if schemas.empty? schemas.each do |schema| schema_to_models(schema.include) if schema.include&.any? schema_to_models(schema.import) if schema.import&.any? # Use schema's resolved_element_order which returns properly typed XSD objects schema.resolved_element_order.each do |order_item| item_name = order_item.name if order_item.is_a?(Lutaml::Xml::Schema::Xsd::SimpleType) || order_item.is_a?(Lutaml::Xml::Schema::Xsd::Group) || order_item.is_a?(Lutaml::Xml::Schema::Xsd::ComplexType) || order_item.is_a?(Lutaml::Xml::Schema::Xsd::Element) || order_item.is_a?(Lutaml::Xml::Schema::Xsd::Attribute) || order_item.is_a?(Lutaml::Xml::Schema::Xsd::AttributeGroup) case order_item when Lutaml::Xml::Schema::Xsd::SimpleType @simple_types[item_name] = setup_simple_type(order_item) when Lutaml::Xml::Schema::Xsd::Group @group_types[item_name] = setup_group_type(order_item, root_call: true) when Lutaml::Xml::Schema::Xsd::ComplexType @complex_types[item_name] = setup_complex_type(order_item) when Lutaml::Xml::Schema::Xsd::Element @elements[item_name] = setup_element(order_item) when Lutaml::Xml::Schema::Xsd::Attribute next if xml_defined_attribute?(schema, item_name) @attributes[item_name] = setup_attribute(order_item) when Lutaml::Xml::Schema::Xsd::AttributeGroup @attribute_groups[item_name] = setup_attribute_groups(order_item) end end end nil end |
#setup_attribute(attribute) ⇒ Object
383 384 385 386 387 388 389 390 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 383 def setup_attribute(attribute) instance = Attribute.new(name: attribute.name, ref: attribute.ref) if attribute.name instance.type = setup_attribute_type(attribute) instance.default = attribute.default end instance end |
#setup_attribute_groups(attribute_group) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 402 def setup_attribute_groups(attribute_group) instance = AttributeGroup.new(name: attribute_group.name, ref: attribute_group.ref) if attribute_group.name resolved_element_order(attribute_group).each do |object| group_attribute = case object when Lutaml::Xml::Schema::Xsd::Attribute setup_attribute(object) when Lutaml::Xml::Schema::Xsd::AttributeGroup setup_attribute_groups(object) end instance << group_attribute if group_attribute end end instance end |
#setup_attribute_type(attribute) ⇒ Object
392 393 394 395 396 397 398 399 400 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 392 def setup_attribute_type(attribute) return attribute.type if attribute.type simple_type = attribute.simple_type attr_name = "ST_#{attribute.name}" simple_type.name = attr_name @simple_types[attr_name] = setup_simple_type(simple_type) attr_name end |
#setup_choice(choice) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 364 def setup_choice(choice) Choice.new.tap do |instance| instance.min_occurs = choice.min_occurs instance.max_occurs = choice.max_occurs resolved_element_order(choice).each do |element| instance << case element when Lutaml::Xml::Schema::Xsd::Element setup_element(element) when Lutaml::Xml::Schema::Xsd::Sequence setup_sequence(element) when Lutaml::Xml::Schema::Xsd::Group setup_group_type(element) when Lutaml::Xml::Schema::Xsd::Choice setup_choice(element) end end end end |
#setup_complex_content(complex_content, name, compiler_complex_type) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 494 def setup_complex_content(complex_content, name, compiler_complex_type) @complex_types[name] = ComplexContent.new.tap do |instance| compiler_complex_type.mixed = complex_content.mixed if extension = complex_content.extension setup_extension(extension, compiler_complex_type) elsif restriction = complex_content.restriction instance.restriction = setup_complex_content_restriction(restriction, compiler_complex_type) end end end |
#setup_complex_content_restriction(restriction, compiler_complex_type) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 462 def setup_complex_content_restriction(restriction, compiler_complex_type) ComplexContentRestriction.new.tap do |instance| compiler_complex_type.base_class = restriction.base resolved_element_order(restriction).each do |element| # For restrictions, only add attributes and attribute groups. # Sequence/choice/group elements are inherited from the base class # and adding them here causes duplicate mappings. next if element.is_a?(Lutaml::Xml::Schema::Xsd::Sequence) || element.is_a?(Lutaml::Xml::Schema::Xsd::Choice) || element.is_a?(Lutaml::Xml::Schema::Xsd::Group) instance << case element when Lutaml::Xml::Schema::Xsd::Attribute setup_attribute(element) when Lutaml::Xml::Schema::Xsd::AttributeGroup setup_attribute_groups(element) when Lutaml::Xml::Schema::Xsd::Sequence setup_sequence(element) when Lutaml::Xml::Schema::Xsd::Choice setup_choice(element) when Lutaml::Xml::Schema::Xsd::Group setup_group_type(element) end end end end |
#setup_complex_type(complex_type) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 290 def setup_complex_type(complex_type) ComplexType.new.tap do |instance| instance.id = complex_type.id instance.name = complex_type.name instance.mixed = complex_type.mixed resolved_element_order(complex_type).each do |element| case element when Lutaml::Xml::Schema::Xsd::Attribute instance << setup_attribute(element) when Lutaml::Xml::Schema::Xsd::Sequence instance << setup_sequence(element) when Lutaml::Xml::Schema::Xsd::Choice instance << setup_choice(element) when Lutaml::Xml::Schema::Xsd::ComplexContent instance << setup_complex_content(element, instance.name, instance) when Lutaml::Xml::Schema::Xsd::AttributeGroup instance << setup_attribute_groups(element) when Lutaml::Xml::Schema::Xsd::Group instance << setup_group_type(element) when Lutaml::Xml::Schema::Xsd::SimpleContent instance.simple_content = setup_simple_content(element) end end end end |
#setup_element(element) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 419 def setup_element(element) element_name = element.name instance = Element.new(name: element_name, ref: element.ref) instance.min_occurs = element.min_occurs instance.max_occurs = element.max_occurs if element_name instance.type = setup_element_type(element, instance) instance.id = element.id instance.fixed = element.fixed instance.default = element.default end instance end |
#setup_element_type(element, _instance) ⇒ Object
Populates @simple_types or @complex_types based on elements available value.
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 434 def setup_element_type(element, _instance) return element.type if element.type type, prefix = if element.simple_type ["simple", "ST"] else ["complex", "CT"] end type_instance = element.public_send(:"#{type}_type") type_instance.name = [prefix, element.name].join("_") instance_variable_get(:"@#{type}_types")[type_instance.name] = public_send(:"setup_#{type}_type", type_instance) type_instance.name end |
#setup_extension(extension, instance) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 506 def setup_extension(extension, instance) instance.base_class = extension.base resolved_element_order(extension).each do |element| instance << case element when Lutaml::Xml::Schema::Xsd::Attribute setup_attribute(element) when Lutaml::Xml::Schema::Xsd::AttributeGroup setup_attribute_groups(element) when Lutaml::Xml::Schema::Xsd::Sequence setup_sequence(element) when Lutaml::Xml::Schema::Xsd::Choice setup_choice(element) when Lutaml::Xml::Schema::Xsd::Group setup_group_type(element) end end end |
#setup_group_type(group, root_call: false) ⇒ Object
349 350 351 352 353 354 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 349 def setup_group_type(group, root_call: false) object = Group.new(group.name, group.ref) object.instance = setup_group_type_instance(group) @group_types[group.name] = object if group.name && !root_call object end |
#setup_group_type_instance(group) ⇒ Object
356 357 358 359 360 361 362 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 356 def setup_group_type_instance(group) if sequence = group.sequence setup_sequence(sequence) elsif choice = group.choice setup_choice(choice) end end |
#setup_restriction(restriction) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 450 def setup_restriction(restriction) Restriction.new.tap do |instance| instance.base_class = restriction.base restriction_patterns(restriction.pattern, instance) restriction_content(instance, restriction) if restriction.enumeration&.any? instance.enumerations = restriction.enumeration.map(&:value) end end end |
#setup_sequence(sequence) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 329 def setup_sequence(sequence) Sequence.new.tap do |instance| resolved_element_order(sequence).each do |object| # No implementation yet for Lutaml::Xml::Schema::Xsd::Any! next if object.is_a?(Lutaml::Xml::Schema::Xsd::Any) instance << case object when Lutaml::Xml::Schema::Xsd::Sequence setup_sequence(object) when Lutaml::Xml::Schema::Xsd::Element setup_element(object) when Lutaml::Xml::Schema::Xsd::Choice setup_choice(object) when Lutaml::Xml::Schema::Xsd::Group setup_group_type(object) end end end end |
#setup_simple_content(simple_content) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 317 def setup_simple_content(simple_content) SimpleContent.new.tap do |instance| if simple_content.extension instance.base_class = simple_content.extension.base setup_extension(simple_content.extension, instance) elsif simple_content.restriction instance.base_class = simple_content.restriction.base instance << setup_restriction(simple_content.restriction) end end end |
#setup_simple_type(simple_type) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 242 def setup_simple_type(simple_type) SimpleType.new(simple_type.name).tap do |type_object| if union = simple_type.union type_object.unions = union.member_types.split elsif restriction = simple_type.restriction type_object.base_class = restriction.base&.split(":")&.last type_object.instance = setup_restriction(restriction) end end end |
#to_models(schema, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 62 def to_models(schema, = {}) as_models(schema, options: ) [:indent] = [:indent] ? [:indent].to_i : 2 # Auto-generate module namespace from output directory if not explicitly set unless .key?(:module_namespace) output_dir = .fetch(:output_dir, "lutaml_models_#{Time.now.to_i}") # Generate a namespace from the directory name (e.g., "my_models" -> "MyModels") dir_name = File.basename(output_dir).split("_").map(&:capitalize).join [:module_namespace] = dir_name end # Allow explicit nil to disable namespace # only set default register_id if module_namespace is present if [:module_namespace] [:register_id] ||= :default end @simple_types.merge!(XmlCompiler::SimpleType.setup_supported_types) # Generate namespace classes namespace_classes_hash = {} @namespace_classes.each do |name, ns_class| namespace_classes_hash[name] = ns_class.to_class(options: ) end classes_list = namespace_classes_hash.merge(@simple_types).merge(@complex_types).merge(@group_types) classes_list = classes_list.transform_values do |type| # Skip namespace classes (already strings) and only process model types next type if type.is_a?(String) type.to_class(options: .merge( module_namespace: [:module_namespace], register_id: [:register_id], )) end if [:create_files] dir = .fetch(:output_dir, "lutaml_models_#{Time.now.to_i}") # If module_namespace provided, create subdirectories if [:module_namespace] module_path = [:module_namespace].split("::").map(&:downcase).join("/") full_dir = File.join(dir, module_path) FileUtils.mkdir_p(full_dir) # Generate central registry file with autoload registry_content = RegistryGenerator.generate(classes_list, ) if registry_content # Registry file goes in parent directory of module path registry_name = module_path.split("/").last registry_file = File.join(dir, "#{registry_name}_registry.rb") File.write(registry_file, registry_content) end # Write class files classes_list.each do |name, klass| create_file(name, klass, full_dir) end else # When no module_namespace, write files directly to output dir # Generated files use require_relative for dependencies (traditional approach) FileUtils.mkdir_p(dir) # Write class files classes_list.each do |name, klass| create_file(name, klass, dir) end end true else require_classes(classes_list) if [:load_classes] classes_list end end |
#xml_defined_attribute?(schema, item_name) ⇒ Boolean
575 576 577 578 |
# File 'lib/lutaml/model/schema/xml_compiler.rb', line 575 def xml_defined_attribute?(schema, item_name) schema.target_namespace == XML_NAMESPACE_URI && XML_DEFINED_ATTRIBUTES.key?(item_name) end |