class JSON::SchemaBuilder::Entity

Attributes

children[RW]
error[RW]
fragment[RW]
fragments[RW]
name[RW]
options[RW]
parent[RW]

Public Class Methods

disable_attributes!(*attributes) click to toggle source
# File lib/json/schema_builder/entity.rb, line 30
def self.disable_attributes!(*attributes)
  attributes.each do |attr|
    undef_method attr rescue NameError
    undef_method "#{attr}=" rescue NameError
  end
end
new(name, opts = { }, &block) click to toggle source
# File lib/json/schema_builder/entity.rb, line 37
def initialize(name, opts = { }, &block)
  @name = name
  @children = []
  @fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
  @fragments["#/"] << self if opts[:root]
  self.type = self.class.registered_type
  initialize_parent_with opts
  initialize_with opts
  eval_block &block
  extract_types
  @initialized = true
end

Public Instance Methods

add_fragment(child) click to toggle source
# File lib/json/schema_builder/entity.rb, line 64
def add_fragment(child)
  @fragments[child.fragment] << child
  parent.add_fragment(child) if @parent
end
as_json() click to toggle source
# File lib/json/schema_builder/entity.rb, line 95
def as_json
  schema.as_json
end
extend(child_name, &block) click to toggle source
# File lib/json/schema_builder/entity.rb, line 57
def extend(child_name, &block)
  children.find { |c| c.name == child_name.to_sym }.tap do |child|
    raise "Property #{child_name} does not exist" unless child
    child.eval_block(&block) if block_given?
  end
end
initialized?() click to toggle source
# File lib/json/schema_builder/entity.rb, line 50
def initialized?
  !!@initialized
end
inspect() click to toggle source
# File lib/json/schema_builder/entity.rb, line 99
def inspect
  "#<#{self.class.name}:#{object_id} @schema=#{schema.as_json}>"
end
merge_children!() click to toggle source
# File lib/json/schema_builder/entity.rb, line 88
def merge_children!
  return if any_of.present?
  children.each do |child|
    schema.merge! child.schema
  end
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/json/schema_builder/entity.rb, line 111
def method_missing(method_name, *args, &block)
  if @parent_context && respond_to?(method_name, true)
    @parent_context.send method_name, *args, &block
  else
    super
  end
end
reinitialize() click to toggle source
# File lib/json/schema_builder/entity.rb, line 54
def reinitialize
end
required() click to toggle source
# File lib/json/schema_builder/entity.rb, line 79
def required
  schema["required"] || []
end
required=(*values) click to toggle source
# File lib/json/schema_builder/entity.rb, line 83
def required=(*values)
  @parent.schema["required"] ||= []
  @parent.schema["required"] << @name if values.any?
end
reset_fragment() click to toggle source
# File lib/json/schema_builder/entity.rb, line 69
def reset_fragment
  @fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
  root._reset_fragments
  root.fragments["#/"] << root
end
respond_to?(method_name, include_all = false) click to toggle source
Calls superclass method
# File lib/json/schema_builder/entity.rb, line 103
def respond_to?(method_name, include_all = false)
  if @parent_context
    @parent_context.respond_to? method_name, include_all
  else
    super
  end
end
schema() click to toggle source
# File lib/json/schema_builder/entity.rb, line 75
def schema
  @schema ||= Schema.new({}, self)
end

Protected Instance Methods

_reset_fragments() click to toggle source
# File lib/json/schema_builder/entity.rb, line 128
def _reset_fragments
  @fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
  @fragment = if parent
    [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
  else
    "#/"
  end

  children.each { |child| child._reset_fragments }
  parent.add_fragment(self) if parent
end
eval_block(&block) click to toggle source
# File lib/json/schema_builder/entity.rb, line 170
def eval_block(&block)
  if block_given?
    @parent_context = block.binding.eval 'self'
    instance_exec self, &block
  end
end
extract_types() click to toggle source
# File lib/json/schema_builder/entity.rb, line 140
def extract_types
  any_of(null) if @nullable
  if any_of.present?
    everything_else = schema.data.reject { |k, v| k == "anyOf" }
    return unless everything_else.present?
    schema.data.select! { |k, v| k == "anyOf" }
    schema.data["anyOf"].unshift everything_else
  end
end
initialize_parent_with(opts) click to toggle source
# File lib/json/schema_builder/entity.rb, line 150
def initialize_parent_with(opts)
  @parent = opts.delete :parent
  if parent
    @fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
    parent.children << self
    parent.add_fragment(self)
  else
    @fragment = "#/"
  end
end
initialize_with(opts) click to toggle source
# File lib/json/schema_builder/entity.rb, line 161
def initialize_with(opts)
  @nullable = opts.delete :null
  @options = opts.delete(:root).class.options.to_h if opts[:root]
  opts.each_pair do |key, value|
    next if value.nil?
    send :"#{ key }=", value
  end
end
root() click to toggle source
# File lib/json/schema_builder/entity.rb, line 121
def root
  return @root if @root
  node = self
  node = node.parent while node.parent
  @root = node
end