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


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


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

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

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

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