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,
  "Hash" => :hash,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(namespace: nil) ⇒ ModelCompiler

Returns a new instance of ModelCompiler.



21
22
23
24
25
26
# File 'lib/lutaml/lml/model_compiler.rb', line 21

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

Instance Method Details

#compile(input) ⇒ Object



28
29
30
31
32
# File 'lib/lutaml/lml/model_compiler.rb', line 28

def compile(input)
  doc = Pipeline.call(input, resolve: false)
  compile_document(doc)
  @compiled
end

#compiled_classesObject



42
43
44
# File 'lib/lutaml/lml/model_compiler.rb', line 42

def compiled_classes
  @compiled
end

#hydrate(input) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/lutaml/lml/model_compiler.rb', line 34

def hydrate(input)
  doc = input.is_a?(Document) ? input : Pipeline.call(input, resolve: false)
  compile_document(doc) unless @compiled.any?
  return {} unless doc.instance

  hydrate_instance(doc.instance)
end

#validate(input_or_instance, compiled: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lutaml/lml/model_compiler.rb', line 46

def validate(input_or_instance, compiled: nil)
  if compiled
    @compiled = compiled
  elsif input_or_instance.is_a?(String) || input_or_instance.is_a?(IO) || input_or_instance.is_a?(StringIO)
    doc = Pipeline.call(input_or_instance, resolve: false)
    compile_document(doc)
  end
  errors = []
  instance = input_or_instance.is_a?(Lutaml::Lml::Instance) ? input_or_instance : nil
  unless instance
    doc ||= Pipeline.call(input_or_instance, resolve: false)
    instance = doc.instance
  end
  validate_instance(instance, errors) if instance
  errors
end