5.13
No commit activity in last 3 years
No release in over 3 years
DSL for nested generic schemas with inheritance and refining.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Declarative

DSL for nested schemas.

Gem Version

Overview

Declarative allows declaring nested schemas.

Installation

Add this line to your application's Gemfile:

gem 'declarative'

Declarative::Schema

Include this into a class or module to allow defining nested schemas using the popular ::property DSL.

Normally, an abstract base class will define essential configuration.

class Model
 extend Declarative::Schema

  def self.default_nested_class
    Model
  end
end

Concrete schema-users simply derive from the base class.

class Song < Model
  property :id

  property :artist do
    property :id
    property :name
  end
end

This won't do anything but populate the ::definitions graph.

Song.definitions #=>

<Definition "id">
<Definition "artist" nested=..>
  <Definition "id">
  <Definition "name">

The nested schema will be a subclass of Model.

Song.definitions.get(:artist) #=> <Anonymous:Model definitions=..>

Overriding Nested Building

When declaring nested schemas, per default, Declarative will use its own Schema::NestedBuilder to create the nested schema composer.

Override ::nested_builder to define your own way of doing that.

class Model
  extend Declarative::Schema

  def self.default_nested_class
    Model
  end

  def self.nested_builder
    ->(options) do
      Class.new(Model) do
        class_eval &options[:_block] # executes `property :name` etc. on nested, fresh class.
      end
    end
  end
end

Features

You can automatically include modules into all nested schemas by using ::feature.

class Model
  extend Declarative::Schema
  feature Bla

Defaults

class Model
  extend Declarative::Schema
  defaults visible: true

Copyright