Class: Rng::SchemaPreamble

Inherits:
Object
  • Object
show all
Defined in:
lib/rng/schema_preamble.rb

Overview

Container for namespace and datatype declarations in RNC schema

Provides structured access to schema metadata that appears at the top of RNC files before the grammar content.

Examples:

Building a preamble

preamble = SchemaPreamble.new
preamble.add_namespace(
  NamespaceDeclaration.new(uri: "http://example.com", is_default: true)
)
preamble.add_datatype(
  DatatypeDeclaration.new(prefix: "xsd", uri: "http://www.w3.org/2001/XMLSchema-datatypes")
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaPreamble

Initialize an empty preamble



22
23
24
25
# File 'lib/rng/schema_preamble.rb', line 22

def initialize
  @namespaces = []
  @datatypes = []
end

Instance Attribute Details

#datatypesObject (readonly)

Returns the value of attribute datatypes.



19
20
21
# File 'lib/rng/schema_preamble.rb', line 19

def datatypes
  @datatypes
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



19
20
21
# File 'lib/rng/schema_preamble.rb', line 19

def namespaces
  @namespaces
end

Instance Method Details

#add_datatype(declaration) ⇒ Object

Add a datatype declaration

Parameters:



37
38
39
# File 'lib/rng/schema_preamble.rb', line 37

def add_datatype(declaration)
  @datatypes << declaration
end

#add_namespace(declaration) ⇒ Object

Add a namespace declaration

Parameters:



30
31
32
# File 'lib/rng/schema_preamble.rb', line 30

def add_namespace(declaration)
  @namespaces << declaration
end

#datatype_mapHash<String, String>

Get a map of datatype prefixes to URIs

Returns:

  • (Hash<String, String>)

    Map of prefix => URI



60
61
62
63
64
# File 'lib/rng/schema_preamble.rb', line 60

def datatype_map
  @datatypes.to_h do |dt|
    [dt.prefix, dt.uri]
  end
end

#default_namespaceString?

Get the default namespace URI

Returns:

  • (String, nil)

    Default namespace URI or nil if none



44
45
46
# File 'lib/rng/schema_preamble.rb', line 44

def default_namespace
  @namespaces.find(&:default?)&.uri
end

#empty?Boolean

Check if preamble is empty

Returns:

  • (Boolean)


69
70
71
# File 'lib/rng/schema_preamble.rb', line 69

def empty?
  @namespaces.empty? && @datatypes.empty?
end

#namespace_mapHash<String, String>

Get a map of namespace prefixes to URIs

Returns:

  • (Hash<String, String>)

    Map of prefix => URI



51
52
53
54
55
# File 'lib/rng/schema_preamble.rb', line 51

def namespace_map
  @namespaces.select(&:prefixed?).to_h do |ns|
    [ns.prefix, ns.uri]
  end
end