declarative 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f743b248235864a196f81afcba519e36856edad
4
- data.tar.gz: 1c53f46fbefe9d256f7f06d39327413b01f8c2d6
3
+ metadata.gz: 777087a37a008f6db451fe0d998180119cef3a43
4
+ data.tar.gz: 9dc8ca151863c9c438c9fd45f3784472c0270e7e
5
5
  SHA512:
6
- metadata.gz: 69271f1ac66235fd31c267665a9da238211f7d97518a0b3dc6945f5a2d845048456386d5bf7e4d969d58ad37c26ba7ea7e31eeccbaad361917717179f5f6a81a
7
- data.tar.gz: eb72aec864a51461c98774eedfa138aefd1591b8b7b7655d6e6d342465f66c37386c5f00f3c085f15bd94f7178d76a31c98f3a1e8da5e17a67f0bd7aa07372e0
6
+ metadata.gz: 3ad76a30355469122e73ef454c393f6f1827648687b5a507f7e066100be68c180562133cae14b37160313e92cecb8838d437b9da3dcac38d037f5b86d9645819
7
+ data.tar.gz: a1a0cd8836cdd1eb93da1dd0ea5d782cc39fc82f650625f26ec550e9430e7ccc8438ef5a41fefef11add1336c320fd889e97367ee2c302ea56ada71732cb56e5
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.0.4
2
+
3
+ * Restructured modules, there's always a public `DSL` module now, etc.
4
+
1
5
  # 0.0.3
2
6
 
3
7
  * Internals, only.
data/README.md CHANGED
@@ -70,7 +70,7 @@ Override `::nested_builder` to define your own way of doing that.
70
70
 
71
71
  ```ruby
72
72
  class Model
73
- include Declarative::Schema
73
+ extend Declarative::Schema
74
74
 
75
75
  def self.default_nested_class
76
76
  Model
@@ -92,7 +92,7 @@ You can automatically include modules into all nested schemas by using `::featur
92
92
 
93
93
  ```ruby
94
94
  class Model
95
- include Declarative::Schema
95
+ extend Declarative::Schema
96
96
  feature Bla
97
97
  ```
98
98
 
@@ -100,7 +100,7 @@ class Model
100
100
 
101
101
  ```ruby
102
102
  class Model
103
- include Declarative::Schema
103
+ extend Declarative::Schema
104
104
  defaults visible: true
105
105
  ```
106
106
 
data/lib/declarative.rb CHANGED
@@ -1,31 +1,9 @@
1
1
  require "declarative/version"
2
2
  require "declarative/definitions"
3
+ require "declarative/heritage"
3
4
  require "declarative/defaults"
4
5
  require "declarative/schema"
6
+ require "declarative/deep_dup"
5
7
 
6
8
  module Declarative
7
- module DSL
8
- def heritage
9
- @heritage ||= Heritage.new
10
- end
11
- end
12
-
13
- module Inheritance
14
- def included(includer)
15
- heritage.(includer)
16
- end
17
- end
18
-
19
-
20
- class Heritage < Array
21
- def record(method, name, options=nil, &block)
22
- self << {method: method, args: [name, options ? options.dup : nil].compact, block: block} # DISCUSS: options.dup.
23
- end
24
-
25
- def call(inheritor)
26
- each do |cfg|
27
- inheritor.send(cfg[:method], *cfg[:args], &cfg[:block])
28
- end
29
- end
30
- end
31
9
  end
@@ -0,0 +1,14 @@
1
+ module Declarative
2
+ class DeepDup
3
+ def self.call(args)
4
+ return Array[*dup_items(args)] if args.is_a?(Array)
5
+ return Hash[dup_items(args)] if args.is_a?(Hash)
6
+ args
7
+ end
8
+
9
+ private
10
+ def self.dup_items(arr)
11
+ arr.to_a.collect { |v| call(v) }
12
+ end
13
+ end
14
+ end
@@ -2,15 +2,22 @@ module Declarative
2
2
  class Definitions < Hash
3
3
  class Definition
4
4
  def initialize(name, options={}, &block)
5
- @options = options.clone
5
+ @options = options.dup
6
6
  @options[:name] = name.to_s
7
7
  end
8
8
 
9
- attr_reader :options # TODO: are we gonna keep this?
10
-
11
9
  def [](name)
12
10
  @options[name]
