Class: Axn::Internal::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/internal/registry.rb

Defined Under Namespace

Classes: DuplicateError, NotFound

Class Method Summary collapse

Class Method Details

.allObject



43
44
45
# File 'lib/axn/internal/registry.rb', line 43

def all
  @items ||= built_in.dup
end

.built_inObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/axn/internal/registry.rb', line 12

def built_in
  @built_in ||= begin
    # Get the directory name from the class name (e.g., "Strategies" -> "strategies")
    dir_name = name.split("::").last.underscore

    # Load all files from the directory
    files = ::Dir[File.join(registry_directory, dir_name, "*.rb")]
    files.each { |file| require file }

    # Get all modules defined within this class
    constants = self.constants.map { |const| const_get(const) }
    items = select_constants_to_load(constants)

    # Convert module names to keys
    items.to_h do |item|
      name = item.name.split("::").last
      key = name.underscore.to_sym
      [key, item]
    end
  end
end

.clear!Object



47
48
49
# File 'lib/axn/internal/registry.rb', line 47

def clear!
  @items = built_in.dup
end

.find(name) ⇒ Object

Raises:

  • (not_found_error_class)


51
52
53
54
55
56
# File 'lib/axn/internal/registry.rb', line 51

def find(name)
  raise not_found_error_class, "#{item_type} name cannot be nil" if name.nil?
  raise not_found_error_class, "#{item_type} name cannot be empty" if name.to_s.strip.empty?

  all[name.to_sym] or raise not_found_error_class, "#{item_type} '#{name}' not found"
end

.register(name, item) ⇒ Object

Raises:

  • (duplicate_error_class)


34
35
36
37
38
39
40
41
# File 'lib/axn/internal/registry.rb', line 34

def register(name, item)
  items = all # ensure built_in is initialized
  key = name.to_sym
  raise duplicate_error_class, "#{item_type} #{name} already registered" if items.key?(key)

  items[key] = item
  items
end