Class: Prmd::Schema
- Inherits:
-
Object
- Object
- Prmd::Schema
- Defined in:
- lib/prmd/schema.rb
Overview
Schema object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #dereference(reference) ⇒ Object
-
#href ⇒ String?
Retrieve this schema’s href.
-
#initialize(new_data = {}) ⇒ Schema
constructor
A new instance of Schema.
-
#merge!(schema) ⇒ void
Merge schema data with provided schema.
- #schema_example(schema) ⇒ Object
- #schema_value_example(value) ⇒ Object
- #schemata_example(schemata_id) ⇒ Object
-
#to_json ⇒ String
Convert Schema to JSON.
-
#to_s ⇒ String
Convert Schema to String.
-
#to_yaml ⇒ String
Convert Schema to YAML.
Constructor Details
#initialize(new_data = {}) ⇒ Schema
Returns a new instance of Schema.
27 28 29 30 |
# File 'lib/prmd/schema.rb', line 27 def initialize(new_data = {}) @data = convert_type_to_array(new_data) @schemata_examples = {} end |
Instance Method Details
#[](key) ⇒ Object
53 54 55 |
# File 'lib/prmd/schema.rb', line 53 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
59 60 61 |
# File 'lib/prmd/schema.rb', line 59 def []=(key, value) @data[key] = value end |
#dereference(reference) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/prmd/schema.rb', line 77 def dereference(reference) if reference.is_a?(Hash) if reference.key?('$ref') value = reference.dup key = value.delete('$ref') else return [nil, reference] # no dereference needed end else key, value = reference, {} end begin datum = @data key.gsub(/[^#]*#\//, '').split('/').each do |fragment| datum = datum[fragment] end # last dereference will have nil key, so compact it out # [-2..-1] should be the final key reached before deref dereferenced_key, dereferenced_value = dereference(datum) [ [key, dereferenced_key].compact.last, [dereferenced_value, value].inject({}, &:merge) ] rescue => error $stderr.puts("Failed to dereference `#{key}`") raise error end end |
#href ⇒ String?
Retrieve this schema’s href
167 168 169 |
# File 'lib/prmd/schema.rb', line 167 def href (@data['links'] && @data['links'].find { |link| link['rel'] == 'self' } || {})['href'] end |
#merge!(schema) ⇒ void
This method returns an undefined value.
Merge schema data with provided schema
67 68 69 70 71 72 73 |
# File 'lib/prmd/schema.rb', line 67 def merge!(schema) if schema.is_a?(Schema) @data.merge!(schema.data) else @data.merge!(schema) end end |
#schema_example(schema) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/prmd/schema.rb', line 139 def schema_example(schema) _, dff_schema = dereference(schema) if dff_schema.key?('example') dff_schema['example'] elsif dff_schema.key?('properties') example = {} dff_schema['properties'].each do |key, value| _, value = dereference(value) example[key] = schema_value_example(value) end example elsif dff_schema.key?('items') schema_value_example(dff_schema) end end |
#schema_value_example(value) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/prmd/schema.rb', line 107 def schema_value_example(value) if value.key?('example') value['example'] elsif value.key?('anyOf') id_ref = value['anyOf'].find do |ref| ref['$ref'] && ref['$ref'].split('/').last == 'id' end ref = id_ref || value['anyOf'].first schema_example(ref) elsif value.key?('properties') # nested properties schema_example(value) elsif value.key?('items') # array of objects _, items = dereference(value['items']) if value['items'].key?('example') if items["example"].is_a?(Array) items["example"] else [items['example']] end else [schema_example(items)] end elsif value.key?('enum') value['enum'][0] elsif DefaultExamples.key?(value["format"]) DefaultExamples[value["format"]] elsif DefaultExamples.key?(value["type"][0]) DefaultExamples[value["type"][0]] end end |
#schemata_example(schemata_id) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/prmd/schema.rb', line 157 def schemata_example(schemata_id) _, schema = dereference("#/definitions/#{schemata_id}") @schemata_examples[schemata_id] ||= begin schema_example(schema) end end |
#to_json ⇒ String
Convert Schema to JSON
174 175 176 177 178 179 |
# File 'lib/prmd/schema.rb', line 174 def to_json new_json = JSON.pretty_generate(@data) # nuke empty lines new_json = new_json.split("\n").reject(&:empty?).join("\n") + "\n" new_json end |
#to_s ⇒ String
Convert Schema to String
191 192 193 |
# File 'lib/prmd/schema.rb', line 191 def to_s to_json end |
#to_yaml ⇒ String
Convert Schema to YAML
184 185 186 |
# File 'lib/prmd/schema.rb', line 184 def to_yaml YAML.dump(@data) end |