class Heroics::ResourceSchema

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

Attributes

name[R]

Public Class Methods

new(schema, name) click to toggle source

Instantiate a resource schema.

@param schema [Hash] The bare JSON schema to wrap. @param name [String] The name of the resource to identify in the schema.

# File lib/heroics/schema.rb, line 58
def initialize(schema, name)
  @schema = schema
  @name = name
  link_schema = schema['definitions'][name]['links'] || []

  duplicate_names = link_schema
    .group_by { |link| Heroics.ruby_name(link['title']) }
    .select { |k, v| v.size > 1 }
    .map(&:first)
  if !duplicate_names.empty?
    raise SchemaError.new("Duplicate '#{name}' link names: " +
                          "'#{duplicate_names.join("', '")}'.")
  end

  @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

Public Instance Methods

description() click to toggle source

A description of the resource.

# File lib/heroics/schema.rb, line 79
def description
  @schema['definitions'][name]['description']
end