Class: Jsapi::DSL::Schema
Overview
Used to define a schema.
Direct Known Subclasses
Instance Method Summary collapse
-
#all_of(*schemas) ⇒ Object
Includes all of the properties from
schemas. -
#example(example) ⇒ Object
Adds a sample matching the schema.
-
#format(format) ⇒ Object
Specifies the format of a string.
-
#items(**keywords, &block) ⇒ Object
Defines the kind of items that can be contained in an array.
-
#model(klass = nil, &block) ⇒ Object
Defines the model class to access nested object parameters by.
-
#property(name, **keywords, &block) ⇒ Object
Adds a property.
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.
19 20 21 |
# File 'lib/jsapi/dsl/schema.rb', line 19 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'
69 70 71 |
# File 'lib/jsapi/dsl/schema.rb', line 69 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.
89 90 91 |
# File 'lib/jsapi/dsl/schema.rb', line 89 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".
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jsapi/dsl/schema.rb', line 100 def items(**keywords, &block) unless @meta_model.respond_to?(:items=) raise Error, "items isn't supported for '#{@meta_model.type}'" end define do @meta_model.items = keywords @meta_model.items.tap do |items| Schema.new(items, &block) if block end end end |
#model(klass = nil, &block) ⇒ Object
Defines the model class to access nested object parameters by.
model Foo do
def
# ...
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".
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/jsapi/dsl/schema.rb', line 176 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".
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/jsapi/dsl/schema.rb', line 211 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 @meta_model.add_property(name, keywords).tap do |property_model| Schema.new(property_model, &block) if block end end end |