Class: Cocina::JsonSchemaWrapper::SchemaWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina/json_schema_wrapper.rb

Overview

Wrapper for individual schemas

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_def, name, parent) ⇒ SchemaWrapper

Returns a new instance of SchemaWrapper.



37
38
39
40
41
# File 'lib/cocina/json_schema_wrapper.rb', line 37

def initialize(schema_def, name, parent)
  @schema_def = schema_def
  @name = name
  @parent = parent
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/cocina/json_schema_wrapper.rb', line 35

def name
  @name
end

Instance Method Details

#all_ofObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cocina/json_schema_wrapper.rb', line 60

def all_of
  return [] unless @schema_def['allOf']

  @schema_def['allOf'].map do |schema|
    if schema['$ref']
      ref_name = schema['$ref'].split('/').last
      SchemaWrapper.new(@parent.spec.dig('$defs', ref_name), ref_name, @parent)
    else
      SchemaWrapper.new(schema, nil, @parent)
    end
  end
end

#defaultObject



112
113
114
# File 'lib/cocina/json_schema_wrapper.rb', line 112

def default
  @schema_def['default']
end

#deprecatedObject



96
97
98
# File 'lib/cocina/json_schema_wrapper.rb', line 96

def deprecated
  @schema_def['deprecated']
end

#descriptionObject



100
101
102
# File 'lib/cocina/json_schema_wrapper.rb', line 100

def description
  @schema_def['description']
end

#enumObject



108
109
110
# File 'lib/cocina/json_schema_wrapper.rb', line 108

def enum
  @schema_def['enum']
end

#exampleObject



104
105
106
# File 'lib/cocina/json_schema_wrapper.rb', line 104

def example
  @schema_def['example']
end

#formatObject



116
117
118
# File 'lib/cocina/json_schema_wrapper.rb', line 116

def format
  @schema_def['format']
end

#itemsObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/cocina/json_schema_wrapper.rb', line 120

def items
  return nil unless @schema_def['items']

  if @schema_def['items']['$ref']
    ref_name = @schema_def['items']['$ref'].split('/').last
    SchemaWrapper.new(@parent.spec.dig('$defs', ref_name), ref_name, @parent)
  else
    SchemaWrapper.new(@schema_def['items'], Generator::SchemaArray::GENERIC_ITEMS_NAME, @parent)
  end
end

#nullableObject



88
89
90
# File 'lib/cocina/json_schema_wrapper.rb', line 88

def nullable
  @schema_def['nullable']
end

#one_ofObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cocina/json_schema_wrapper.rb', line 47

def one_of
  return [] unless @schema_def['oneOf']

  @schema_def['oneOf'].map do |schema|
    if schema['$ref']
      ref_name = schema['$ref'].split('/').last
      SchemaWrapper.new(@parent.spec.dig('$defs', ref_name), ref_name, @parent)
    else
      SchemaWrapper.new(schema, nil, @parent)
    end
  end
end

#patternObject



92
93
94
# File 'lib/cocina/json_schema_wrapper.rb', line 92

def pattern
  @schema_def['pattern']
end

#propertiesObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/cocina/json_schema_wrapper.rb', line 73

def properties
  @schema_def['properties']&.transform_values do |schema|
    if schema['$ref']
      ref_name = schema['$ref'].split('/').last
      SchemaWrapper.new(@parent.spec.dig('$defs', ref_name), ref_name, @parent)
    else
      SchemaWrapper.new(schema, nil, @parent)
    end
  end
end

#requiredObject



84
85
86
# File 'lib/cocina/json_schema_wrapper.rb', line 84

def required
  @schema_def['required']
end

#typeObject



43
44
45
# File 'lib/cocina/json_schema_wrapper.rb', line 43

def type
  @schema_def['type']
end