parametric 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: f5dfee33e2a3f175505899e0238390e2795e0a862469b851a3a550aef152e8be
4
- data.tar.gz: c1225c196aeb662384da471dd31b62ea8e072a90049eecb609976dfa21eec1d6
3
+ metadata.gz: ac256ff264b098720b8104c163e3f63e34b1caf4bd72bfa037451f4748b4982a
4
+ data.tar.gz: 9e4f467c327b68fdc24b9da3068f784c75df95b9e642d804eaf5d1e743ad635c
5
5
  SHA512:
6
- metadata.gz: 2645e19eda4c75f82f8620fa16cce48fad7b60a14d1749e78a3de05411b55f1daf2ae9e41149467ccd1ccecd613178972d8283ead6a8becdf0c35bae1d4a7123
7
- data.tar.gz: 84ecd165ff1355b17f5596f7f7c47a1b2965a2a7c50e6211801400f019aa8d318847ebf68ecbbf2c6c75da6cd8f3bb257707f9e69a4d9b5500ddf82c52733ff2
6
+ metadata.gz: 91b8cea04a00f942e1fd6f1fd20a70b60903e3b959c434d489ad0431154700f176f7905ebe19957d11b9741617da22dac24dc9d05b8ee6abb90914537ea268c5
7
+ data.tar.gz: b2e705e5a45f5b795f19a4c222684375109389cfd8407e1875aea8da9ab82e52a66621a7906d9045d792d9c7ff4d8bd76d5af39ef02488fc63ff7c290fc2c9ba
data/README.md CHANGED
@@ -742,6 +742,34 @@ class CreateUserForm
742
742
  end
743
743
  ```
744
744
 
745
+ ## Multiple schema definitions
746
+
747
+ Form objects can optionally define more than one schema by giving them names:
748
+
749
+ ```ruby
750
+ class UpdateUserForm
751
+ include Parametric::DSL
752
+
753
+ # a schema named :query
754
+ # for example for query parameters
755
+ schema(:query) do
756
+ field(:user_id).type(:integer).present
757
+ end
758
+
759
+ # a schema for PUT body parameters
760
+ schema(:payload) do
761
+ field(:name).present
762
+ field(:age).present
763
+ end
764
+ end
765
+ ```
766
+
767
+ Named schemas are inherited and can be extended and given options in the same way as the nameless version.
768
+
769
+ Named schemas can be retrieved by name, ie. `UpdateUserForm.schema(:query)`.
770
+
771
+ If no name given, `.schema` uses `:schema` as default schema name.
772
+
745
773
  ## Expanding fields dynamically
746
774
 
747
775
  Sometimes you don't know the exact field names but you want to allow arbitrary fields depending on a given pattern.
@@ -22,26 +22,37 @@ module Parametric
22
22
  #
23
23
  # foo.params # => {title: "A title", age: 20}
24
24
  #
25
+ DEFAULT_SCHEMA_NAME = :schema
26
+
25
27
  def self.included(base)
26
28
  base.extend(ClassMethods)
27
- base.schema = Parametric::Schema.new
29
+ base.schemas = {DEFAULT_SCHEMA_NAME => Parametric::Schema.new}
28
30
  end
29
31
 
30
32
  module ClassMethods
31
33
  def schema=(sc)
32
- @schema = sc
34
+ @schemas[DEFAULT_SCHEMA_NAME] = sc
35
+ end
36
+
37
+ def schemas=(sc)
38
+ @schemas = sc
33
39
  end
34
40
 
35
41
  def inherited(subclass)
36
- subclass.schema = @schema.merge(Parametric::Schema.new)
42
+ subclass.schemas = @schemas.each_with_object({}) do |(key, sc), hash|
43
+ hash[key] = sc.merge(Parametric::Schema.new)
44
+ end
37
45
  end
38
46
 
39
- def schema(options = {}, &block)
40
- return @schema unless options.any? || block_given?
47
+ def schema(*args, &block)
48
+ options = args.last.is_a?(Hash) ? args.last : {}
49
+ key = args.first.is_a?(Symbol) ? args.first : DEFAULT_SCHEMA_NAME
50
+ current_schema = @schemas.fetch(key) { Parametric::Schema.new }
51
+ return current_schema unless options.any? || block_given?
41
52
 
42
53
  new_schema = Parametric::Schema.new(options, &block)
43
- @schema = @schema.merge(new_schema)
44
- after_define_schema(@schema)
54
+ @schemas[key] = current_schema ? current_schema.merge(new_schema) : new_schema
55
+ after_define_schema(@schemas[key])
45
56
  end
46
57
 
47
58
  def after_define_schema(sc)
@@ -1,3 +1,3 @@
1
1
  module Parametric
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/spec/dsl_spec.rb CHANGED
@@ -5,6 +5,10 @@ describe "classes including DSL module" do
5
5
  class Parent
6
6
  include Parametric::DSL
7
7
 
8
+ schema :extras, search_type: :string do |opts|
9
+ field(:search).policy(opts[:search_type])
10
+ end
11
+
8
12
  schema(age_type: :integer) do |opts|
9
13
  field(:title).policy(:string)
10
14
  field(:age).policy(opts[:age_type])
@@ -12,12 +16,18 @@ describe "classes including DSL module" do
12
16
  end
13
17
 
14
18
  class Child < Parent
19
+ schema :extras do
20
+ field(:query).type(:string)
21
+ end
22
+
15
23
  schema(age_type: :string) do
16
24
  field(:description).policy(:string)
17
25
  end
18
26
  end
19
27
 
20
28
  class GrandChild < Child
29
+ schema :extras, search_type: :integer
30
+
21
31
  schema(age_type: :integer)
22
32
  end
23
33
 
@@ -42,6 +52,16 @@ describe "classes including DSL module" do
42
52
  expect(child_output[:title]).to eq "A title"
43
53
  expect(child_output[:age]).to eq "38"
44
54
  expect(child_output[:description]).to eq "A description"
55
+
56
+ # named schema
57
+ parent_output = Parent.schema(:extras).resolve(search: 10, query: 'foo').output
58
+ child_output = Child.schema(:extras).resolve(search: 10, query: 'foo').output
59
+
60
+ expect(parent_output.keys).to match_array([:search])
61
+ expect(parent_output[:search]).to eq "10"
62
+ expect(child_output.keys).to match_array([:search, :query])
63
+ expect(child_output[:search]).to eq "10"
64
+ expect(child_output[:query]).to eq "foo"
45
65
  end
46
66
 
47
67
  it "inherits options" do
@@ -51,6 +71,11 @@ describe "classes including DSL module" do
51
71
  expect(grand_child_output[:title]).to eq "A title"
52
72
  expect(grand_child_output[:age]).to eq 38
53
73
  expect(grand_child_output[:description]).to eq "A description"
74
+
75
+ # named schema
76
+ grand_child_output = GrandChild.schema(:extras).resolve(search: "100", query: "bar").output
77
+ expect(grand_child_output.keys).to match_array([:search, :query])
78
+ expect(grand_child_output[:search]).to eq 100
54
79
  end
55
80
  end
56
81
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parametric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-06 00:00:00.000000000 Z
11
+ date: 2018-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler