Class: Dry::DependencyInjection::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/dependency_injection/importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ Importer

Returns a new instance of Importer.



7
8
9
# File 'lib/dry/dependency_injection/importer.rb', line 7

def initialize(container)
  @container = container
end

Instance Method Details

#finalizeObject



30
31
32
# File 'lib/dry/dependency_injection/importer.rb', line 30

def finalize
  @container.finalize if @container.respond_to?(:finalize)
end

#import(path, prefix = '') ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dry/dependency_injection/importer.rb', line 11

def import(path, prefix = '')
  unless File.directory?(path)
    raise ArgumentError.new("path must be  directory: #{path}")
  end
  full = File.expand_path(path)
  Dir[File.join(full, prefix, '**', '*.rb')].each do |file|
    next if block_given? && !yield(file)
    require file
    subpath = file.gsub(/#{full}\/|\.rb/, '')
    class_name = Dry::Core::Inflector.camelize(subpath)
    clazz = Dry::Core::Inflector.constantize(class_name)
    if clazz.is_a?(Class)
      key = subpath.gsub('/', '.')
      @container.register(key, clazz)
    end
  end
  self
end