Sha256: 8663e3c0aa801fffc39d006b8b09c17d76cd880747ab09c85f40c874f0383abc

Contents?: true

Size: 1013 Bytes

Versions: 2

Compression:

Stored size: 1013 Bytes

Contents

# frozen_string_literal: true

module SolidusAdmin
  class ComponentRegistry
    ComponentNotFoundError = Class.new(NameError)

    def initialize
      @names = {}
    end

    def []=(key, value)
      @names[key] = value
    end

    def [](key)
      if @names[key]
        @names[key].constantize
      else
        infer_constant_from(key)
      end
    end

    private

    def infer_constant_from(key)
      "solidus_admin/#{key}/component".classify.constantize
    rescue NameError
      prefix = "#{SolidusAdmin::Configuration::ENGINE_ROOT}/app/components/solidus_admin/"
      suffix = "/component.rb"
      dictionary = Dir["#{prefix}**#{suffix}"].map { _1.delete_prefix(prefix).delete_suffix(suffix) }
      corrections = DidYouMean::SpellChecker.new(dictionary: dictionary).correct(key.to_s)

      raise ComponentNotFoundError.new(
        "Unknown component #{key}#{DidYouMean.formatter.message_for(corrections)}",
        key.classify,
        receiver: ::SolidusAdmin
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_admin-0.3.1 lib/solidus_admin/component_registry.rb
solidus_admin-0.3.0 lib/solidus_admin/component_registry.rb