Class: Inspec::Schema::Primitives::SchemaType

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/schema/primitives.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, body, dependencies) ⇒ SchemaType

Returns a new instance of SchemaType.



38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#dependsObject

Returns the value of attribute depends.



37
38
39
# File 'lib/inspec/schema/primitives.rb', line 37

def depends
  @depends
end

#nameObject

Returns the value of attribute name.



37
38
39
# File 'lib/inspec/schema/primitives.rb', line 37

def name
  @name
end

Instance Method Details

#all_dependsObject

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



68
69
70
71
72
73
74
75
76
77
78
# 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

#bodyObject

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



51
52
53
54
55
# File 'lib/inspec/schema/primitives.rb', line 51

def body
  @body.merge({
      "title" => @name,
  })
end

#refObject

Yields this type as a json schema ref



63
64
65
# File 'lib/inspec/schema/primitives.rb', line 63

def ref
  { "$ref" => "#/definitions/#{ref_name}" }
end

#ref_nameObject

Formats this to have a JSON pointer compatible title



58
59
60
# File 'lib/inspec/schema/primitives.rb', line 58

def ref_name
  @name.gsub(/\s+/, "_")
end