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 = {}, options = {}) ⇒ 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 = {}, options = {}) ⇒ Schema
Returns a new instance of Schema.
28 29 30 31 |
# File 'lib/prmd/schema.rb', line 28 def initialize(new_data = {}, = {}) @data = convert_type_to_array(new_data, ) @schemata_examples = {} end |
Instance Method Details
#[](key) ⇒ Object
58 59 60 |
# File 'lib/prmd/schema.rb', line 58 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
64 65 66 |
# File 'lib/prmd/schema.rb', line 64 def []=(key, value) @data[key] = value end |
#dereference(reference) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/prmd/schema.rb', line 82 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
174 175 176 |
# File 'lib/prmd/schema.rb', line 174 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
72 73 74 75 76 77 78 |
# File 'lib/prmd/schema.rb', line 72 def merge!(schema) if schema.is_a?(Schema) @data.merge!(schema.data) else @data.merge!(schema) end end |
#schema_example(schema) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/prmd/schema.rb', line 146 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
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 137 138 139 140 141 142 143 |
# File 'lib/prmd/schema.rb', line 112 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?('allOf') value['allOf'].map { |ref| schema_example(ref) }.reduce({}, &:merge) 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
164 165 166 167 168 169 |
# File 'lib/prmd/schema.rb', line 164 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
181 182 183 184 185 186 |
# File 'lib/prmd/schema.rb', line 181 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
198 199 200 |
# File 'lib/prmd/schema.rb', line 198 def to_s to_json end |
#to_yaml ⇒ String
Convert Schema to YAML
191 192 193 |
# File 'lib/prmd/schema.rb', line 191 def to_yaml YAML.dump(@data) end |