declarative 0.0.10 → 0.0.20

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6e5c86877c99e99dfa9ce880df46435218fcc7a0
4
- data.tar.gz: e4b808d37b26399bade9158a92040fb882a3c840
2
+ SHA256:
3
+ metadata.gz: fc3dccaf0b8fa27ffd73acd1146949c5a79c1f950b72979038f20b34e751a516
4
+ data.tar.gz: f1744ace25a4cc4bb42ce3bb5d45a8ca09a993d8f5f7643e52bc747049674929
5
5
  SHA512:
6
- metadata.gz: 92ec08dc8dcbe1939a657112edc880b977f16e5b786722a0af007f895399d28d9370415f3db81c2c71e5f270f5be6eeaa10d6b0bcf654f64683113ebe76d639a
7
- data.tar.gz: e56d957d4b9b29695b861a7e3852cd66aac31f8f3fe0a01dfa343e054a38f6b4c98aa39dcce5f6e61157ac9a055a6d7cee1730ba8f804d003283db611ef280c1
6
+ metadata.gz: bac6ff5ad4c34454f3f2212f478299c7c1c22ff94d333149ff73ba93e24792d4b3c33f7cd78f7359fbc3a008c67412abb486b67fd22b22cd1b097f03f2816983
7
+ data.tar.gz: 8544c6eaa0ff5ebe1cefa67e5d003b2c44ad7ba0a353c1c169ef971b8a1dc496f7faaa0db75be987b92d45151a8c0db05babdf0f9d9718052cbb407e5ca8e30c
@@ -1,8 +1,12 @@
1
1
  language: ruby
2
+ before_install: gem install bundler
3
+ cache: bundler
2
4
  rvm:
3
- - 2.3.1
4
- - 1.9.3
5
- gemfile:
6
- - Gemfile
7
- before_install:
8
- - gem install bundler
5
+ - ruby-head
6
+ - 2.7
7
+ - 2.6
8
+ - 2.5
9
+ - 2.4
10
+ jobs:
11
+ allow_failures:
12
+ - rvm: ruby-head
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 Nick Sutterer
1
+ Copyright (c) 2015-2020 Nick Sutterer
2
2
 
3
3
  MIT License
4
4
 
@@ -12,12 +12,14 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = "https://github.com/apotonick/declarative"
13
13
  spec.license = "MIT"
14
14
 
15
- spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test)/})
17
+ end
18
+ spec.test_files = spec.files.grep(%r{^(test)/})
18
19
  spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = '>= 2.3.0'
19
21
 
20
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rake"
21
23
  spec.add_development_dependency "minitest"
22
24
  spec.add_development_dependency "minitest-line"
23
25
  end
@@ -1,12 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Declarative
2
- class DeepDup
4
+ module DeepDup
3
5
  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
6
+ case args
7
+ when Array
8
+ Array[*dup_items(args)]
9
+ when ::Hash
10
+ ::Hash[dup_items(args)]
11
+ else
12
+ args
13
+
14
+ end
7
15
  end
8
16
 
9
- private
10
17
  def self.dup_items(arr)
11
18
  arr.to_a.collect { |v| call(v) }
12
19
  end
@@ -22,7 +22,7 @@ module Declarative
22
22
  evaluated_options = @dynamic_options.(name, given_options)
23
23
 
24
24
  options = Variables.merge( @static_options, handle_array_and_deprecate(evaluated_options) )
25
- options = Variables.merge( options, handle_array_and_deprecate(given_options) ) # FIXME: given_options is not tested!
25
+ Variables.merge( options, handle_array_and_deprecate(given_options) ) # FIXME: given_options is not tested!
26
26
  end
27
27
 
28
28
  def handle_array_and_deprecate(variables)
@@ -1,7 +1,7 @@
1
1
  module Declarative
2
2
  class Definitions < ::Hash
3
3
  class Definition
4
- def initialize(name, options={}, &block)
4
+ def initialize(name, options={})
5
5
  @options = options.dup
6
6
  @options[:name] = name.to_s
7
7
  end
@@ -40,7 +40,7 @@ module Declarative
40
40
  options = options[:_defaults].(name, options) if options[:_defaults] # FIXME: pipeline?
