Sha256: ef369cd5fc29bb19f7a3c1f4be05ac3c607d48b2945f599cefe385b4165aaaf9

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

require 'inflecto'

module Dry
  module Component
    class Loader
      class Component
        attr_reader :loader
        attr_reader :identifier, :path, :file

        def initialize(loader, input)
          @loader = loader

          @identifier = input.to_s.gsub(loader.path_separator, loader.namespace_separator)
          if loader.default_namespace
            re = /^#{Regexp.escape(loader.default_namespace)}#{Regexp.escape(loader.namespace_separator)}/
            @identifier = @identifier.sub(re, '')
          end

          @path = input.to_s.gsub(loader.namespace_separator, loader.path_separator)
          @file = "#{path}.rb"
        end

        def namespaces
          identifier.split(loader.namespace_separator).map(&:to_sym)
        end

        def constant
          Inflecto.constantize(constant_name)
        end

        def instance(*args)
          constant.new(*args)
        end

        private

        def constant_name
          Inflecto.camelize(path)
        end
      end

      PATH_SEPARATOR = '/'.freeze

      attr_reader :default_namespace
      attr_reader :namespace_separator
      attr_reader :path_separator

      def initialize(config)
        @default_namespace = config.default_namespace
        @namespace_separator = config.namespace_separator
        @path_separator = PATH_SEPARATOR
      end

      def load(component_path)
        Component.new(self, component_path)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-component-0.4.3 lib/dry/component/loader.rb
dry-component-0.4.2 lib/dry/component/loader.rb