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.



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

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.



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

def name
  @name
end

Instance Method Details

#all_ofObject



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

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



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

def default
  @schema_def['default']
end

#deprecatedObject



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

def deprecated
  @schema_def['deprecated']
end

#descriptionObject



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

def description
  @schema_def['description']
end

#enumObject



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

def enum
  @schema_def['enum']
end

#exampleObject



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

def example
  @schema_def['example']
end

#formatObject



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

def format
  @schema_def['format']
end

#itemsObject



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

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



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

def nullable
  @schema_def['nullable']
end

#one_ofObject



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

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



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

def pattern
  @schema_def['pattern']
end

#propertiesObject



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

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



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

def required
  @schema_def['required']
end

#typeObject



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

def type
  @schema_def['type']
end