41
41
  base = options[:_base]
42
42
 
43
- if options.delete(:inherit) and parent_property = get(name)
43
+ if options.delete(:inherit) and (parent_property = get(name))
44
44
  base = parent_property[:nested]
45
45
  options = parent_property.merge(options) # TODO: Definition#merge
46
46
  end
@@ -13,13 +13,6 @@ module Declarative
13
13
  each { |cfg| call!(inheritor, cfg, &block) }
14
14
  end
15
15
 
16
- private
17
- def call!(inheritor, cfg)
18
- yield cfg if block_given? # allow messing around with recorded arguments.
19
-
20
- inheritor.send(cfg[:method], *cfg[:args], &cfg[:block])
21
- end
22
-
23
16
  module DSL
24
17
  def heritage
25
18
  @heritage ||= Heritage.new
@@ -41,5 +34,12 @@ module Declarative
41
34
  heritage.(mod)
42
35
  end
43
36
  end
37
+
38
+ private
39
+ def call!(inheritor, cfg)
40
+ yield cfg if block_given? # allow messing around with recorded arguments.
41
+
42
+ inheritor.send(cfg[:method], *cfg[:args], &cfg[:block])
43
+ end
44
44
  end
45
45
  end
@@ -45,9 +45,10 @@ module Declarative
45
45
 
46
46
  private
47
47
  def build_definition(name, options={}, &block)
48
- default_options = {}
49
- default_options[:_base] = default_nested_class
50
- default_options[:_defaults] = _defaults
48
+ default_options = {
49
+ _base: default_nested_class,
50
+ _defaults: _defaults
51
+ }
51
52
  default_options[:_nested_builder] = nested_builder if block
52
53
 
53
54
  # options = options.merge( Defaults.wrap_arrays(options) )
@@ -1,22 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Declarative
2
4
  def self.Inspect(obj)
3
5
  string = obj.inspect
4
6
 
5
7
  if obj.is_a?(Proc)
6
- elements = string.split("/")
8
+ elements = string.split('/')
7
9
  string = "#{elements.first}#{elements.last}"
8
10
  end
9
- string.gsub(/0x\w+/, "")
11
+ string.gsub(/0x\w+/, '')
10
12
  end
11
13
 
12
14
  module Inspect
13
15
  def inspect
14
16
  string = super
15
17
  if is_a?(Proc)
16
- elements = string.split("/")
18
+ elements = string.split('/')
17
19
  string = "#{elements.first}#{elements.last}"
18
20
  end
19
- string.gsub(/0x\w+/, "")
21
+ string.gsub(/0x\w+/, '')
20
22
  end
21
23
 
22
24
  module Schema
@@ -29,15 +31,15 @@ module Declarative
29
31
 
30
32
  module Definitions::Inspect
31
33
  def inspect
32
- each { |dfn|
34
+ each do |dfn|
33
35
  dfn.extend(Declarative::Inspect)
34
36
 
35
- if dfn[:nested] && dfn[:nested].is_a?(Declarative::Schema::DSL)
37
+ if dfn[:nested]&.is_a?(Declarative::Schema::DSL)
36
38
  dfn[:nested].extend(Declarative::Inspect::Schema)
37
39
  else
38
- dfn[:nested].extend(Declarative::Definitions::Inspect) if dfn[:nested]
40
+ dfn[:nested]&.extend(Declarative::Definitions::Inspect)
39
41
  end
40
- }
42
+ end
41
43
  super
42
44
  end
43
45
 
@@ -1,3 +1,3 @@
1
1
  module Declarative
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.20"
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declarative
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-11 00:00:00.000000000 Z
11
+ date: 2020-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -76,12 +76,6 @@ files:
76
76
  - lib/declarative/testing.rb
77
77
  - lib/declarative/variables.rb
78
78
  - lib/declarative/version.rb
79
- - test/defaults_test.rb
80
- - test/definitions_test.rb
81
- - test/heritage_test.rb
82
- - test/schema_test.rb
83
- - test/test_helper.rb
84
- - test/variables_test.rb
85
79
  homepage: https://github.com/apotonick/declarative
