module Compote::Schema
Constants
- MAPPINGS
Public Class Methods
apply_extends( initial_data, &get_data )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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