Class: Heroics::ResourceSchema
- Inherits:
-
Object
- Object
- Heroics::ResourceSchema
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#description ⇒ Object
A description of the resource.
-
#initialize(schema, name) ⇒ ResourceSchema
constructor
Instantiate a resource schema.
-
#link(name) ⇒ Object
Get a schema for a named link.
-
#links ⇒ Array<LinkSchema>
The link schema children that are part of this resource schema.
Constructor Details
#initialize(schema, name) ⇒ ResourceSchema
Instantiate a resource 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
#name ⇒ Object (readonly)
Returns the value of attribute name.
52 53 54 |
# File 'lib/heroics/schema.rb', line 52 def name @name end |
Instance Method Details
#description ⇒ Object
A description of the resource.
79 80 81 |
# File 'lib/heroics/schema.rb', line 79 def description @schema['definitions'][name]['description'] end |
#link(name) ⇒ Object
Get a schema for a named link.
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 |
#links ⇒ Array<LinkSchema>
The link schema children that are part of this resource schema.
96 97 98 |
# File 'lib/heroics/schema.rb', line 96 def links @links.values end |