Class: Railsmith::BaseService::AssociationRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/railsmith/base_service/association_registry.rb

Overview

Holds all AssociationDefinition objects declared on a service class. Supports inheritance via #dup — subclasses get their own copy that can be extended without affecting the parent.

Instance Method Summary collapse

Constructor Details

#initializeAssociationRegistry

Returns a new instance of AssociationRegistry.



9
10
11
# File 'lib/railsmith/base_service/association_registry.rb', line 9

def initialize
  @definitions = {}
end

Instance Method Details

#[](name) ⇒ Object



25
26
27
# File 'lib/railsmith/base_service/association_registry.rb', line 25

def [](name)
  @definitions[name.to_sym]
end

#allObject

All registered definitions in declaration order.



21
22
23
# File 'lib/railsmith/base_service/association_registry.rb', line 21

def all
  @definitions.values
end

#any?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/railsmith/base_service/association_registry.rb', line 29

def any?
  @definitions.any?
end

#dupObject

Returns a new AssociationRegistry with the same definitions. AssociationDefinition objects are frozen so sharing them is safe.



39
40
41
42
43
# File 'lib/railsmith/base_service/association_registry.rb', line 39

def dup
  copy = self.class.new
  @definitions.each_value { |d| copy.register(d) }
  copy
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/railsmith/base_service/association_registry.rb', line 33

def empty?
  @definitions.empty?
end

#register(definition) ⇒ Object

Register an AssociationDefinition. Later registrations with the same name overwrite earlier ones (allows subclass override).



15
16
17
18
# File 'lib/railsmith/base_service/association_registry.rb', line 15

def register(definition)
  @definitions[definition.name] = definition
  self
end