class Restspec::Schema::DSL

The Schema DSL is what should be used inside the `schemas.rb` file. This class is related to the top-level namespace of the DSL.

Attributes

mixins[RW]

Public Class Methods

new() click to toggle source
# File lib/restspec/schema/dsl.rb, line 6
def initialize
  self.mixins = {}
end

Public Instance Methods

mixin(name, &definition) click to toggle source

Generates a set of calls that can be executed in many schemas with {SingleSchemaDSL#include_attributes}.

They are useful to share attributes.

@example

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

@param name {Symbol} the mixin's name @param definition A block that will be executed on demand

in an {SingleSchemaDSL} object's context.
# File lib/restspec/schema/dsl.rb, line 52
def mixin(name, &definition)
  mixins[name] = definition
end
schema(name, options = {}, &definition) click to toggle source

Generates a schema and sends the schema to an {SingleSchemaDSL} instance for further definitions.

@example

schema :book, root: true do
  puts self.class # SingleSchemaDSL
  puts self.schema.class # Schema
end

@param name {Symbol} the schema's name @param options {Hash} a set of options for {Schema#initialize} @param definition A block that will be executed inside the context

of a {SingleSchemaDSL} object.
# File lib/restspec/schema/dsl.rb, line 23
def schema(name, options = {}, &definition)
  dsl = SingleSchemaDSL.new(name, options, mixins)
  dsl.instance_eval(&definition)
  Restspec::SchemaStore.store(dsl.schema)
end