Class: Jsapi::DSL::Schema

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

Overview

Used to specify details of a schema.

Direct Known Subclasses

Parameter, RequestBody, Response

Instance Method Summary collapse

Methods inherited from Node

#initialize, #method_missing, #respond_to_missing?

Constructor Details

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

Dynamic Method Handling

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

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.



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

def all_of(*schemas)
  schemas.each { |schema| _meta_model.add_all_of({ schema: schema }) }
end

#example(example) ⇒ Object

Specifies a sample matching the schema.



16
17
18
# File 'lib/jsapi/dsl/schema.rb', line 16

def example(example)
  _meta_model.add_example(example)
end

#format(format) ⇒ Object

Overrides Kernel#format to handle format as a keyword.



21
22
23
# File 'lib/jsapi/dsl/schema.rb', line 21

def format(format) # :nodoc:
  _keyword(:format, format)
end

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

Specifies 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".



32
33
34
35
36
37
38
39
# File 'lib/jsapi/dsl/schema.rb', line 32

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

Specifies 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".



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsapi/dsl/schema.rb', line 54

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

Defines a property.

property 'foo', type: 'string'

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

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/jsapi/dsl/schema.rb', line 75

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