86
80
  licenses:
87
81
  - MIT
@@ -94,22 +88,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
88
  requirements:
95
89
  - - ">="
96
90
  - !ruby/object:Gem::Version
97
- version: '0'
91
+ version: 2.3.0
98
92
  required_rubygems_version: !ruby/object:Gem::Requirement
99
93
  requirements:
100
94
  - - ">="
101
95
  - !ruby/object:Gem::Version
102
96
  version: '0'
103
97
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.6.8
98
+ rubygems_version: 3.0.3
106
99
  signing_key:
107
100
  specification_version: 4
108
101
  summary: DSL for nested schemas.
109
- test_files:
110
- - test/defaults_test.rb
111
- - test/definitions_test.rb
112
- - test/heritage_test.rb
113
- - test/schema_test.rb
114
- - test/test_helper.rb
115
- - test/variables_test.rb
102
+ test_files: []
@@ -1,99 +0,0 @@
1
- require "test_helper"
2
-
3
- class DefaultsOptionsTest < Minitest::Spec
4
- let (:song) { Struct.new(:title, :author_name, :song_volume, :description).new("Revolution", "Some author", 20, nil) }
5
- let (:schema) { Declarative::Definitions.new(Declarative::Definitions::Definition).extend Declarative::Definitions::Inspect }
6
- let (:defaults) { Declarative::Defaults.new }
7
-
8
- describe "hash options combined with dynamic options" do
9
- it do
10
- defaults.merge!(render_nil: true) do |name|
11
- { as: name.to_s.upcase }
12
- end
13
-
14
- schema.add :title, _defaults: defaults
15
- schema.add :author_name
16
- schema.add :description, _defaults: defaults
17
-
18
- schema.inspect.must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"TITLE", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"DESCRIPTION", :name=>"description"}>}'
19
- end
20
- end
21
-
22
- describe "with only dynamic property options" do
23
- it do
24
- defaults.merge!({}) do |name|
25
- { as: name.to_s.upcase }
26
- end
27
-
28
- schema.add :title, _defaults: defaults
29
- schema.add :author_name
30
- schema.add :description, _defaults: defaults
31
-
32
- schema.inspect.must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:as=>"TITLE", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:as=>"DESCRIPTION", :name=>"description"}>}'
33
- end
34
- end
35
-
36
- describe "with only hashes" do
37
- it do
38
- defaults.merge!(render_nil: true)
39
-
40
- schema.add :title, _defaults: defaults
41
- schema.add :author_name
42
- schema.add :description, _defaults: defaults
43
-
44
- schema.inspect.must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :name=>"description"}>}'
45
- end
46
- end
47
-
48
- describe "#add options win" do
49
- it do
50
- defaults.merge!(render_nil: true) do |name|
51
- { as: name.to_s.upcase }
52
- end
53
-
54
- schema.add :title, as: "Title", _defaults: defaults
55
- schema.add :author_name
56
- schema.add :description, _defaults: defaults
57
-
58
- schema.inspect.must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"Title", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"DESCRIPTION", :name=>"description"}>}'
59
- end
60
- end
61
-
62
-
63
- describe "multiple Defaults#merge!" do
64
- it "merges arrays automatically" do
65
- defaults.merge!(a: 1, b: 2)
66
- defaults.merge!( b: 3, _features: Declarative::Variables::Append(["A"]) )
67
- defaults.merge!( _features: Declarative::Variables::Append(["B", "C"]) )
68
- defaults.(nil, {}).inspect.must_equal "{:a=>1, :b=>3, :_features=>[\"A\", \"B\", \"C\"]}"
69
- end
70
-
71
- it "what" do
72
- defaults.merge!( _features: Declarative::Variables::Append(["A"]) ) do |name, options|
73
- { _features: Declarative::Variables::Append( ["B", "D"] ) }
74
- end
75
-
76
- defaults.(nil, {}).inspect.must_equal "{:_features=>[\"A\", \"B\", \"D\"]}"
77
- end
78
- end
79
-
80
- describe "deprecation" do
81
- require 'stringio'
82
- before do
83
- @old_stderr = $stderr
84
- $stderr = StringIO.new
85
- end
86
-
87
- after { $stderr = @old_stderr }
88
-
89
- it "prints deprecation twice" do
90
- defaults.merge!( _features: ["A"] ) do |name, options|
91
- { _features: ["B", "D"] }
92
- end
93
-
94
- defaults.(nil, {}).inspect.must_equal "{:_features=>[\"A\", \"B\", \"D\"]}"
95
-
96
- $stderr.string.must_equal %{[Declarative] Defaults#merge! and #call still accept arrays and automatically prepend those. This is now deprecated, you should replace `ary` with `Declarative::Variables::Append(ary)`.\n}*2
97
- end
98
- end
99
- end
@@ -1,95 +0,0 @@
1
- require "test_helper"
2
-
3
- class DefinitionsTest < Minitest::Spec
4
- NestedBuilder = ->(options) {
5
- base = options[:_base] || Declarative::Definitions.new(Declarative::Definitions::Definition)
6
- base.instance_exec(&options[:_block])
7
- base
8
- }
9
-
10
- let (:schema) { Declarative::Definitions.new(Declarative::Definitions::Definition).extend(Declarative::Definitions::Inspect) }
11
-
12
- it "what" do
13
- # #add works with name
14
- schema.add :id
15
- # get works with symbol
16
- schema.get(:id).inspect.must_equal '#<Declarative::Definitions::Definition: @options={:name=>"id"}>'
17
- # get works with string
18
- schema.get("id").inspect.must_equal '#<Declarative::Definitions::Definition: @options={:name=>"id"}>'
19
-
20
- # #add with name and options
21
- schema.add(:id, unique: true)
22
- schema.get(:id).inspect.must_equal '#<Declarative::Definitions::Definition: @options={:unique=>true, :name=>"id"}>'
23
- end
24
-
25
- it "overwrites old when called twice" do
26
- schema.add :id
27
- schema.add :id, cool: true
28
- schema.inspect.must_equal '{"id"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :name=>"id"}>}'
29
- end
30
-
31
- it "#add with block" do
32
- schema.add :artist, _nested_builder: NestedBuilder do
33
- add :name
34
- add :band, _nested_builder: NestedBuilder do
35
- add :location
36
- end
37
- end
38
-
39
- schema.inspect.must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>, "band"=>#<Declarative::Definitions::Definition: @options={:nested=>{"location"=>#<Declarative::Definitions::Definition: @options={:name=>"location"}>}, :name=>"band"}>}, :name=>"artist"}>}'
40
- end
41
-
42
- it "#add with :nested instead of block" do
43
- nested_schema = Declarative::Definitions.new(Declarative::Definitions::Definition)
44
- nested_schema.extend(Declarative::Definitions::Inspect)
45
-
46
- nested_schema.add :name
47
-
48
- schema.add :artist, nested: nested_schema
49
-
50
- schema.inspect.must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>}, :name=>"artist"}>}'
51
- end
52
-
53
-
54
- it "#add with inherit: true and block" do
55
- schema.add :artist, cool: true, _nested_builder: NestedBuilder do
56
- add :name
57
- add :band, crazy: nil, _nested_builder: NestedBuilder do
58
- add :location
59
- end
60
- end
61
-
62
- schema.add :id, unique: true, value: 1
63
-
64
- schema.add :artist, uncool: false, _nested_builder: NestedBuilder, inherit: true do
65
- add :band, normal: false, _nested_builder: NestedBuilder, inherit: true do
66
- add :genre
67
- end
68
- end
69
-
70
- schema.add :id, unique: false, inherit: true
71
-
72
- 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"}>}'
73
- end
74
-
75
- it "#add with nested options followed by inherit: true" do
76
- schema.add :id, deserializer: options = { render: false }
77
- schema.add :id, inherit: true
78
-
79
- schema.get(:id)[:deserializer][:parse] = true
80
-
81
- options.must_equal(render: false)
82
- end
83
- end
84
-
85
-
86
- class DefinitionTest < Minitest::Spec
87
- let (:definition) { Declarative::Definitions::Definition.new(:name) }
88
-
89
- it "#merge does return deep copy" do
90
- options = { render: false }
91
- merged = definition.merge(options)
92
- definition.merge!(render: true)
93
- merged.must_equal(:name=>"name", render: false)
94
- end
95
- end
@@ -1,49 +0,0 @@
1
- require "test_helper"
2
-
3
- class HeritageTest < Minitest::Spec
4
- P = Proc.new{}.extend(Declarative::Inspect)
5
- # #record
6
- module RepresenterA
7
- extend Declarative::Heritage::DSL
8
-
9
- # one arg.
10
- heritage.record(:representation_wrap=, true)
11
- # 2 args.
12
- heritage.record(:property, :name, enable: true)
13
- # 3 args.
14
- heritage.record(:property, :id, {}, &P)
15
- end
16
-
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
34
-
35
- describe "#call with block" do
36
- let (:heritage) { Declarative::Heritage.new.record(:property, :id, {}) }
37
-
38
- class CallWithBlock
39
- def self.property(name, options)
40
- @args = [name, options]
41
- end
42
- end
43
-
44
- it do
45
- heritage.(CallWithBlock) { |cfg| cfg[:args].last.merge!(_inherited: true) }
46
- CallWithBlock.instance_variable_get(:@args).must_equal [:id, {:_inherited=>true}]
47
- end
48
- end
49
- end
@@ -1,115 +0,0 @@
1
- require "test_helper"
2
-
3
- class SchemaTest < Minitest::Spec
4
- class Decorator
5
- extend Declarative::Schema
6
-
7
- def self.default_nested_class
8
- Decorator
9
- end
10
- end
11
-
12
- module AddLinks
13
- def self.included(includer)
14
- super
15
- includer.property(:links)
16
- end
17
- end
18
-
19
- class Concrete < Decorator
20
- defaults render_nil: true do |name|
21
- { as: name.to_s.upcase }
22
- end
23
- feature AddLinks
24
-
25
- property :artist, cool: true do
26
- property :name
27
- property :band, crazy: nil do
28
- property :location
29
- end
30
- end
31
-
32
- property :id, unique: true, value: 1
33
- end
34
-
35
-
36
- it do
37
- Concrete.extend(Declarative::Inspect::Schema)
38
- Concrete.inspect
39
- Concrete.inspect.gsub(/\s/, "").must_equal 'Schema:{
40
- "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
41
- "artist"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ARTIST",:cool=>true,:nested=>Schema:{
42
- "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
43
- "name"=>#<Declarative::Definitions::Definition:@options={:name=>"name"}>,
44
- "band"=>#<Declarative::Definitions::Definition:@options={:crazy=>nil,:nested=>Schema:{
45
- "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
46
- "location"=>#<Declarative::Definitions::Definition:@options={:name=>"location"}>},:name=>"band"}>},:name=>"artist"}>,
47
- "id"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ID",:unique=>true,:value=>1,:name=>"id"}>}'.
48
- gsub("\n", "").gsub(/\s/, "")
49
- end
50
-
51
- class InheritingConcrete < Concrete
52
- property :uuid
53
- end
54
-
55
-
56
- it do
57
- InheritingConcrete.extend(Declarative::Inspect::Schema)
58
- InheritingConcrete.inspect
59
- InheritingConcrete.inspect.gsub(/\s/, "").must_equal 'Schema:{
60
- "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
61
- "artist"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ARTIST",:cool=>true,:nested=>Schema:{
62
- "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
63
- "name"=>#<Declarative::Definitions::Definition:@options={:name=>"name"}>,
64
- "band"=>#<Declarative::Definitions::Definition:@options={:crazy=>nil,:nested=>Schema:{
65
- "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
66
- "location"=>#<Declarative::Definitions::Definition:@options={:name=>"location"}>},:name=>"band"}>},:name=>"artist"}>,
67
- "id"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ID",:unique=>true,:value=>1,:name=>"id"}>,
68
- "uuid"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"UUID",:name=>"uuid"}>}
69
- '.
70
- gsub("\n", "").gsub(/\s/, "")
71
- end
72
-
73
-
74
- describe "::property still allows passing internal options" do
75
- class ConcreteWithOptions < Decorator
76
- defaults cool: true
77
-
78
- # you can pass your own _nested_builder and it will still receive correct,
79
- # defaultized options.
80
- property :artist, _nested_builder: ->(options) { OpenStruct.new(cool: options[:cool]) }
81
- end
82
-
83
- it do
84
- ConcreteWithOptions.extend(Declarative::Inspect::Schema).inspect.must_equal 'Schema: {"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>#<OpenStruct cool=true>, :name=>"artist"}>}'
85
- end
86
- end
87
-
88
- describe "multiple ::defaults" do
89
- class Twin < Decorator
90
- module A; end
91
- module B; end
92
- module D; end
93
-
94
- defaults a: "a", _features: [A] do |name|
95
- { first: 1, _features: [D] }
96
- end
97
-
98
- # DISCUSS: currently, we only allow one dynamic block.
99
- defaults b: "b", _features: [B]# do |name, options|
100
- # {}
101
- #end
102
-
103
-
104
- property :id do end
105
- end
106
-
107
- it do
108
- Twin.extend(Declarative::Inspect::Schema).inspect.must_equal 'Schema: {"id"=>#<Declarative::Definitions::Definition: @options={:a=>"a", :b=>"b", :first=>1, :nested=>Schema: {}, :name=>"id"}>}'
109
- # :_features get merged.
110
- Twin.definitions.get(:id)[:nested].is_a? Twin::A
111
- Twin.definitions.get(:id)[:nested].is_a? Twin::B
112
- Twin.definitions.get(:id)[:nested].is_a? Twin::D
113
- end
114
- end
115
- end
@@ -1,4 +0,0 @@
1
- require "minitest/autorun"
2
- require "declarative"
3
- require "declarative/testing"
4
- require "ostruct"
@@ -1,65 +0,0 @@
1
- require "test_helper"
2
-
3
- class DSLOptionsTest < Minitest::Spec
4
- let(:defaults) { { id: 1, connections: { first: 1, second: 2 }, list: [3] } }
5
-
6
- Variables = Declarative::Variables
7
-
8
- after do
9
- Declarative::Inspect(defaults).must_equal %{{:id=>1, :connections=>{:first=>1, :second=>2}, :list=>[3]}}
10
- end
11
-
12
- #- Merge
13
- it "merges Merge over original" do
14
- options = Variables.merge(
15
- defaults,
16
- connections: Variables::Merge( second: 3, third: 4 )
17
- )
18
-
19
- options.must_equal( { id: 1, connections: { first: 1, second: 3, third: 4 }, :list=>[3] } )
20
- end
21
-
22
- it "accepts Procs" do
23
- options = Variables.merge(
24
- defaults,
25
- connections: proc = ->(*) { raise }
26
- )
27
-
28
- options.must_equal( { id: 1, connections: proc, :list=>[3] } )
29
- end
30
-
31
- it "overrides original without Merge" do
32
- options = Variables.merge(
33
- defaults, connections: { second: 3, third: 4 } )
34
-
35
- options.must_equal( { id: 1, connections: { second: 3, third: 4 }, :list=>[3] } )
36
- end
37
-
38
- it "creates new hash if original not existent" do
39
- options = Variables.merge(
40
- defaults,
41
- bla: Variables::Merge( second: 3, third: 4 )
42
- )
43
-
44
- options.must_equal( {:id=>1, :connections=>{:first=>1, :second=>2}, :list=>[3], :bla=>{:second=>3, :third=>4}} )
45
- end
46
-
47
- #- Append
48
- it "appends to Array" do
49
- options = Variables.merge(
50
- defaults,
51
- list: Variables::Append( [3, 4, 5] )
52
- )
53
-
54
- options.must_equal( { id: 1, connections: { first: 1, second: 2 }, :list=>[3, 3, 4, 5] } )
55
- end
56
-
57
- it "creates new array if original not existent" do
58
- options = Variables.merge(
59
- defaults,
60
- another_list: Variables::Append( [3, 4, 5] )
61
- )
62
-
63
- options.must_equal( { id: 1, connections: { first: 1, second: 2 }, :list=>[3], :another_list=>[3, 4, 5] } )
64
- end
65
- end