class Idcf::JsonHyperSchema::Analyst

json-hyper-schema analyst

Attributes

load_schema[R]
process_version[R]
schema[R]

Public Instance Methods

expand(schema, options = {}) click to toggle source

json file laod

@param schema [Hash] @param options [Hash] @return [Expand::Base] @raise

# File lib/idcf/json_hyper_schema/analyst.rb, line 41
def expand(schema, options = {})
  return schema unless schema.class == Hash
  @process_version = schema_version(schema)
  # MEMO:
  # Ruby2.7系では以下のような警告がでる
  #
  # Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
  #
  # Ruby2.7系以上だけをサポートの対象とするなら場合分ける必要ないがそうではないので場合分けをしている。
  if RUBY_VERSION >= '2.7'
    result = expand_class.new(**options)
  else
    result = expand_class.new(options)
  end
  result.do!(schema)
  result
end
json_schema_init() click to toggle source
# File lib/idcf/json_hyper_schema/analyst.rb, line 9
def json_schema_init
  configuration = JsonSchema.configuration
  Idcf::JsonHyperSchema::Validation.validations.each do |key, val|
    configuration.register_format(key, val)
  end
  self
end
load(path, options = {}) click to toggle source

json file laod

@param path [String] @param options [Hash] @return [Hash] @raise

# File lib/idcf/json_hyper_schema/analyst.rb, line 23
def load(path, options = {})
  @schema ||= {}
  unless @schema[path].nil?
    @load_schema = @schema[path]
    return self
  end
  j             = JSON.parse(File.read(path))
  @load_schema  = expand(j, options).schema
  @schema[path] = @load_schema
  self
end

Protected Instance Methods

base_module() click to toggle source

base module namespace

@return [String]

# File lib/idcf/json_hyper_schema/analyst.rb, line 92
def base_module
  list = self.class.to_s.underscore.split('/')
  list.pop
  list.join('/')
end
expand_class() click to toggle source

expand class

@return [Class]

# File lib/idcf/json_hyper_schema/analyst.rb, line 101
def expand_class
  path = "#{base_module}/expands/#{@process_version}"
  require path
  path.classify.constantize
end
schema_version(schema) click to toggle source

schema version

@param schema [Hash] @return [String] @raise

# File lib/idcf/json_hyper_schema/analyst.rb, line 141
def schema_version(schema)
  v_list = schema_version_list
  result = v_list.last
  check  = schema.is_a?(JsonSchema::Schema) ? schema.data : schema
  unless check['$schema'].nil? || check['$schema'].empty?
    result = schema_version_str(check['$schema'])
  end

  raise 'The undefined version.' if v_list.index(result).nil?
  result
end
schema_version_list() click to toggle source

schema version list support schema version list

@return array

# File lib/idcf/json_hyper_schema/analyst.rb, line 157
def schema_version_list
  result = []
  Dir.glob("#{File.dirname(__FILE__)}/expands/v*.rb") do |f|
    result << File.basename(f, '.rb')
  end
  result.sort
end
schema_version_str(schema) click to toggle source

schema version str

@param schema [String] $schema @return [String]

# File lib/idcf/json_hyper_schema/analyst.rb, line 169
def schema_version_str(schema)
  schema =~ /draft[^\d]{1,2}(\d+)/
  mt = Regexp.last_match(1)
  return "v#{mt.to_i}" unless mt.nil?
  ''
end