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, mixins = {}) ⇒ SingleSchemaDSL

Returns a new instance of SingleSchemaDSL


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

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

Instance Attribute Details

#schemaSchema

Returns the current schema

Returns:

  • (Schema)

    the current schema


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

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).

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

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


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

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