13
11
  end
12
+
13
+ def merge!(hash) # TODO: this should return a new Definition instance.
14
+ @options.merge!(hash)
15
+ self
16
+ end
17
+
18
+ def merge(hash) # TODO: should be called #copy.
19
+ DeepDup.(@options).merge(hash)
20
+ end
14
21
  end
15
22
 
16
23
  def initialize(definition_class)
@@ -34,7 +41,7 @@ module Declarative
34
41
 
35
42
  if options.delete(:inherit) and parent_property = get(name)
36
43
  base = parent_property[:nested]
37
- options = parent_property.options.merge(options) # TODO: Definition#merge
44
+ options = parent_property.merge(options) # TODO: Definition#merge
38
45
  end
39
46
 
40
47
  if options[:_nested_builder]
@@ -0,0 +1,36 @@
1
+ module Declarative
2
+ class Heritage < Array
3
+ def record(method, *args, &block)
4
+ self << {method: method, args: DeepDup.(args), block: block} # DISCUSS: options.dup.
5
+ end
6
+
7
+ def call(inheritor)
8
+ each do |cfg|
9
+ inheritor.send(cfg[:method], *cfg[:args], &cfg[:block])
10
+ end
11
+ end
12
+
13
+
14
+ module DSL
15
+ def heritage
16
+ @heritage ||= Heritage.new
17
+ end
18
+ end
19
+
20
+ # To be extended into classes using Heritage. Inherits the heritage.
21
+ module Inherited
22
+ def inherited(subclass)
23
+ super
24
+ heritage.(subclass)
25
+ end
26
+ end
27
+
28
+ # To be included into modules using Heritage. When included, inherits the heritage.
29
+ module Included
30
+ def included(mod)
31
+ super
32
+ heritage.(mod)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,6 +10,13 @@ module Declarative
10
10
  #
11
11
  # Requirements to includer: ::default_nested_class, override building with ::nested_builder.
12
12
  module Schema
13
+ def self.extended(extender)
14
+ extender.extend DSL # ::property
15
+ extender.extend Feature # ::feature
16
+ extender.extend Heritage::DSL # ::heritage
17
+ extender.extend Heritage::Inherited # ::included
18
+ end
19
+
13
20
  module DSL
14
21
  def property(name, options={}, &block)
15
22
  heritage.record(:property, name, options, &block)
@@ -53,17 +60,6 @@ module Declarative
53
60
  end
54
61
  end
55
62
 
56
- module Heritage
57
- def heritage
58
- @heritage ||= ::Declarative::Heritage.new
59
- end
60
-
61
- def inherited(subclass) # DISCUSS: this could be in Decorator? but then we couldn't do B < A(include X) for non-decorators, right?
62
- super
63
- heritage.(subclass)
64
- end
65
- end
66
-
67
63
  module Feature
68
64
  # features are registered as defaults using _features, which in turn get translated to
69
65
  # Class.new... { feature mod } which makes it recursive in nested schemas.
@@ -1,3 +1,3 @@
1
1
  module Declarative
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -77,6 +77,26 @@ class DefinitionsTest < Minitest::Spec
77
77
 
78
78
 
79
79
  schema.inspect.must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>, "band"=>#<Declarative::Definitions::Definition: @options={:crazy=>nil, :nested=>{"location"=>#<Declarative::Definitions::Definition: @options={:name=>"location"}>, "genre"=>#<Declarative::Definitions::Definition: @options={:name=>"genre"}>}, :name=>"band", :normal=>false}>}, :name=>"artist", :uncool=>false}>, "id"=>#<Declarative::Definitions::Definition: @options={:unique=>false, :value=>1, :name=>"id"}>}'
80
+ end
81
+
82
+ it "#add with nested options followed by inherit: true" do
83
+ schema.add :id, deserializer: options = { render: false }
84
+ schema.add :id, inherit: true
85
+
86
+ schema.get(:id)[:deserializer][:parse] = true
87
+
88
+ options.must_equal(render: false)
89
+ end
90
+ end
91
+
92
+
93
+ class DefinitionTest < Minitest::Spec
94
+ let (:definition) { Declarative::Definitions::Definition.new(:name) }
80
95
 
96
+ it "#merge does return deep copy" do
97
+ options = { render: false }
98
+ merged = definition.merge(options)
99
+ definition.merge!(render: true)
100
+ merged.must_equal(:name=>"name", render: false)
81
101
  end
