Module: FHIR::CommonStructureDefinition

Extended by:
Deprecate
Included in:
R4::StructureDefinition, R4B::StructureDefinition, R5::StructureDefinition
Defined in:
lib/fhir_models/fhir_ext/common_structure_definition.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deprecate

deprecate

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



11
12
13
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 11

def errors
  @errors
end

#findingObject

Returns the value of attribute finding.



10
11
12
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 10

def finding
  @finding
end

#hierarchyObject

Returns the value of attribute hierarchy.



13
14
15
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 13

def hierarchy
  @hierarchy
end

#warningsObject

Returns the value of attribute warnings.



12
13
14
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 12

def warnings
  @warnings
end

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 15

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#data_type?(data_type_code, value) ⇒ Boolean

data_type_code == a FHIR DataType code (see hl7.org/fhir/2015May/datatypes.html) value == the representation of the value

Returns:

  • (Boolean)


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 324

def data_type?(data_type_code, value)
  # FHIR models covers any base Resources
  if versioned_fhir_module::RESOURCES.include?(data_type_code)
    definition = versioned_fhir_module::Definitions.resource_definition(data_type_code)
    unless definition.nil?
      ret_val = false
      begin
        # klass = Module.const_get("FHIR::#{data_type_code}")
        # ret_val = definition.validates_resource?(klass.new(deep_copy(value)))
        ret_val = definition.validates_hash?(value)
        unless ret_val
          @errors += definition.errors
          @warnings += definition.warnings
        end
      rescue StandardError
        @errors << "Unable to verify #{data_type_code} as a FHIR Resource."
      end
      return ret_val
    end
  end

  # Remaining data types: handle special cases before checking type StructureDefinitions
  case data_type_code.downcase
  when 'domainresource'
    true # we don't have to verify domain resource, because it will be included in the snapshot
  when 'resource'
    resource_type = value['resourceType']
    definition = versioned_fhir_module::Definitions.resource_definition(resource_type)
    if !definition.nil?
      ret_val = false
      begin
        # klass = Module.const_get("FHIR::#{resource_type}")
        # ret_val = definition.validates_resource?(klass.new(deep_copy(value)))
        ret_val = definition.validates_hash?(value)
        unless ret_val
          @errors += definition.errors
          @warnings += definition.warnings
        end
      rescue StandardError
        @errors << "Unable to verify #{resource_type} as a FHIR Resource."
      end
      ret_val
    else
      @errors << "Unable to find base Resource definition: #{resource_type}"
      false
    end
  when *versioned_fhir_module::PRIMITIVES.keys.map(&:downcase)
    FHIR.primitive?(datatype: data_type_code, value: value)
  else
    # Eliminate endless loop on Element is an Element
    return true if data_type_code == 'Element' && id == 'Element'

    definition = versioned_fhir_module::Definitions.type_definition(data_type_code)
    definition = versioned_fhir_module::Definitions.resource_definition(data_type_code) if definition.nil?
    if !definition.nil?
      ret_val = false
      begin
        # klass = Module.const_get("FHIR::#{data_type_code}")
        # ret_val = definition.validates_resource?(klass.new(deep_copy(value)))
        ret_val = definition.validates_hash?(value)
        unless ret_val
          @errors += definition.errors
          @warnings += definition.warnings
        end
      rescue StandardError
        @errors << "Unable to verify #{data_type_code} as a FHIR type."
      end
      ret_val
    else
      @errors << "Unable to find base type definition: #{data_type_code}"
      false
    end
  end
end

#describe_element(element) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 100

def describe_element(element)
  if element.path.end_with?('.extension', '.modifierExtension') && element.sliceName
    "#{element.path} (#{element.sliceName})"
  else
    element.path
  end
end

#some_type_of_xml_or_json?(code) ⇒ Boolean

Returns:

  • (Boolean)


448
449
450
451
452
453
454
455
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 448

def some_type_of_xml_or_json?(code)
  m = code.downcase
  return true if ['xml', 'json'].include?(m)
  return true if m.start_with?('application/', 'text/') && m.end_with?('json', 'xml')
  return true if m.start_with?('application/xml', 'text/xml', 'application/json', 'text/json')

  false
end

#validate_resource(resource) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 45

def validate_resource(resource)
  @errors = []
  @warnings = []
  if resource.is_a?(FHIR::Model)
    valid_json?(resource.to_json) if resource
  else
    @errors << "#{resource.class} is not a resource."
  end
  @errors
end

#validates_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 56

def validates_hash?(hash)
  @errors = []
  @warnings = []
  valid_json?(hash) if hash
  @errors
end

#validates_resource?(resource) ⇒ Boolean


Profile Validation

Returns:

  • (Boolean)


41
42
43
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 41

def validates_resource?(resource)
  validate_resource(resource).empty?
end

#verify_cardinality(element, nodes) ⇒ Object



311
312
313
314
315
316
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 311

def verify_cardinality(element, nodes)
  # Check the cardinality
  min = element.min
  max = element.max == '*' ? Float::INFINITY : element.max.to_i
  @errors << "#{describe_element(element)} failed cardinality test (#{min}..#{max}) -- found #{nodes.size}" if (nodes.size < min) || (nodes.size > max)
end

#verify_fixed_value(element, value) ⇒ Object



318
319
320
# File 'lib/fhir_models/fhir_ext/common_structure_definition.rb', line 318

def verify_fixed_value(element, value)
  @errors << "#{describe_element(element)} value of '#{value}' did not match fixed value: #{element.fixed}" if !element.fixed.nil? && element.fixed != value
end