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


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

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

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

#schema(name, &definition) ⇒ Object

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

Examples:

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

22
23
24
25
26
# File 'lib/restspec/schema/dsl.rb', line 22

def schema(name, &definition)
  dsl = SingleSchemaDSL.new(name, mixins)
  dsl.instance_eval(&definition)
  Restspec::SchemaStore.store(dsl.schema)
end