82
102
  end
@@ -1,17 +1,34 @@
1
1
  require "test_helper"
2
2
 
3
3
  class HeritageTest < Minitest::Spec
4
+ P = Proc.new{}.extend(Declarative::Inspect)
4
5
  # #record
5
6
  module RepresenterA
6
- extend Declarative::DSL
7
+ extend Declarative::Heritage::DSL
7
8
 
8
9
  # one arg.
9
10
  heritage.record(:representation_wrap=, true)
10
11
  # 2 args.
11
12
  heritage.record(:property, :name, enable: true)
12
13
  # 3 args.
13
- heritage.record(:property, :id, {}, &Proc.new{}.extend(Declarative::Inspect))
14
+ heritage.record(:property, :id, {}, &P)
14
15
  end
15
16
 
16
- it { RepresenterA.heritage.inspect.must_equal "[{:method=>:representation_wrap=, :args=>[true], :block=>nil}, {:method=>:property, :args=>[:name, {:enable=>true}], :block=>nil}, {:method=>:property, :args=>[:id, {}], :block=>#<Proc:@heritage_test.rb:13>}]" }
17
+ it { RepresenterA.heritage.inspect.must_equal "[{:method=>:representation_wrap=, :args=>[true], :block=>nil}, {:method=>:property, :args=>[:name, {:enable=>true}], :block=>nil}, {:method=>:property, :args=>[:id, {}], :block=>#<Proc:@heritage_test.rb:4>}]" }
18
+
19
+
20
+ describe "dup of arguments" do
21
+ module B
22
+ extend Declarative::Heritage::DSL
23
+
24
+ options = {render: true, nested: {render: false}}
25
+
26
+ heritage.record(:property, :name, options, &P)
27
+
28
+ options[:parse] = true
29
+ options[:nested][:parse] = false
30
+ end
31
+
32
+ it { B.heritage.inspect.must_equal "[{:method=>:property, :args=>[:name, {:render=>true, :nested=>{:render=>false}}], :block=>#<Proc:@heritage_test.rb:4>}]" }
33
+ end
17
34
  end
data/test/schema_test.rb CHANGED
@@ -2,9 +2,7 @@ require "test_helper"
2
2
 
3
3
  class SchemaTest < Minitest::Spec
4
4
  class Decorator
5
- extend Declarative::Schema::DSL
6
- extend Declarative::Schema::Feature # TODO: make automatic
7
- extend Declarative::Schema::Heritage # TODO: make automatic
5
+ extend Declarative::Schema
8
6
 
9
7
  def self.default_nested_class
10
8
  Decorator
@@ -38,8 +36,6 @@ class SchemaTest < Minitest::Spec
38
36
  it do
39
37
  Concrete.extend(Declarative::Inspect::Schema)
40
38
  Concrete.inspect
41
- pp Concrete.definitions.get(:artist).options
42
- # pp Concrete.definitions.get(:artist)[:nested].definitions.get(:band)[:nested].definitions
43
39
  Concrete.inspect.gsub(/\s/, "").must_equal 'Schema:{
44
40
  "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
45
41
  "artist"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ARTIST",:cool=>true,:nested=>Schema:{
@@ -60,7 +56,6 @@ class SchemaTest < Minitest::Spec
60
56
  it do
61
57
  InheritingConcrete.extend(Declarative::Inspect::Schema)
62
58
  InheritingConcrete.inspect
63
- pp InheritingConcrete.definitions.get(:artist).options
64
59
  # pp InheritingConcrete.definitions.get(:artist)[:nested].definitions.get(:band)[:nested].definitions
65
60
  InheritingConcrete.inspect.gsub(/\s/, "").must_equal 'Schema:{
66
61
  "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
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.3
4
+ version: 0.0.4
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-31 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uber
@@ -82,8 +82,10 @@ files:
82
82
  - Rakefile
83
83
  - declarative.gemspec
84
84
  - lib/declarative.rb
85
+ - lib/declarative/deep_dup.rb
85
86
  - lib/declarative/defaults.rb
86
87
  - lib/declarative/definitions.rb
88
+ - lib/declarative/heritage.rb
87
89
  - lib/declarative/schema.rb
88
90
  - lib/declarative/testing.rb
89
91
  - lib/declarative/version.rb