Module: Compote::Schema

Defined in:
lib/compote/schema.rb

Class Method Summary collapse

Class Method Details

.apply_extends(initial_data, &get_data) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/compote/schema.rb', line 307

def self.apply_extends ( initial_data, &get_data )

  extends = initial_data.dig 'compote', 'extends'

  return initial_data.clone unless extends

  extending_datas = extends.map do | key, options |

    data = get_data.call key

    data = data.select { | key, value | options[ 'only' ].include? key } if options[ 'only' ]

    data = data.reject { | key, value | options[ 'except' ].include? key } if options[ 'except' ]

    data

  end

  extending_datas += [ initial_data ]

  resulted_data = merge extending_datas

  resulted_data

end

.merge(datas) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/compote/schema.rb', line 333

def self.merge ( datas )

  result = {}

  datas.each do | data |

    data.keys.each do | key |

      values = [ result[ key ], data[ key ] ]

      result[ key ] = begin

        if values.all? { | value | value.is_a? Hash }

          merge values

        elsif values.all? { | value | value.is_a? Array } && ! %w( command entrypoint ).include?( key )

          values.first + values.last

        else

          values.last.clone

        end

      end

    end

  end

  result

end

.normalize(config, value, mappings = MAPPINGS) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/compote/schema.rb', line 233

def self.normalize ( config, value, mappings = MAPPINGS )

  validate! value if mappings == MAPPINGS


  value = value.clone


  class_mappings = mappings.select { | key, value | key.is_a? Class }

  unless class_mappings.empty?

    class_mapping = class_mappings[ value.class ]

    value = class_mapping.call value, config if class_mapping

  end


  paths_mappings = mappings.select { | key, value | key.is_a? String }

  unless paths_mappings.empty? || ! value.is_a?( Enumerable ) || value.empty?

    if paths_mappings.keys == [ '*' ]

      path_mappings = paths_mappings[ '*' ]

      if value.is_a? Hash

        value = value.map do | key, value |

          [ key, normalize( config, value, path_mappings ) ]

        end.to_h

      end

      if value.is_a? Array

        value = value.map do | value |

          normalize config, value, path_mappings

        end

      end

    elsif value.is_a? Hash

      value = value.map do | key, value |

        path_mappings = paths_mappings[ key ]

        if path_mappings

          [ key, normalize( config, value, path_mappings ) ]

        else

          [ key, value ]

        end

      end.to_h

    end

  end


  value

end

.validate!(data) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/compote/schema.rb', line 221

def self.validate! ( data )

  scheme = Pathname.new( __FILE__ ).join( '../schema.json' ).to_path

  JSON::Validator.validate! scheme, data

rescue JSON::Schema::ValidationError => error

  raise ConfigFormatError.new error: error, data: data

end