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

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.



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

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.



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

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.



96
97
98
# File 'lib/heroics/schema.rb', line 96

def links
  @links.values
end