Class: Inspec::Schema::Primitives::SchemaType
- Inherits:
-
Object
- Object
- Inspec::Schema::Primitives::SchemaType
- 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
-
#depends ⇒ Object
Returns the value of attribute depends.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#all_depends ⇒ Object
Recursively acquire all depends for this schema.
-
#body ⇒ Object
Produce this schema types generated body.
-
#initialize(name, body, dependencies, description = nil) ⇒ SchemaType
constructor
A new instance of SchemaType.
-
#ref ⇒ Object
Yields this type as a json schema ref.
-
#ref_name ⇒ Object
Formats this to have a JSON pointer compatible title.
Constructor Details
#initialize(name, body, dependencies, description = nil) ⇒ SchemaType
Returns a new instance of SchemaType.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/inspec/schema/primitives.rb', line 38 def initialize(name, body, dependencies, description = nil) # 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) # The description of the type @description = description end |
Instance Attribute Details
#depends ⇒ Object
Returns the value of attribute depends.
37 38 39 |
# File 'lib/inspec/schema/primitives.rb', line 37 def depends @depends end |
#name ⇒ Object
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_depends ⇒ Object
Recursively acquire all depends for this schema. Return them sorted by name
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/inspec/schema/primitives.rb', line 71 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 ⇒ Object
Produce this schema types generated body. Use to actually define the ref!
53 54 55 56 57 58 |
# File 'lib/inspec/schema/primitives.rb', line 53 def body @body.merge({ "description" => @description, "title" => @name, }).compact end |
#ref ⇒ Object
Yields this type as a json schema ref
66 67 68 |
# File 'lib/inspec/schema/primitives.rb', line 66 def ref { "$ref" => "#/definitions/#{ref_name}" } end |
#ref_name ⇒ Object
Formats this to have a JSON pointer compatible title
61 62 63 |
# File 'lib/inspec/schema/primitives.rb', line 61 def ref_name @name.gsub(/\s+/, "_") end |