Sha256: c2b4b91bc5b51c892678c617e48166298fba24b74eaadd96aa6b45b7e3f0fe55

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

# Declarative

_DSL for nested schemas._

[![Gem Version](https://badge.fury.io/rb/declarative.svg)](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>

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/declarative-0.0.20/README.md
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/declarative-0.0.20/README.md
declarative-0.0.20 README.md
declarative-0.0.10 README.md
declarative-0.0.9 README.md