Module: SchemaTools::Modules::Read
Overview
Read schemas into a hash
Instance Method Summary collapse
-
#_handle_reference_properties(schema) ⇒ Object
Merge referenced property definitions into the given schema.
-
#read(schema_name, path_or_schema = nil) ⇒ Object
Read a schema and return it as hash.
-
#read_all(path = nil) ⇒ Array<HashWithIndifferentAccess>
Read all available schemas from a given path(folder +subfolders) and return the found object definitions them as array.
-
#registry ⇒ Hash{String=>Hash{Symbol=>HashWithIndifferentAccess}}
Variable remembering already read-in schema’s { :invoice =>schema :credit_note =>schema } }.
- #registry_reset ⇒ Object
Instance Method Details
#_handle_reference_properties(schema) ⇒ Object
Merge referenced property definitions into the given schema. e.g. each object has an updated_at field which we define in a single location(external file) instead of repeating the property def in each schema.
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/schema_tools/modules/read.rb', line 101 def _handle_reference_properties(schema) return unless schema["properties"] schema["properties"].each { |key, value| next unless value["$ref"] json_pointer = value["$ref"] values_from_pointer = RefResolver.load_json_pointer json_pointer schema["properties"][key].merge!(values_from_pointer) {|key, old, new| old } schema["properties"][key].delete("$ref") } end |
#read(schema_name, path_or_schema = nil) ⇒ Object
Read a schema and return it as hash. You can supply a path or the global path defined in #SchemaTools.schema_path is used. Schemata are returned from cache(#registry) if present to prevent filesystem round-trips. The cache can be reset with #registry_reset
@return schema as hash, nil if schema is not an object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/schema_tools/modules/read.rb', line 35 def read(schema_name, path_or_schema=nil) schema_name = schema_name.to_sym return registry[schema_name] if registry[schema_name] if path_or_schema.is_a?(::Hash) path = nil plain_data = path_or_schema.to_json elsif path_or_schema.is_a?(::String) || path_or_schema.nil? path = path_or_schema file_path = File.join(path || SchemaTools.schema_path, "#{schema_name}.json") unless File.exist?(file_path) # check if file exists else try to find first real path in sub-dirs recursive_search = Dir.glob( File.join(SchemaTools.schema_path, '**/*', "#{schema_name}.json"))[0] # use only if we found something, else keep path which will throw error on file.open later file_path = recursive_search || file_path end else raise ArgumentError, 'Second parameter must be a path or a schema!' end plain_data ||= File.open(file_path, 'r'){|f| f.read} schema = ActiveSupport::JSON.decode(plain_data).with_indifferent_access # only import object definitions, shared property definitions are handled separate return unless schema[:type] == 'object' if schema[:extends] extends = schema[:extends].is_a?(Array) ? schema[:extends] : [ schema[:extends] ] extends.each do |ext_name| ext = read(ext_name, path) # current schema props win schema[:properties] = ext[:properties].merge(schema[:properties]) end end _handle_reference_properties schema registry[ schema_name ] = schema end |
#read_all(path = nil) ⇒ Array<HashWithIndifferentAccess>
Read all available schemas from a given path(folder +subfolders) and return the found object definitions them as array. Also populates the registry
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/schema_tools/modules/read.rb', line 78 def read_all(path=nil) schemas = [] file_paths = if path [File.join(path, '*.json')] else [ File.join( SchemaTools.schema_path, '*.json'), File.join( SchemaTools.schema_path, '**/*', '*.json') ] end Dir.glob( file_paths ).each do |file| schema_name = File.basename(file, '.json').to_sym schemas << read(schema_name, path) end schemas.compact! schemas end |
#registry ⇒ Hash{String=>Hash{Symbol=>HashWithIndifferentAccess}}
Variable remembering already read-in schema’s
:invoice =>{schema
:credit_note =>schema
}
}
18 19 20 |
# File 'lib/schema_tools/modules/read.rb', line 18 def registry @registry ||= {} end |
#registry_reset ⇒ Object
22 23 24 |
# File 'lib/schema_tools/modules/read.rb', line 22 def registry_reset @registry = nil end |