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.
12 13 14 15 |
# File 'lib/prmd/schema.rb', line 12 def initialize(new_data = {}) @data = convert_type_to_array(new_data) @schemata_examples = {} end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 |
# File 'lib/prmd/schema.rb', line 38 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
44 45 46 |
# File 'lib/prmd/schema.rb', line 44 def []=(key, value) @data[key] = value end |
#dereference(reference) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/prmd/schema.rb', line 62 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
142 143 144 |
# File 'lib/prmd/schema.rb', line 142 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
52 53 54 55 56 57 58 |
# File 'lib/prmd/schema.rb', line 52 def merge!(schema) if schema.is_a?(Schema) @data.merge!(schema.data) else @data.merge!(schema) end end |
#schema_example(schema) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/prmd/schema.rb', line 114 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/prmd/schema.rb', line 92 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') [items['example']] else [schema_example(items)] end end end |
#schemata_example(schemata_id) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/prmd/schema.rb', line 132 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
149 150 151 152 153 154 |
# File 'lib/prmd/schema.rb', line 149 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
166 167 168 |
# File 'lib/prmd/schema.rb', line 166 def to_s to_json end |
#to_yaml ⇒ String
Convert Schema to YAML
159 160 161 |
# File 'lib/prmd/schema.rb', line 159 def to_yaml YAML.dump(@data) end |