Sha256: 73a3ce00c34e97f31b3cd9b1cf031bca006e9fee278c05c4ae693bc86d86db2c

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module Configurations
  module Maps
    class Types
      attr_reader :map
      class Entry
        attr_reader :type

        def initialize(type)
          @type = type
        end

        def valid?(value)
          !@type || value.is_a?(@type)
        end
      end

      def initialize(reader = Readers::Tolerant.new)
        @map = {}
        @reader = reader
      end

      def add(type, properties)
        properties.each do |property|
          add_entry(property, type, @map)
        end
      end

      def test!(path, value)
        entry = @reader.read(@map, path)
        return unless entry

        fail(
          ConfigurationError,
          "#{path.print} must be configured with #{entry.type} (got #{value})",
          caller
        ) unless entry.valid?(value)
      end

      def add_entry(property, type, subtree)
        if property.is_a?(Hash)
          property.each do |key, val|
            subtree[key] = add_entry(val, type, subtree.fetch(key, {}))
          end
        elsif property.is_a?(Array)
          property.each do |val|
            add_entry(val, type, subtree)
          end
        else
          subtree[property] = Entry.new(type)
        end

        subtree
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
configurations-2.2.2 lib/configurations/maps/types.rb
configurations-2.2.1 lib/configurations/maps/types.rb