Class: Udb::TopLevelDatabaseObject

Inherits:
DatabaseObject show all
Extended by:
T::Sig
Defined in:
lib/udb/obj/database_obj.rb,
lib/udb/exception_code.rb

Overview

base class for any object defined in its own YAML file

expected to contain at least:

$schema:
kind:
name:

Defined Under Namespace

Classes: SchemaError, SchemaValidationError, ValidationError

Instance Method Summary collapse

Constructor Details

#initialize(data, data_path, arch)

Parameters:

  • data (Hash{String => T.untyped})

    Hash with fields to be added

  • data_path (String, Pathname)

    Path to the data file

  • arch (ConfiguredArchitecture)


434
435
436
# File 'lib/udb/obj/database_obj.rb', line 434

def initialize(data, data_path, arch)
  super(data, data_path, arch, DatabaseObject::Kind.deserialize(T.must_because(data["kind"]) { pp data }))
end

Instance Method Details

#create_json_schemer_resolver(udb_resolver) ⇒ T.proc.params(pattern: Regexp).returns(T.untyped)

Parameters:

Returns:

  • (T.proc.params(pattern: Regexp).returns(T.untyped))


375
376
377
378
379
380
381
382
383
# File 'lib/udb/obj/database_obj.rb', line 375

def create_json_schemer_resolver(udb_resolver)
  proc do |pattern|
    if pattern.to_s =~ /^http/
      JSON.parse(Net::HTTP.get(pattern))
    else
      JSON.load_file(udb_resolver.schemas_path / pattern.to_s)
    end
  end
end

#key?(k) ⇒ Boolean

Parameters:

  • k (String)

Returns:

  • (Boolean)


445
# File 'lib/udb/obj/database_obj.rb', line 445

def key?(k) = @data.key?(k)

#keysArray<String>

Returns List of keys added by this DatabaseObject.

Returns:

  • (Array<String>)

    List of keys added by this DatabaseObject



440
# File 'lib/udb/obj/database_obj.rb', line 440

def keys = @data.keys

#primary_defined_byExtensionRequirement

Returns Name of an extension that “primarily” defines the object (i.e., is the first in a list).

Returns:

  • (ExtensionRequirement)

    Name of an extension that “primarily” defines the object (i.e., is the first in a list)



450
451
452
# File 'lib/udb/obj/database_obj.rb', line 450

def primary_defined_by
  defined_by_condition.first_requirement
end

#validate(resolver)

This method returns an undefined value.

validate the data against it’s schema

Parameters:

Raises:



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/udb/obj/database_obj.rb', line 388

def validate(resolver)
  @@schemas[resolver] ||= {}
  schemas = @@schemas[resolver]
  ref_resolver = create_json_schemer_resolver(resolver)

  if @data.key?("$schema")
    schema_path = data["$schema"]
    schema_file, obj_path = schema_path.split("#")
    schema =
      if schemas.key?(schema_file)
        schemas[schema_file]
      else
        schemas[schema_file] = JSONSchemer.schema(
          File.read("#{resolver.schemas_path}/#{schema_file}"),
          regexp_resolver: "ecma",
          ref_resolver:,
          insert_property_defaults: true
        )
        raise SchemaError, T.must(schemas[schema_file]).validate_schema unless T.must(schemas[schema_file]).valid_schema?

        schemas[schema_file]
      end

    unless obj_path.nil?
      obj_path_parts = obj_path.split("/")[1..]

      obj_path_parts.each do |k|
        schema = schema.fetch(k)
      end
    end

    # convert through JSON to handle anything supported in YAML but not JSON
    # (e.g., integer object keys will be converted to strings)
    jsonified_obj = JSON.parse(JSON.generate(@data))

    raise "Nothing there?" if jsonified_obj.nil?

    raise SchemaValidationError.new(@data_path, schema.validate(jsonified_obj)) unless schema.valid?(jsonified_obj)
  else
    warn "No $schema for #{@data_path}"
  end
end