Class: Jsapi::DSL::Schema

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/dsl/schema.rb

Overview

Used to define a schema.

Direct Known Subclasses

Parameter, RequestBody, Response

Instance Method Summary collapse

Methods inherited from Base

#import, #import_relative, #initialize, #respond_to_missing?

Constructor Details

This class inherits a constructor from Jsapi::DSL::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Jsapi::DSL::Base

Instance Method Details

#all_of(*schemas) ⇒ Object

Includes all of the properties from schemas. Each argument must be the name of a schema defined by ClassMethods#api_schema or Definitions#schema.



10
11
12
# File 'lib/jsapi/dsl/schema.rb', line 10

def all_of(*schemas)
  schemas.each { |schema| @meta_model.add_all_of({ ref: schema }) }
end

#example(example) ⇒ Object

Adds a sample matching the schema.

example 'foo'


59
60
61
# File 'lib/jsapi/dsl/schema.rb', line 59

def example(example)
  @meta_model.add_example(example)
end

#format(format) ⇒ Object

Specifies the format of a string.

format 'date-time'

Raises an Error if type is other than "string".

See Meta::Schema::String#format for further information.



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

def format(format)
  keyword(:format, format)
end

#items(**keywords, &block) ⇒ Object

Defines the kind of items that can be contained in an array.

items do
  property 'foo', type: 'string'
end

Raises an Error if type is other than "array".



90
91
92
93
94
95
96
97
# File 'lib/jsapi/dsl/schema.rb', line 90

def items(**keywords, &block)
  unless @meta_model.respond_to?(:items=)
    raise Error, "items isn't supported for '#{@meta_model.type}'"
  end

  @meta_model.items = keywords
  Schema.new(@meta_model.items, &block) if block
end

#model(klass = nil, &block) ⇒ Object

Defines the model class to access nested object parameters by.

model Foo do
  def bar
    # ...
  end
end

klass can be any subclass of Model::Base. If block is given, an anonymous class is created that inherits either from klass or Model::Base.

Raises an Error if type is other than "object".



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/jsapi/dsl/schema.rb', line 161

def model(klass = nil, &block)
  unless @meta_model.respond_to?(:model=)
    raise Error, "model isn't supported for '#{@meta_model.type}'"
  end

  if block
    klass = Class.new(klass || Model::Base)
    klass.class_eval(&block)
  end
  @meta_model.model = klass
end

#property(name, **keywords, &block) ⇒ Object

Adds a property.

property 'foo', type: 'string'

property 'foo', type: 'object' do
  property 'bar', type: 'string'
end

Raises an Error if type is other than "object".



196
197
198
199
200
201
202
203
204
205
# File 'lib/jsapi/dsl/schema.rb', line 196

def property(name, **keywords, &block)
  define('property', name.inspect) do
    unless @meta_model.respond_to?(:add_property)
      raise Error, "property isn't supported for '#{@meta_model.type}'"
    end

    property_model = @meta_model.add_property(name, keywords)
    Schema.new(property_model, &block) if block
  end
end