class Idcf::JsonHyperSchema::Expands::Base
json schema expand json schema v4
Constants
- FULL_HREF_REGEXP
Attributes
definition_ids[R]
options[R]
origin[R]
schema[R]
Public Class Methods
new(global_access: true)
click to toggle source
initialize
@param global_access [Boolean] Is an external reference performed?
# File lib/idcf/json_hyper_schema/expands/base.rb, line 18 def initialize(global_access: true) @definition_ids = {} @options = { global_access: global_access } end
Public Instance Methods
do!(schema)
click to toggle source
do
@param schema [Hash] @return [Hash] @raise
# File lib/idcf/json_hyper_schema/expands/base.rb, line 31 def do!(schema) raise 'schema is not hash' unless schema.class == Hash target = schema.deep_dup @origin = schema.deep_dup @definition_ids = delete_id(make_ids(target)) @schema = exp(target) @schema end
find(path)
click to toggle source
ref find
@param path [String] @return Mixed
# File lib/idcf/json_hyper_schema/expands/base.rb, line 44 def find(path) if path =~ FULL_HREF_REGEXP return path unless @options[:global_access] global_find(path) else local_search(path) end end
Protected Instance Methods
delete_id(data)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 77 def delete_id(data) return data unless data.class == Hash data.delete('id') if data['id'].class != Hash data.each do |k, v| data[k] = delete_id(v) end data end
exp(schema)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 87 def exp(schema) case schema when Hash schema = exp_hash(schema) when Array schema = exp_array(schema) end schema end
exp_array(schema)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 112 def exp_array(schema) schema.each_with_index do |v, k| schema[k] = expansion(k, v) end schema end
exp_hash(schema)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 98 def exp_hash(schema) list = schema.keys list.each do |k| exp = expansion(k, schema[k]) if k == '$ref' && exp.class == Hash schema.delete(k) schema.merge!(exp) else schema[k] = exp end end schema end
expansion(key, piece)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 119 def expansion(key, piece) case piece when String expansion_string(key, piece) when Hash, Array exp(piece) else piece end end
expansion_string(key, piece)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 130 def expansion_string(key, piece) return piece unless key == '$ref' d = find(piece) return piece if d.nil? return exp(d) if d.class == Hash || d.class == Array key = piece key = Regexp.last_match(1) if piece =~ /#(.*)/ { key => d } end
global_find(path)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 143 def global_find(path) return nil unless path =~ /#.+/ res = URI.open(path) code, message = res.status raise "error: #{message}" unless code == '200' j = JSON.parse(res.read) expand = Idcf::JsonHyperSchema::Analyst.new.expand(j) expand.find(Regexp.last_match(0)) end
local_search(path)
click to toggle source
local data search definitions_id or local_path
@param path [String] ex) definitions/name @return Mixed
# File lib/idcf/json_hyper_schema/expands/base.rb, line 159 def local_search(path) ids = @definition_ids[path] return ids unless ids.nil? paths = local_search_path_list(path) return nil if paths.empty? data = search_data(@origin, paths) data.nil? ? nil : delete_id(data.deep_dup) end
local_search_path_list(path)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 168 def local_search_path_list(path) path[1, path.size - 1].split('/').select do |v| v.strip != '' end end
make_ids(schema)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 55 def make_ids(schema) {}.tap do |result| next if schema['definitions'].nil? schema['definitions'].each_value do |v| result[v['id']] = v.deep_dup unless v['id'].nil? result.merge!(search_child_ids(v)) end end end
search_child_ids(child)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 65 def search_child_ids(child) result = {} return result unless child.class == Hash child.each_value do |v| next unless v.class == Hash id = v['id'] result[id] = v.deep_dup unless id.nil? result.merge!(search_child_ids(v)) end result end
search_data(data, paths)
click to toggle source
# File lib/idcf/json_hyper_schema/expands/base.rb, line 174 def search_data(data, paths) result = data.deep_dup paths.each do |k| next if k.blank? key = result.class == Hash ? k : k.to_i result = result[key] break if result.nil? end result end