declarative 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +7 -1
- data/README.md +87 -0
- data/lib/declarative/definitions.rb +4 -2
- data/lib/declarative/schema.rb +15 -16
- data/lib/declarative/version.rb +1 -1
- data/test/schema_test.rb +15 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f743b248235864a196f81afcba519e36856edad
|
4
|
+
data.tar.gz: 1c53f46fbefe9d256f7f06d39327413b01f8c2d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69271f1ac66235fd31c267665a9da238211f7d97518a0b3dc6945f5a2d845048456386d5bf7e4d969d58ad37c26ba7ea7e31eeccbaad361917717179f5f6a81a
|
7
|
+
data.tar.gz: eb72aec864a51461c98774eedfa138aefd1591b8b7b7655d6e6d342465f66c37386c5f00f3c085f15bd94f7178d76a31c98f3a1e8da5e17a67f0bd7aa07372e0
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -16,6 +16,93 @@ Add this line to your application's Gemfile:
|
|
16
16
|
gem 'declarative'
|
17
17
|
```
|
18
18
|
|
19
|
+
## Declarative::Schema
|
20
|
+
|
21
|
+
Include this into a class or module to allow defining nested schemas using the popular `::property` DSL.
|
22
|
+
|
23
|
+
Normally, an abstract base class will define essential configuration.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Model
|
27
|
+
include Declarative::Schema
|
28
|
+
|
29
|
+
def self.default_nested_class
|
30
|
+
Model
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Concrete schema-users simply derive from the base class.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class Song < Model
|
39
|
+
property :id
|
40
|
+
|
41
|
+
property :artist do
|
42
|
+
property :id
|
43
|
+
property :name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
This won't do anything but populate the `::definitions` graph.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Song.definitions #=>
|
52
|
+
|
53
|
+
<Definition "id">
|
54
|
+
<Definition "artist" nested=..>
|
55
|
+
<Definition "id">
|
56
|
+
<Definition "name">
|
57
|
+
```
|
58
|
+
|
59
|
+
The nested schema will be a subclass of `Model`.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
Song.definitions.get(:artist) #=> <Anonymous:Model definitions=..>
|
63
|
+
```
|
64
|
+
|
65
|
+
## Overriding Nested Building
|
66
|
+
|
67
|
+
When declaring nested schemas, per default, Declarative will use its own `Schema::NestedBuilder` to create the nested schema composer.
|
68
|
+
|
69
|
+
Override `::nested_builder` to define your own way of doing that.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class Model
|
73
|
+
include Declarative::Schema
|
74
|
+
|
75
|
+
def self.default_nested_class
|
76
|
+
Model
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.nested_builder
|
80
|
+
->(options) do
|
81
|
+
Class.new(Model) do
|
82
|
+
class_eval &options[:_block] # executes `property :name` etc. on nested, fresh class.
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## Features
|
90
|
+
|
91
|
+
You can automatically include modules into all nested schemas by using `::feature`.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class Model
|
95
|
+
include Declarative::Schema
|
96
|
+
feature Bla
|
97
|
+
```
|
98
|
+
|
99
|
+
## Defaults
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
class Model
|
103
|
+
include Declarative::Schema
|
104
|
+
defaults visible: true
|
105
|
+
```
|
19
106
|
|
20
107
|
## Copyright
|
21
108
|
|
@@ -27,6 +27,7 @@ module Declarative
|
|
27
27
|
# :_features
|
28
28
|
# :_defaults
|
29
29
|
# :_base
|
30
|
+
# :_nested_builder
|
30
31
|
def add(name, options={}, &block)
|
31
32
|
options = options[:_defaults].(name, options) if options[:_defaults] # FIXME: pipeline?
|
32
33
|
base = options[:_base]
|
@@ -36,7 +37,7 @@ module Declarative
|
|
36
37
|
options = parent_property.options.merge(options) # TODO: Definition#merge
|
37
38
|
end
|
38
39
|
|
39
|
-
if
|
40
|
+
if options[:_nested_builder]
|
40
41
|
options[:nested] = build_nested(
|
41
42
|
options.merge(
|
42
43
|
_base: base,
|
@@ -57,8 +58,9 @@ module Declarative
|
|
57
58
|
end
|
58
59
|
|
59
60
|
private
|
61
|
+
# Run builder to create nested schema (or twin, or representer, or whatever).
|
60
62
|
def build_nested(options)
|
61
|
-
|
63
|
+
options[:_nested_builder].(options)
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
data/lib/declarative/schema.rb
CHANGED
@@ -7,19 +7,19 @@ module Declarative
|
|
7
7
|
# ::feature the way we have it in Representable, Reform, and Disposable.
|
8
8
|
#
|
9
9
|
# The schema with its defnitions will be kept in ::definitions.
|
10
|
+
#
|
11
|
+
# Requirements to includer: ::default_nested_class, override building with ::nested_builder.
|
10
12
|
module Schema
|
11
13
|
module DSL
|
12
14
|
def property(name, options={}, &block)
|
13
15
|
heritage.record(:property, name, options, &block)
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
options[:_nested_builder] = nested_builder if block
|
20
|
-
options[:_defaults] = _defaults
|
17
|
+
default_options = {}
|
18
|
+
default_options[:_base] = default_nested_class
|
19
|
+
default_options[:_defaults] = _defaults
|
20
|
+
default_options[:_nested_builder] = nested_builder if block
|
21
21
|
|
22
|
-
definitions.add(name, options, &block)
|
22
|
+
definitions.add(name, default_options.merge(options), &block)
|
23
23
|
end
|
24
24
|
|
25
25
|
def defaults(options={}, &block)
|
@@ -42,7 +42,14 @@ module Declarative
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def nested_builder
|
45
|
-
NestedBuilder
|
45
|
+
NestedBuilder # default implementation.
|
46
|
+
end
|
47
|
+
|
48
|
+
NestedBuilder = ->(options) do
|
49
|
+
base = Class.new(options[:_base]) do
|
50
|
+
feature *options[:_features]
|
51
|
+
class_eval(&options[:_block])
|
52
|
+
end
|
46
53
|
end
|
47
54
|
end
|
48
55
|
|
@@ -75,13 +82,5 @@ module Declarative
|
|
75
82
|
defaults[:_features] << mod
|
76
83
|
end
|
77
84
|
end
|
78
|
-
|
79
|
-
|
80
|
-
NestedBuilder = ->(options) do
|
81
|
-
base = Class.new(options[:_base]) do
|
82
|
-
feature *options[:_features]
|
83
|
-
class_eval(&options[:_block])
|
84
|
-
end
|
85
|
-
end
|
86
85
|
end
|
87
86
|
end
|
data/lib/declarative/version.rb
CHANGED
data/test/schema_test.rb
CHANGED
@@ -75,4 +75,19 @@ class SchemaTest < Minitest::Spec
|
|
75
75
|
'.
|
76
76
|
gsub("\n", "").gsub(/\s/, "")
|
77
77
|
end
|
78
|
+
|
79
|
+
|
80
|
+
describe "::property still allows passing internal options" do
|
81
|
+
class ConcreteWithOptions < Decorator
|
82
|
+
defaults cool: true
|
83
|
+
|
84
|
+
# you can pass your own _nested_builder and it will still receive correct,
|
85
|
+
# defaultized options.
|
86
|
+
property :artist, _nested_builder: ->(options) { OpenStruct.new(cool: options[:cool]) }
|
87
|
+
end
|
88
|
+
|
89
|
+
it do
|
90
|
+
ConcreteWithOptions.extend(Declarative::Inspect::Schema).inspect.must_equal 'Schema: {"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>#<OpenStruct cool=true>, :name=>"artist"}>}'
|
91
|
+
end
|
92
|
+
end
|
78
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: declarative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uber
|