Class: Heroics::ResourceSchema

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

Overview

A wrapper around a bare resource element in a JSON schema to make it easier to use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, name) ⇒ ResourceSchema

Instantiate a resource schema.

Parameters:

  • schema (Hash)

    The bare JSON schema to wrap.

  • name (String)

    The name of the resource to identify in the schema.



58
59
60
61
62
63
64
65
66
67
# File 'lib/heroics/schema.rb', line 58

def initialize(schema, name)
  @schema = schema
  @name = name
  link_schema = schema['definitions'][name]['links'] || []

  @links = Hash[link_schema.each_with_index.map do |link, link_index|
                  link_name = Heroics.ruby_name(link['title'])
                  [link_name, LinkSchema.new(schema, name, link_index)]
                end]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



52
53
54
# File 'lib/heroics/schema.rb', line 52

def name
  @name
end

Instance Method Details

#descriptionObject

A description of the resource.



70
71
72
# File 'lib/heroics/schema.rb', line 70

def description
  @schema['definitions'][name]['description']
end

Get a schema for a named link.

Parameters:

  • name (String)

    The name of the link.

Raises:

  • (SchemaError)

    Raised if an unknown link name is provided.



78
79
80
81
82
# File 'lib/heroics/schema.rb', line 78

def link(name)
  schema = @links[name]
  raise SchemaError.new("Unknown link '#{name}'.") unless schema
  schema
end

The link schema children that are part of this resource schema.

Returns:

  • (Array<LinkSchema>)

    The link schema children.



87
88
89
# File 'lib/heroics/schema.rb', line 87

def links
  @links.values
end