Sha256: d436d14c39acff870094fc672d967712d7bdddf7c47f566956a6319c4076fc23

Contents?: true

Size: 810 Bytes

Versions: 2

Compression:

Stored size: 810 Bytes

Contents

# frozen_string_literal: true

require 'key_tree/loader/nil'

module KeyTree
  # Module to manage key tree loaders
  module Loader
    BUILTIN_LOADERS = {
      json: 'JSON',
      yaml: 'YAML', yml: 'YAML'
    }.freeze

    class << self
      def [](type)
        type = type.to_sym if type.respond_to?(:to_sym)
        loaders[type] || @fallback
      end

      def []=(type, loader_class)
        type = type.to_sym if type.respond_to?(:to_sym)
        loaders[type] = loader_class
      end

      attr_writer :fallback, :loaders
      alias fallback fallback=

      private

      def loaders
        @loaders ||= BUILTIN_LOADERS.each_with_object({}) do |pair, result|
          type, name = pair
          result[type] = const_get(name) if const_defined?(name)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
key_tree-0.8.0 lib/key_tree/loader.rb
key_tree-0.7.0 lib/key_tree/loader.rb