Class: Polycrystal::Registry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Singleton
Defined in:
lib/polycrystal/registry.rb

Defined Under Namespace

Classes: CrystalModule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



18
19
20
# File 'lib/polycrystal/registry.rb', line 18

def initialize
  @modules = []
end

Instance Attribute Details

#modulesObject (readonly)

Returns the value of attribute modules.



14
15
16
# File 'lib/polycrystal/registry.rb', line 14

def modules
  @modules
end

Instance Method Details

#find_module(path:, file:) ⇒ Object



38
39
40
# File 'lib/polycrystal/registry.rb', line 38

def find_module(path:, file:)
  modules.find { |mod| mod.path == path && mod.file == file }
end

#register(path:, file: nil, modules: nil, unshift: false) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polycrystal/registry.rb', line 22

def register(path:, file: nil, modules: nil, unshift: false)
  raise ArgumentError, 'include file is requred to wrap modules' if file.nil? && !modules.nil?

  existing = find_module(file: file, path: path)
  if existing
    existing.modules += modules if modules
  else
    new_mod = CrystalModule.new(
      path: path,
      file: file,
      modules: modules
    )
    unshift ? @modules.unshift(new_mod) : @modules.push(new_mod)
  end
end