Class: Restspec::Schema::SingleSchemaDSL

Inherits:
Object
  • Object
show all
Includes:
Types::TypeMethods
Defined in:
lib/restspec/schema/dsl.rb

Overview

The DSL to use inside schema and mixin blocks of a DSL instance block. It defines specific things of a schema or a group of them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types::TypeMethods

#array, #boolean, #date, #datetime, #decimal, #decimal_string, #embedded_schema, #hash, #integer, #null, #one_of, #schema_id, #string

Constructor Details

#initialize(name, options = {}, mixins = {}) ⇒ SingleSchemaDSL

Returns a new instance of SingleSchemaDSL.



70
71
72
73
# File 'lib/restspec/schema/dsl.rb', line 70

def initialize(name, options = {}, mixins = {})
  self.schema = Schema.new(name, options)
  self.mixins = mixins
end

Instance Attribute Details

#schemaSchema

Returns the current schema.

Returns:

  • (Schema)

    the current schema



68
69
70
# File 'lib/restspec/schema/dsl.rb', line 68

def schema
  @schema
end

Instance Method Details

#attribute(name, type, options = {}) ⇒ Object

Creates an attribute and saving it into the schema. It uses the same parameters as the Attribute#initialize method.

Examples:


schema :books do
 attribute :title, string
 attribute :created_at, datetime, :for => [:response]
end

Parameters:

  • name

    the name of the attribute

  • type

    an instance of a subclass of Types::BasicType that works like the type of this attribute, allowing the type to generate examples and run validations based on this attribute.

  • options (defaults to: {})

    that can be the following:

    • example: A callable object (eg: a lambda) that returns something.
    • for: Defines what abilities this attributes has. This is an array that can contains none, some or all the symbols :response and :payload. This option defaults to [:response, :payload], allowing the attribute to be used for run validations from Checker#check! (:response) and for generating payload from Restspec::Schema::SchemaExample#value (:payload).


86
87
88
89
# File 'lib/restspec/schema/dsl.rb', line 86

def attribute(name, type, options = {})
  new_attribute = Attribute.new(name, type, options)
  schema.attributes[name.to_s] = new_attribute
end

#include_attributes(name) ⇒ Object

Includes a mixin generated by the DSL#mixin function into the schema.

Examples:


mixin :timestamps do
  attribute :created_at, date
  attribute :updated_at, date
end

schema :book do
  include_attributes :timestamps
end

schema :celphones do
  include_attributes :timestamps
end

Parameters:

  • name (Symbol)

    the mixin name



97
98
99
# File 'lib/restspec/schema/dsl.rb', line 97

def include_attributes(name)
  self.instance_eval &mixins.fetch(name)
end