class Circuitdata::Schema

Constants

BASE_PATH
CACHE
CACHE_PATH
TYPE_PATH

Public Class Methods

add_questions_to_category(category, question_id, question_schema, path) click to toggle source
# File lib/circuitdata/schema.rb, line 82
def self.add_questions_to_category(category, question_id, question_schema, path)
  category_questions = category[:questions]
  id = :"#{category.fetch(:id)}/#{question_id}"

  if question_schema.fetch(:type) == "object"
    category_questions << build_category(id, question_schema, path)
    return
  end

  question = {
    id: id,
    code: question_id,
    name: question_id.to_s.humanize,
    description: "",
  }
  category_questions << question

  [:default, :enforced, :restricted].each do |question_type|
    schema = question_schema.dup
    question[:description] = schema.delete(:description) || question[:description]

    question[question_type] = {
      schema: schema,
      path: json_pointer(path + [category[:id], question_id], question_type),
    }
    question[:uom] ||= schema[:uom]
  end
end
build_category(category_id, category_schema, pointer_path) click to toggle source
# File lib/circuitdata/schema.rb, line 40
def self.build_category(category_id, category_schema, pointer_path)
  category = {
    id: category_id,
    name: name_from_id(category_id),
    questions: [],
    array?: category_schema[:type] == "array",
  }
  if category_schema.has_key?(:properties)
    questions = category_schema.fetch(:properties)
  elsif category_schema.has_key?(:patternProperties)
    questions = category_schema.fetch(:patternProperties)
  elsif category_schema.fetch(:type) == "array"
    questions = category_schema.fetch(:items)
    if questions.has_key?(:oneOf)
      questions = questions.fetch(:oneOf)
      return one_of_category(category, questions, pointer_path)
    elsif questions.has_key?(:properties)
      questions = questions.fetch(:properties)
    end
  else
    raise "Unknown type"
  end

  questions.each do |question_id, question_schema|
    add_questions_to_category(category, question_id, question_schema, pointer_path)
  end
  category
end
cached(*path) click to toggle source
# File lib/circuitdata/schema.rb, line 139
def self.cached(*path)
  file_path = File.join(CACHE_PATH, *path.map(&:to_s)) + ".json"

  CACHE[file_path] ||= JSON.parse(File.read(file_path), symbolize_names: true)
end
json_pointer(path_parts, type) click to toggle source
# File lib/circuitdata/schema.rb, line 111
def self.json_pointer(path_parts, type)
  ([""] + path_parts - [:properties, :patternProperties])
    .join("/")
    .sub("enforced", type.to_s)
end
layer_kinds() click to toggle source
# File lib/circuitdata/schema.rb, line 117
def self.layer_kinds
  product_schema = type_schema(:products)
  kinds = product_schema.dig(:layers, :items, :oneOf).map { |of| of.dig(:properties, :function, :enum).first }
  kinds.sort
end
name_from_id(id) click to toggle source
# File lib/circuitdata/schema.rb, line 135
def self.name_from_id(id)
  id.to_s.split("/").last.humanize
end
one_of_category(category, questions, pointer_path) click to toggle source
# File lib/circuitdata/schema.rb, line 69
def self.one_of_category(category, questions, pointer_path)
  category.delete(:questions)
  category[:one_of] = questions.map do |question_set|
    {
      match_attributes: {
        function: question_set.fetch(:properties).fetch(:function).fetch(:enum).first,
      },
      group: build_category(category.fetch(:id), question_set, pointer_path),
    }
  end
  category
end
process_kinds() click to toggle source
# File lib/circuitdata/schema.rb, line 123
def self.process_kinds
  product_schema = type_schema(:products)
  kinds = product_schema.dig(:processes, :items, :oneOf).map { |of| of.dig(:properties, :function, :enum).first }
  kinds.sort
end
product_questions(cached: true) click to toggle source
# File lib/circuitdata/schema.rb, line 22
def self.product_questions(cached: true)
  cached ? cached(:questions, :product) : questions_for_type(:products)
end
profile_questions(cached: true) click to toggle source
# File lib/circuitdata/schema.rb, line 26
def self.profile_questions(cached: true)
  cached ? cached(:questions, :profile) : questions_for_type(:profiles)
end
questions_for_type(type) click to toggle source
# File lib/circuitdata/schema.rb, line 30
def self.questions_for_type(type)
  pointer_path = TYPE_PATH.fetch(type)
  result = []
  type_schema(type).each do |category_id, category_schema|
    next if category_id == :version
    result << build_category(category_id, category_schema, pointer_path)
  end
  result
end
type_schema(type) click to toggle source
# File lib/circuitdata/schema.rb, line 129
def self.type_schema(type)
  schema = Circuitdata.dereferenced_schema
  pointer_path = TYPE_PATH.fetch(type)
  schema.dig(*pointer_path)
end