Class: Restspec::Schema::DSL

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

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



6
7
8
# File 'lib/restspec/schema/dsl.rb', line 6

def initialize
  self.mixins = {}
end

Instance Method Details

#mixin(name, &definition) ⇒ Object

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

They are useful to share attributes.

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's name

  • definition

    A block that will be executed on demand in an SingleSchemaDSL object's context.



52
53
54
# File 'lib/restspec/schema/dsl.rb', line 52

def mixin(name, &definition)
  mixins[name] = definition
end

#schema(name, options = {}, &definition) ⇒ Object

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

Examples:

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

Parameters:

  • name (Symbol)

    the schema's name

  • options (Hash) (defaults to: {})

    a set of options for Schema#initialize

  • definition

    A block that will be executed inside the context of a SingleSchemaDSL object.



23
24
25
26
27
# 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