Sha256: cde4bd95402681ae9254c4d41e5d23b36cf15b85307ab148fa766788540290ad

Contents?: true

Size: 1.46 KB

Versions: 97

Compression:

Stored size: 1.46 KB

Contents

module CC
  class Config
    module Validation
      module HashValidations
        def validate_hash_data
          unless data.is_a?(Hash)
            errors << "Config file should contain a hash, not a #{data.class.to_s.downcase}"
            return false
          end
          true
        end

        def validate_key_type(key, types)
          if types.is_a?(Class)
            return validate_key_type(key, [types])
          elsif data.key?(key)
            unless types.include?(data[key].class)
              errors << key_type_error_message(key, types)
              return false
            end
          end
          true
        end

        def key_type_error_message(key, types)
          if types.one?
            klass_name = types[0].to_s.downcase
            article =
              if klass_name[0] == "a"
                "an"
              else
                "a"
              end
            "'#{key}' must be #{article} #{klass_name}"
          elsif types == [TrueClass, FalseClass]
            "'#{key}' must be a boolean"
          else
            type_names = types.map(&:to_s).map(&:downcase)
            "'#{key}' must be one of #{type_names.join(", ")}"
          end
        end

        def warn_unrecognized_keys(recognized_keys)
          unknown_keys = data.keys.reject { |k| recognized_keys.include?(k) }
          unknown_keys.each do |key|
            warnings << "unrecognized key '#{key}'"
          end
        end
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
codeclimate-0.85.27 lib/cc/config/validation/hash_validations.rb
codeclimate-0.85.26 lib/cc/config/validation/hash_validations.rb
codeclimate-0.85.25 lib/cc/config/validation/hash_validations.rb
codeclimate-0.85.24 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.54 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.53 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.52 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.51 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.50 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.49 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.48 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.47 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.46 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.45 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.44 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.43 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.42 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.41 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.40 lib/cc/config/validation/hash_validations.rb
codeclimate-fede-0.85.39 lib/cc/config/validation/hash_validations.rb