Method: SchemaTools::Modules::Read#read
- Defined in:
- lib/schema_tools/modules/read.rb
#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 |
# 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) schema = Schema.new(path_or_schema) 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 schema = Schema.new(file_path) else raise ArgumentError, 'Second parameter must be a path or a schema!' end # only import object definitions, shared property definitions are handled separate return unless schema[:type] == 'object' registry[ schema_name ] = schema return schema end |