Class: Lutaml::Lml::ModelCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/lml/model_compiler.rb

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

TYPE_MAP =
{
  "String" => :string,
  "Integer" => :integer,
  "Boolean" => :boolean,
  "Float" => :float,
  "Date" => :date,
  "date_time" => :date_time,
  "DateTime" => :date_time,
  "Time" => :time,
  "Uri" => :string,
}.freeze
UNBOUNDED_TOKENS =

Tokens that denote unbounded cardinality in LML attribute declarations. Anything else must parse as an Integer.

%w[* n N unbounded].freeze

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil) ⇒ ModelCompiler

Returns a new instance of ModelCompiler.



24
25
26
27
28
29
# File 'lib/lutaml/lml/model_compiler.rb', line 24

def initialize(namespace: nil)
  @namespace = namespace
  @compiled = {}
  @forward_refs = {}
  @enum_names = Set.new
end

Instance Method Details

#compile(input) ⇒ Object



31
32
33
34
35
# File 'lib/lutaml/lml/model_compiler.rb', line 31

def compile(input)
  doc = Lutaml::Lml.parse_document(input)
  compile_document(doc)
  @compiled
end

#compiled_classesObject



45
46
47
# File 'lib/lutaml/lml/model_compiler.rb', line 45

def compiled_classes
  @compiled
end

#hydrate(input) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/lutaml/lml/model_compiler.rb', line 37

def hydrate(input)
  doc = input.is_a?(Document) ? input : Lutaml::Lml.parse_document(input)
  compile_document(doc) unless @compiled.any?
  return {} unless doc.instance

  hydrate_instance(doc.instance)
end

#validate(input, compiled: nil) ⇒ Object

Validate an instances document against compiled models.

Accepts the instances input as a String, IO, StringIO, or a pre-parsed Document. If compiled: is supplied, that registry is used; otherwise models are compiled from the same input (which must therefore contain a models section).

Returns an array of validation error strings (empty if all pass).



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

def validate(input, compiled: nil)
  doc = input.is_a?(Document) ? input : Lutaml::Lml.parse_document(input)
  if compiled
    @compiled = compiled
  else
    compile_document(doc)
  end

  errors = []
  validate_instance(doc.instance, errors) if doc.instance
  errors
end