1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# Declarative
_DSL for nested schemas._
[](http://badge.fury.io/rb/declarative)
# Overview
Declarative allows _declaring_ nested schemas.
## Installation
Add this line to your application's Gemfile:
```ruby
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.
```ruby
class Model
extend Declarative::Schema
def self.default_nested_class
Model
end
end
```
Concrete schema-users simply derive from the base class.
```ruby
class Song < Model
property :id
property :artist do
property :id
property :name
end
end
```
This won't do anything but populate the `::definitions` graph.
```ruby
Song.definitions #=>
<Definition "id">
<Definition "artist" nested=..>
<Definition "id">
<Definition "name">
```
The nested schema will be a subclass of `Model`.
```ruby
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.
```ruby
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`.
```ruby
class Model
extend Declarative::Schema
feature Bla
```
## Defaults
```ruby
class Model
extend Declarative::Schema
defaults visible: true
```
## Copyright
* Copyright (c) 2015 Nick Sutterer <apotonick@gmail.com>
|