parametric 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/parametric/dsl.rb +2 -2
- data/lib/parametric/struct.rb +11 -6
- data/lib/parametric/version.rb +1 -1
- data/spec/struct_spec.rb +26 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab73665f1c6b574e0710689244bf7aad39d37fbd2e6465c8f2185f9bd9421090
|
4
|
+
data.tar.gz: 829999fb2f7ef34d136b24fc3fd7f7260785bca8cb8fc6dabcae65e3c1f1e14a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 428e567cf0a782f7a424280b79ea10320c7326a95011d4591350b3d96024c67f5e07616fd62103b23f7a98b9e56e970275c668bb285f8192ac7a05950bfd027c
|
7
|
+
data.tar.gz: 9fd12296245cdcee7a29a7882ae8da866d64ffde1fa065c25136a4a661b30cf97f351bda588b4058b7707c9b2e7688bd0e2b356f2c3438daf8f27f3ecf87a16e
|
data/lib/parametric/dsl.rb
CHANGED
@@ -57,10 +57,10 @@ module Parametric
|
|
57
57
|
return current_schema unless new_schema
|
58
58
|
|
59
59
|
@schemas[key] = current_schema ? current_schema.merge(new_schema) : new_schema
|
60
|
-
|
60
|
+
parametric_after_define_schema(@schemas[key])
|
61
61
|
end
|
62
62
|
|
63
|
-
def
|
63
|
+
def parametric_after_define_schema(sc)
|
64
64
|
# noop hook
|
65
65
|
end
|
66
66
|
end
|
data/lib/parametric/struct.rb
CHANGED
@@ -55,7 +55,7 @@ module Parametric
|
|
55
55
|
end
|
56
56
|
|
57
57
|
# this hook is called after schema definition in DSL module
|
58
|
-
def
|
58
|
+
def parametric_after_define_schema(schema)
|
59
59
|
schema.fields.keys.each do |key|
|
60
60
|
define_method key do
|
61
61
|
_graph[key]
|
@@ -69,6 +69,14 @@ module Parametric
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
+
def parametric_build_class_for_child(key, child_schema)
|
73
|
+
klass = Class.new do
|
74
|
+
include Struct
|
75
|
+
end
|
76
|
+
klass.schema = child_schema
|
77
|
+
klass
|
78
|
+
end
|
79
|
+
|
72
80
|
def wrap(key, value)
|
73
81
|
field = schema.fields[key]
|
74
82
|
return value unless field
|
@@ -78,11 +86,8 @@ module Parametric
|
|
78
86
|
# find constructor for field
|
79
87
|
cons = field.meta_data[:schema]
|
80
88
|
if cons.kind_of?(Parametric::Schema)
|
81
|
-
klass =
|
82
|
-
|
83
|
-
end
|
84
|
-
klass.schema = cons
|
85
|
-
klass.after_define_schema(cons)
|
89
|
+
klass = parametric_build_class_for_child(key, cons)
|
90
|
+
klass.parametric_after_define_schema(cons)
|
86
91
|
cons = klass
|
87
92
|
end
|
88
93
|
cons ? cons.new(value) : value.freeze
|
data/lib/parametric/version.rb
CHANGED
data/spec/struct_spec.rb
CHANGED
@@ -99,6 +99,32 @@ describe Parametric::Struct do
|
|
99
99
|
expect(instance.friends.first.age).to eq 10
|
100
100
|
end
|
101
101
|
|
102
|
+
it 'wraps nested schemas in custom class' do
|
103
|
+
klass = Class.new do
|
104
|
+
include Parametric::Struct
|
105
|
+
|
106
|
+
def self.parametric_build_class_for_child(key, child_schema)
|
107
|
+
Class.new do
|
108
|
+
include Parametric::Struct
|
109
|
+
schema child_schema
|
110
|
+
def salutation
|
111
|
+
"my age is #{age}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
schema do
|
117
|
+
field(:name).type(:string).present
|
118
|
+
field(:friends).type(:array).schema do
|
119
|
+
field(:age).type(:integer)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
user = klass.new(name: 'Ismael', friends: [{age: 43}])
|
125
|
+
expect(user.friends.first.salutation).to eq 'my age is 43'
|
126
|
+
end
|
127
|
+
|
102
128
|
it "wraps regular schemas in structs" do
|
103
129
|
friend_schema = Parametric::Schema.new do
|
104
130
|
field(:name)
|
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.
|
4
|
+
version: 0.2.5
|
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-11-
|
11
|
+
date: 2018-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|