class Inspec::Schema::Primitives::SchemaType

Use this class to quickly add/use object types to/in a definition block

Attributes

depends[RW]
name[RW]

Public Class Methods

new(name, body, dependencies) click to toggle source
# File lib/inspec/schema/primitives.rb, line 38
def initialize(name, body, dependencies)
  # Validate the schema
  Primitives.validate_schema(body)
  # The title of the type
  @name = name
  # The body of the type
  @body = body
  # What SchemaType[]s it depends on. In essence, any thing that you .ref in the body
  @depends = Set.new(dependencies)
end

Public Instance Methods

all_depends() click to toggle source

Recursively acquire all depends for this schema. Return them sorted by name

# File lib/inspec/schema/primitives.rb, line 68
def all_depends
  result = @depends
  # Fetch all from children
  @depends.each do |nested_type|
    # Yes, converting back to set here does some duplicate sorting.
    # But here, performance really isn't our concern.
    result += Set.new(nested_type.all_depends)
  end
  # Return the results as a sorted array
  Array(result).sort_by(&:name)
end
body() click to toggle source

Produce this schema types generated body. Use to actually define the ref!

# File lib/inspec/schema/primitives.rb, line 51
def body
  @body.merge({
      "title" => @name,
  })
end
ref() click to toggle source

Yields this type as a json schema ref

# File lib/inspec/schema/primitives.rb, line 63
def ref
  { "$ref" => "#/definitions/#{ref_name}" }
end
ref_name() click to toggle source

Formats this to have a JSON pointer compatible title

# File lib/inspec/schema/primitives.rb, line 58
def ref_name
  @name.gsub(/\s+/, "_")
end