@@ -12,45 +12,21 @@ def variant
1212
1313 def rule_config
1414 case variant
15- when :contains_key
16- self . contains_key
17- when :contains_type
18- self . contains_type
19- when :item_of_collection
20- self . item_of_collection
2115 when :number_range
2216 self . number_range
2317 when :regex
2418 self . regex
25- when :input_types
26- self . input_types
27- when :return_type
28- self . return_type
29- when :parent_type
30- self . parent_type
3119 else
3220 raise UnexpectedRuleType , "Unknown rule type #{ variant } "
3321 end
3422 end
3523
3624 def create ( variant , config )
3725 case variant
38- when :contains_key
39- self . contains_key = DataTypeContainsKeyRuleConfig . new ( config )
40- when :contains_type
41- self . contains_type = DataTypeContainsTypeRuleConfig . new ( config )
42- when :item_of_collection
43- self . item_of_collection = DataTypeItemOfCollectionRuleConfig . from_hash ( config )
4426 when :number_range
4527 self . number_range = DataTypeNumberRangeRuleConfig . new ( config )
4628 when :regex
4729 self . regex = DataTypeRegexRuleConfig . new ( config )
48- when :input_types
49- self . input_types = DataTypeInputTypesRuleConfig . new ( config )
50- when :return_type
51- self . return_type = DataTypeReturnTypeRuleConfig . new ( config )
52- when :parent_type
53- self . parent_type = DataTypeParentTypeRuleConfig . from_hash ( config )
5430 else
5531 raise UnexpectedRuleType , "Unknown rule type #{ variant } "
5632 end
@@ -63,46 +39,6 @@ def self.create(variant, config)
6339 end
6440 end
6541
66- DataTypeContainsKeyRuleConfig . class_eval do
67- def to_h
68- {
69- key : self . key ,
70- data_type_identifier : self . data_type_identifier . to_h ,
71- }
72- end
73-
74- def self . from_hash ( config )
75- new (
76- key : config [ :key ] ,
77- data_type_identifier : DataTypeIdentifier . from_hash ( config [ :data_type_identifier ] )
78- )
79- end
80- end
81-
82- DataTypeContainsTypeRuleConfig . class_eval do
83- def to_h
84- {
85- data_type_identifier : self . data_type_identifier . to_h ,
86- }
87- end
88-
89- def self . from_hash ( config )
90- new ( data_type_identifier : DataTypeIdentifier . from_hash ( config [ :data_type_identifier ] ) )
91- end
92- end
93-
94- DataTypeItemOfCollectionRuleConfig . class_eval do
95- def to_h
96- {
97- items : self . items . map { |item | item . to_ruby ( true ) } ,
98- }
99- end
100-
101- def self . from_hash ( hash )
102- new ( items : hash . fetch ( :items ) . map { |item | Value . from_ruby ( item ) } )
103- end
104- end
105-
10642 DataTypeNumberRangeRuleConfig . class_eval do
10743 def to_h
10844 {
@@ -120,154 +56,5 @@ def to_h
12056 }
12157 end
12258 end
123-
124- DataTypeInputTypesRuleConfig . class_eval do
125- def to_h
126- {
127- input_types : self . input_types . map { |input_type | input_type . to_h }
128- }
129- end
130-
131- def self . from_hash ( hash )
132- new (
133- input_types : hash . fetch ( :input_types ) . map { |input_type | DataTypeInputType . from_hash ( input_type ) }
134- )
135- end
136- end
137-
138- DataTypeInputTypesRuleConfig ::DataTypeInputType . class_eval do
139- def to_h
140- {
141- data_type_identifier : self . data_type_identifier ,
142- input_identifier : self . input_identifier ,
143- }
144- end
145-
146- def self . from_hash ( config )
147- new (
148- input_identifier : config [ :input_identifier ] ,
149- data_type_identifier : DataTypeIdentifier . from_hash ( config [ :data_type_identifier ] )
150- )
151- end
152- end
153-
154- DataTypeReturnTypeRuleConfig . class_eval do
155- def to_h
156- {
157- data_type_identifier : self . data_type_identifier ,
158- }
159- end
160-
161- def self . from_hash ( config )
162- new ( data_type_identifier : DataTypeIdentifier . from_hash ( config [ :data_type_identifier ] ) )
163- end
164- end
165-
166- DataTypeParentTypeRuleConfig . class_eval do
167- def to_h
168- {
169- parent_type : self . parent_type ,
170- }
171- end
172-
173- def self . from_hash ( config )
174- new ( parent_type : DataTypeIdentifier . from_hash ( config [ :parent_type ] ) )
175- end
176- end
177-
178- DataTypeIdentifier . class_eval do
179- def to_h
180- if !self . data_type_identifier . empty?
181- return { data_type_identifier : self . data_type_identifier }
182- elsif !self . generic_type . nil?
183- return { generic_type : self . generic_type . to_h }
184- elsif !self . generic_key . nil?
185- return { generic_key : self . generic_key }
186- end
187- end
188-
189- def from_hash ( config )
190- if config . keys . intersection ( [ :data_type_identifier , :generic_type , :generic_key ] ) . count > 1
191- raise UnexpectedType , "Cannot have more than one type"
192- end
193-
194- if config . key? ( :data_type_identifier )
195- self . data_type_identifier = config [ :data_type_identifier ]
196- elsif config . key? ( :generic_type )
197- self . generic_type = GenericType . from_hash ( config [ :generic_type ] )
198- elsif config . key? ( :generic_key )
199- self . generic_key = config [ :generic_key ]
200- else
201- raise UnexpectedType , "Unknown type"
202- end
203-
204- self
205- end
206-
207- def self . from_hash ( config )
208- new . from_hash ( config )
209- end
210- end
211-
212- GenericType . class_eval do
213- def to_h
214- {
215- data_type_identifier : self . data_type_identifier ,
216- generic_mappers : self . generic_mappers . map ( &:to_h ) ,
217- }
218- end
219-
220- def from_hash ( config )
221- if config . key? ( :data_type_identifier )
222- self . data_type_identifier = config . fetch ( :data_type_identifier )
223- end
224-
225- if config . key? ( :generic_mappers )
226- self . generic_mappers . clear
227- config [ :generic_mappers ] . each do |mapper_config |
228- self . generic_mappers << GenericMapper . from_hash ( mapper_config )
229- end
230- end
231-
232- self
233- end
234-
235- def self . from_hash ( config )
236- new . from_hash ( config )
237- end
238- end
239-
240- GenericMapper . class_eval do
241- def to_h
242- {
243- target : self . target ,
244- source : self . source . map ( &:to_h ) ,
245- generic_combinations : self . generic_combinations . map ( &:to_h ) ,
246- }
247- end
248-
249- def from_hash ( config )
250- self . target = config [ :target ]
251- if config . key? ( :source )
252- self . source . clear
253- config [ :source ] . each do |source |
254- self . source << DataTypeIdentifier . from_hash ( source )
255- end
256- end
257-
258- if config . key? ( :generic_combinations )
259- self . generic_combinations . clear
260- config [ :generic_combinations ] . each do |combo |
261- self . generic_combinations << GenericMapper ::GenericCombinationStrategy . resolve ( combo )
262- end
263- end
264-
265- self
266- end
267-
268- def self . from_hash ( config )
269- new . from_hash ( config )
270- end
271- end
27259 end
27360end
0 commit comments