Module: Lutaml::Xml::SharedDsl

Included in:
Type::ValueXmlMapping
Defined in:
lib/lutaml/xml/shared_dsl.rb

Overview

Shared DSL methods for XML configuration

This module provides common DSL methods used by both Model classes (via Lutaml::Xml::Mapping) and Type classes (via Lutaml::Model::Type::ValueXmlMapping).

Examples:

Including in a class

class MyMapping
  include Lutaml::Xml::SharedDsl

  def namespace(ns_class)
    validate_namespace_class!(ns_class)
    @namespace_class = ns_class
  end
end

Constant Summary collapse

VALID_XSD_TYPE_SYMBOLS =

Valid XSD type symbols for shorthand notation

%i[
  string
  integer
  float
  double
  decimal
  boolean
  date
  time
  datetime
  duration
  anyuri
  qname
  base64binary
  hexbinary
].freeze

Instance Method Summary collapse

Instance Method Details

#resolve_xsd_type(type) ⇒ String

Resolve XSD type from various input formats

Examples:

Resolving from symbol

resolve_xsd_type(:string)  # => "xs:string"
resolve_xsd_type(:integer) # => "xs:integer"

Resolving from Type class

resolve_xsd_type(Lutaml::Model::Type::String)  # => "xs:string"

Parameters:

  • type (Symbol, Class)

    the type to resolve

Returns:

  • (String)

    the resolved XSD type name



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lutaml/xml/shared_dsl.rb', line 57

def resolve_xsd_type(type)
  # Use ::Symbol and ::Class to refer to Ruby built-in classes
  case type
  when ::Symbol
    resolve_xsd_type_from_symbol(type)
  when ::Class
    resolve_xsd_type_from_class(type)
  else
    raise ArgumentError,
          "xsd_type must be a Symbol or Class, got #{type.class}"
  end
end

#resolve_xsd_type_from_class(klass) ⇒ String

Resolve XSD type from a class

Parameters:

  • klass (Class)

    the type class

Returns:

  • (String)

    the XSD type name

Raises:

  • (ArgumentError)

    if class doesn’t inherit from Type::Value



108
109
110
111
112
113
114
115
116
# File 'lib/lutaml/xml/shared_dsl.rb', line 108

def resolve_xsd_type_from_class(klass)
  unless klass.is_a?(::Class) && klass < Lutaml::Model::Type::Value
    raise ArgumentError,
          "xsd_type class must inherit from Lutaml::Model::Type::Value, " \
          "got #{klass.inspect}"
  end

  klass.default_xsd_type
end

#resolve_xsd_type_from_symbol(sym) ⇒ String

Resolve XSD type from a symbol

Parameters:

  • sym (Symbol)

    the type symbol

Returns:

  • (String)

    the XSD type name

Raises:

  • (ArgumentError)

    if unknown type symbol



93
94
95
96
97
98
99
100
101
# File 'lib/lutaml/xml/shared_dsl.rb', line 93

def resolve_xsd_type_from_symbol(sym)
  unless VALID_XSD_TYPE_SYMBOLS.include?(sym)
    raise ArgumentError,
          "Unknown type symbol: #{sym.inspect}. " \
          "Valid symbols: #{VALID_XSD_TYPE_SYMBOLS.join(', ')}"
  end

  "xs:#{sym}"
end

#validate_namespace_class!(ns_class) ⇒ Object

Validate that a namespace class is valid

Examples:

Validating an XmlNamespace class

validate_namespace_class!(MyNamespace)  # passes if MyNamespace < Lutaml::Xml::Namespace

Validating special symbols

validate_namespace_class!(:blank)   # passes
validate_namespace_class!(:inherit) # passes

Parameters:

  • ns_class (Class, Symbol, nil)

    the namespace class or symbol to validate

Raises:

  • (Lutaml::Model::InvalidNamespaceError)

    if invalid



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lutaml/xml/shared_dsl.rb', line 32

def validate_namespace_class!(ns_class)
  return if ns_class.nil?
  return if %i[blank inherit].include?(ns_class)

  # Use ::Class to refer to Ruby built-in Class (avoiding Lutaml::Model::Type::Class)
  valid = ns_class.is_a?(::Class) && defined?(::Lutaml::Xml::Namespace) && ns_class < ::Lutaml::Xml::Namespace
  unless valid
    raise Lutaml::Xml::Error::InvalidNamespaceError.new(
      expected: "XmlNamespace class, :inherit, or :blank",
      got: ns_class,
    )
  end
end